Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Change code to use new bash shell syntax.

...

Here is a really straightforward example of using xargs to calculate a MD5 hash on every file in the current directory,

Code Block
langlanguagehtmlbash
ls | xargs -t -n1 md5

This is how it works,

...

Thanks to the -t the output will be shown on screen,

Code Block
languagebash
md5 planetary.doc
MD5 (bash) = ab5970d50d67bcafe5c554387f76534e
md5 Superman.jpg
MD5 (cat) = cdefa50d737dfcf8dc57886ea1a758c4

...

Now let's get more advanced and use -I to allow substitution. First we'll create a some temporary files,

Code Block
langlanguagehtmlbash
mkdir temp
cd temp
touch files1 file2 file3 # Creates 3 empty files

Now using xargs we will add the txt extension to each file,

Code Block
languagebash
ls | xargs -t -I {} mv {} {}.txt
mv file1 file1.txt
mv file2 file2.txt
mv file3 file3.txt

The -I {} specifies that the arguments by ls will be placed in the location of the {} called the replacement string. In fact you use whatever variable name you want instead of {}. For example, the following will also work,

Code Block
langlanguagehtmlbash
ls | xargs -t -I varX md5 varX
md5 file1.txt
MD5 (file1.txt) = d41d8cd98f00b204e9800998ecf8427e
md5 file2.txt
MD5 (file2.txt) = d41d8cd98f00b204e9800998ecf8427e
md5 file3.txt
MD5 (file3.txt) = d41d8cd98f00b204e9800998ecf8427e

...

The echo command is useful to test and see what xargs will be looping through,

Code Block
langlanguagehtmlbash
ls | xargs -I {} echo "mv {} {}.txt"
mv file1 file1.txt
mv file2 file2.txt
mv file3 file3.txt

...

Try to memorize this command,

Code Block
langlanguagehtmlbash
find [folder] -type f | xargs -I {} grep -li "text" {}

find [folder] -type f {search the specified folder for all files, returns full path of each file}
    | xargs -I {} grep -li "[text]" {} {piped into xargs to grep for all files containing specified text ignoring case}

...

Try to memorize this command,

Code Block
langlanguagehtmlbash
find [folder] -type f | xargs -I {} grep -li "text" {} | xargs perl -pi -e 's/[text_to_search_for]/[text_to_search_for]/g'

find [folder] -type f                  # search the specified folder for all files, returns full path of each file
    | xargs -I {} grep -li "[text]" {} # piped into xargs with to grep for all files containing specified text ignoring case}
    | xargs perl -pi -e 's/[text_to_search_for]/[text_to_replace_with]/g' # pipe list of files and search/replace with specified text}

...

List directories from largest to smallest at the top level only.

Code Block
languagebash
du -sk * | sort

Long Running Processes

...

Other Useful Commands

Code Block
langlanguagehtmlbash
digest -a md5 -v /path/to/file