Skip to Content

Five unix commands everyone should know

I'm a long time user and lover of the command line. Whether it is for Mac OSX or linux there are a few simple tricks that will have you praising everything UNIX related.

Number 1. - Find a phrase whithin a group of files

This command is useful for those times when you can see something on a web page you are working on but you just can't seem to find the source file that produces.

  1. find ./ |xargs grep "phrase you are searching for"

Number 2. - Display file sizes in a readable fashion

When you are trying to figure out how big something is it can be daunting to look at something like this.

  1. # OUTPUT of command df
  2. /dev/sdb 433455904 28225416 383212184 7% /mnt
  3. /dev/sdh 30963708 2644996 26745848 9% /home/jdurand/sites

Simply type -h after any command that list files.

Examples:
  1. ls -hal
  2. df -h
  3. du -h *

Number 3. - Finding big files

The following code will list any file that has more than 20 MB and display how big the file is in human readable format.

  1. find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $8 ": " $5 }'

Number 4. - Removing directories with lots of files

If you have ever gotten the error message "Argument list too long" after trying to rm -rf a directory it is because their are to many files in the directory you are trying to delete and the rm tool does not like it very much. The solution is to use your trusty friend find and pipe the output to the rm command.

  1. # warning this command can be very harmful . Use with caution
  2. find . -name 'file-*' | xargs rm

Number 5. - Rsync files over non-standard ssh port

For security purposes many people change the port that ssh is running. It makes it harder for nmap and spam bots to know how to handle it. The one side effect is that many tools including Rsync use port 22 by default. If you find yourself in this situation simply use the command below to get out of it.

  1. rsync -avz -e "ssh -p PORT_NUMBER_HERE" user@remoteip:/path/to/files/ /local/path/

If you are reading this article and want to leave a comment, please feel free to use the comment box below. Questions are welcome as well.