//Browsing archives (Page 4 of 13)
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow

To find out the number of files of each type in your current directory try the following:

find ${*-.} -type f | xargs file | awk -F, '{print $1}' | awk '{$1=NULL;print $0}' | sort | uniq -c | sort -nr  

5 PHP script text 2 data 2 Zip archive data 2 GIF image data 1 PNG image data

(You may want to add this as an alias rather than type it in each time!)

View Comments »

Add the following to your profile/bashrc etc:
cd() {
  if [ -n "$1" ]; then
    builtin cd "$@" && ls
    builtin cd ~ && ls

View Comments »

In Linux:

import -window root screenshot-$(date +%Y%m%d%k%M).png

View Comments »

Add the following alias and function to your profile to be able to copy and paste files at the command line:

ccopy(){ cp $1 /tmp/ccopy.$1; }
alias cpaste="ls /tmp/ccopy* | sed 's|[^\.]*.\.||' | xargs -I % mv /tmp/ccopy.% ./%"

You can see below how this can be used:
blackbird:~/tst tks1$ ls
1.txt   2.txt   t1.tss  t2.tss  t3.tss
blackbird:~/tst tks1$ ccopy 1.txt
blackbird:~/tst tks1$ ccopy 2.txt

blackbird:~/tst tks1$ cd ../tst2 blackbird:~/tst2 tks1$ ls
blackbird:~/tst2 tks1$ cpaste
blackbird:~/tst2 tks1$ ls 1.txt 2.txt

View Comments »

Disable history for a particular account in bash with:

(in home dir)

rm .bash_history

ln -sf /dev/null .bash_history

View Comments »

Often I find myself using Ctrl-R in Bash to get an old command, only to find that too many days have passed and it's no longer in the .bash_history file.

It is possible to increase the number of lines in the history file, but there can always be a moment when you'll need a long command from many months ago. The solution below uses the PROMPT_COMMAND variable, a command that bash executes before showing each prompt. Here are the two lines to add to your profile:


PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $$ $USER \ "$(history 1)" >> ~/.bash_permanent_history'

If a previous PROMPT_COMMAND was set, it gets executed before this and then appends a line of the format:


to a file called .bash_permanent_history in the current user home.

Adding the username is useful to distinguish between "sudo -s" sessions and normal sessions which retain the same value for "~/", and so append lines to the same .bash_permanent_history file.

View Comments »

To remove empty directories (even if filenames or dirnames contain spaces or weird characters) from a tree you can do:

find . -type d -empty -print0 | xargs -0 rmdir

View Comments »

Convert mac addresses such as 000000abde00 into 00:00:00:ab:de:00

awk '{for(i=10;i>=2;i-=2)$0=substr($0,1,i)":"substr($0,i+1);print}' macaddress_list
sed 's/\(..\)/\1:/g;s/:$//' macaddress_list

// sil at infiltrated.net

View Comments »

This checks if a daemon is running, if not it starts the daemon. Great for daemons that need to always be running. Can be used with cron

ps -C someprogram || { someprogram & }

// sil at infiltrated dot net

View Comments »

This is a little known and very underrated shell variable. CDPATH does for the cd built-in what PATH does for executables. By setting this wisely, you can cut down on the number of key-strokes you enter per day.

For example:
$ export CDPATH='.:~:/usr/local/apache/htdocs:/disk/backups'

Now, whenever you use the cd command, bash will check all the directories in the $CDPATH list for matches to the directory name.

View Comments »

You can use diff to see the differences between two files, but it can be useful to see what is the same and more clearly how they differ. This is where comm comes in useful.

comm tells you what information is common to two lists and what information appears uniquely in one or the other.

$ find . -type f -print -exec cat {} \;

$ comm 1.txt 2.txt 

The first column shows lines only in the first file, the second column lines from the second file and the third column lines from both.

This can be made easier still by adding a bit of perl:
$ comm 1.txt 2.txt | perl -pe 's/^/1: /g;s/1: \t/2: /g;s/2: \t/A: /g;' | sort
1: b
2: e
A: a
A: c

View Comments »

You can view unprintable and non ASCII characters in a file with 'cat -v -t -e'


$ cat myfile
Hello,  World!    
$ cat -v -t -e myfile 
Hello, ^IWorld!    $

In this file there is a tab between the two words and 4 spaces at the end of the line that would not have been visible with a plain cat.

View Comments »

It's easy to remove (or copy, move etc.) all files that match a given criteria, but harder to move all but ones excluded by a criteria.

To do this we can combine grep's -v option with Unix command substitution:

$ ls
1.txt   2.txt   3.txt   4.txt
$ rm `ls | grep -v 4\.txt`
$ ls

View Comments »

dos2unix requires the name of an input and output file so it can be hard to run on a list of files. The following gets around this and will run dos2unix on all files in a directory:

for f in `find * -type f`; do echo "dos2unix $f $f"; done | sh

View Comments »

ps | grep processName | grep -v grep | awk '{print "kill -9 " $2}' | sh

Take the '| sh' off the end before running to check the commands that will be run.

View Comments »

Print a random shell-fu tip:
links -dump "http://www.shell-fu.org/lister.php?random" | grep -A 100 -- ----


View Comments »

Save time when making multiple files or directories (or performing any command requiring a list) where the file names only slightly differ.

$ ls
$ touch tst{1,2,3,4,5}.txt
$ ls
tst1.txt        tst2.txt        tst3.txt        tst4.txt        tst5.txt
$ mkdir dir{1,2,3}
$ ls
dir1       dir2       dir3       tst1.txt   tst2.txt   tst3.txt   tst4.txt   tst5.txt

View Comments »

If, like me, you often make mistakes on the command line, try using the history shortcut '^^' to repeat the last command with changes.

For example:

$ cat fiel
cat: fiel: No such file or directory
$ ^el^le
cat file

View Comments »

These handy one-liners are used to perform the famous Caesar cipher encryption where letters of the alphabet are shifted by differing margins. The same tr command can be used to encrypt and decrypt encoded files/strings.

Rot-13 encryption:
$ cat file|tr A-Za-z N-ZA-Mn-za-m

$ echo -n "Secret Msg"|tr A-Za-z N-ZA-Mn-za-m

Rot-47 encryption:
$ cat file|tr '!-~' 'P-~!-O'

$ echo -n "Secret Msg"|tr '!-~' 'P-~!-O'

View Comments »

Don't have telnet or netcat handy for making a socket connection? Most Linux distros - not likely Debian - have this functionality built directly into Bash. The following will pull my site's index source on port 80, replace with any URL.


exec 3<>/dev/tcp/kinqpinz.info/80
echo -e "GET / HTTP/1.1\nHost: kinqpinz.info;\nConnection: close\n\n">&3
cat <&3

View Comments »

Zero out files leaving directory and file sets in place - used for archival purposes.
/// bash script file: cdn (located in /usr/local/bin)
# Cataboligne - zero a single file passed in argument 1, and make a log entry
cat /dev/null > "$1"
echo "zero $1" >> $HOME/.zero-log
/// end bash script

command line:
find  -wholename '*/*.[mM][pP][32cC]' -exec echo {}\; -exec cdn {} \;

This example zeros all mp2/mp3/mpc of any case anywhere under the current directory + sub directories and tells you what got zapped.

Do the same thing for all files in a directory from a file manager (such as rox) menu shortcut.
/// bash script file: xt-zerofiles (located in /usr/local/bin)
# Cataboligne - zero all files in a directory structure with double logging
# and verify, called from rox sub menu
cd "$@"
echo "" > /tmp/.cont
xterm -geometry 70x1 -title 'Zero Caution!' -e 'read -p "zero files in $PWD: Y/N? " RSP;echo $RSP > /tmp/.cont'
RSP2=`cat /tmp/.cont`
if [[ "$RSP2" != "Y" && "$RSP2" != "Yes" && "$RSP2" != "y" ]]; then exit; fi;
echo >> $HOME/.zero-log-summary
echo >> $HOME/.zero-log        
echo $(date) " zero fn() = $PWD" >> $HOME/.zero-log-summary
echo "------------------------------" >> $HOME/.zero-log-summary
echo $(date) " zero fn() = $PWD" >> $HOME/.zero-log        
echo "------------------------------" >> $HOME/.zero-log        
find -type f -exec cdn {} \;
rm -f /tmp/.cont
echo >> $HOME/.zero-log-summary
echo >> $HOME/.zero-log
/// end bash script

The method of calling this script depends on the file manager. With rox you right click on a file (such as a directory) and select "Customize menu" on the selected file sub menu. A rox window pops open with all the menu items for that file type and you can then open rox /usr/local/bin and drag and drop a link for xt-zerofiles.
I recommend a link instead of a copy so if you update the script you dont have to figure out where you copied it. The custom menu is my single favorite rox feature.

The script pops open a small bash window to ask for verification due to the risk of running such a command arbitrarily. And yes, I keep two logs there, you could reduce it to the $HOME/.zero-log or even have no logging.

/// Cataboligne

View Comments »

vimdiff <(svn cat -r ##ver## foo) foo

will put you in the usual vimdiff output, but comparing between ##ver## and your current version of foo

View Comments »

When specifying time with find options such as -mmin (minutes) or -mtime (24 hour periods, starting from now), you can specify a number n to mean exactly n, -n to mean less than n, and +n to mean more than n. 2 For example:

find . -mtime -1  # find files modified within the past 24 hours
find . -mtime 1   # find files modified between 24 and 48 hours ago
find . -mtime +1  # find files modified more than 48 hours ago
find . -mmin +5 -mmin -10 # find files modifed between 5 and 10 minutes ago

View Comments »

Use pushd and popd to store directories to go back to later:

Blackbird:~ flsw$ pwd
Blackbird:~ flsw$ pushd tst	#current dir becomes tst 
~/tst ~
Blackbird:~/tster flsw$ pushd	#no argument moves back and forth
~ ~/tster

popd +n removes n entries from the stack without changing directory
pushd +n 'rotates' the stack so the nth directory moves to the top becoming the current directory.

View Comments »

Put the following alias in your profile to store a directory name to use later on:
alias sd='set \!:1=$cwd'

Example usage:
$ sd mydir
$ cd mydir

This method works for csh but not bash. For bash try the following function in your profile:
sd(){ export $1=$PWD; }

$ sd abc
$ cd $abc

View Comments »

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit