Once in a while I need to compress a bunch of files and directories into a single, easy to transport file that can be later de-compressed as needed.  Sometimes I do this for backup purposes and sometimes just to move websites to new hosts.  What’s nice about the tar command in linux is that it allows you to compress entire directories and all sub directories below it recursively.

To compress a directory, run this tar command:

tar cvzf <output file> <input directory>

just replace <output file> and <input file> respectively and you’ll have yourself a compressed tar file after it’s done executing.

To de-compress a tar file (untar), run this command:

tar xvfz <file name>

Again, if you tared up a directory, vs just a set of files, the untar command will decompress the directory itself retaining the original file structure, nice.

If you just want to look at the contents of a tar file without decompressing anything:

tar tzf <file name>

If you want a little more information on the Tar command, check out Duke’s tar guide.

Useful Linux Commands # 1

May 16th, 2008

There are several pretty cool linux commands that I use quite often during development. Some of these commands are basic stuff to the average developer, but I think newer/learning developers will find this information pretty good - so here is a list detailing a few useful tricks:

  • Grep - grep is short for “Global Regular Expression Print” and will search through a list of files for a regular expression pattern match, then print matched lines.
    Grep Command Example
    I like using grep to quickly check and see if a text string exists within a given file, that way I don’t need to open up the file and search through it manually, cutting down on my dev time. For example:
    grep ‘<your string>’ <file name>
    Will print out all occurrences of the <your string> string from the file <file name>. You could also do:
    grep -r “<some string or pattern>” .
    which will search all files and folders, recursively, and print out all occurrences of the <some string or pattern> string. Pretty cool, especially when you have an entire code base of files you want to search through if you ever need to find/change the name of a particular function that is used in many different places.
  • Pipe - No I’m not talking about smoking, but about sending program output into another program’s input. I use this all the time and makes my life a lot easier. For example, you could pipe the output from program “A” and use it as input to “B” input.
  • Use Pipe in conjunction with Grep - Grep is also really nice for searching output from other programs and we can utilize the pipe command for this - so for example, you could do a:
    w | grep <username>
    to get instances of a particular user who is logged into your system. The “|” (pipe) symbol just takes the output from the “w” command and uses it as input for the grep command (notice there is no <file name> argument for grep since the input was already piped in)
  • Use find to locate a file on your system. I hate searching for files manually, so luckily “find” does it for me:
    find . -name ‘<file name>’
    will find all files with the given file name starting from the current directory. Another example, you could also do a
    find . -name ‘*text*’
    to find all files names that contain the ‘text’ string, within your search path. Note that I’ve used the * wild cards on either side of the string to match file names that could have characters before and or after the matched pattern.
  • Using Find in conjunction Grep and Pipe for advanced searching - For example you could do
    find . -name “*.php” | grep -vE ‘/(<dir 1>|<dir 2>)/’ | xargs grep <some string>
    which will find the string of your choice within .php files from the directory you are currently in. Notice the “v” flag to grep which inverts where we want to search, this is good if we have a large directory that we already know we don’t need to look through. I’ve used the “E” flag here so that grep recognizes my directory list as a regular expression pattern, since I’m using the “|” OR operator (not to be confused with the pipe symbol outside of a regular expression). Also note the xargs command we are piping to which then automatically pipes a list of files into grep. So all in all, the above command is really nice if you want to search for all occurrences of a word or phrase, or maybe you want to search for a misspelled word in several different files - but you don’t know which files specifically, so find does this part for us!

If you know of any other commands or tricks that make your work easier, do share!