Linux command line magic with find and xargs

Once again I used find command to find the files. This time I wanted to find all tar archives and extract them to current directory.

Had to do some googling on how to pass the filenames to tar but here it is. The magic requires using -I switch for xargs.

$ find . -name '*.tar.gz' | xargs -I{} tar xvfz {}

The xargs man page describes the -I switch as

 -I replace-str
              Replace occurrences of replace-str in the initial-arguments with
              names read from standard input.  Also, unquoted  blanks  do  not
              terminate  input  items;  instead  the  separator is the newline
              character.  Implies -x and -L 1.

To extract tar files I use the command tar xvfz <filename>. If your tar files are not gzip compressed, leave out the z option.

Leave a comment