Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 17 Next »

xargs

Is a very useful program to take a list and run commands against that list.

Here is a really straightforward example of using xargs,

  • -t will show you what xargs is about to execute before it executes it.
  • -n1 limits the arguments passed by the directory to pass one argument, in this case one file name at a time.
Tin-Phams-iMac:PC tinpham$ ls | xargs -t -n1 md5
md5 planetary.doc
MD5 (bash) = ab5970d50d67bcafe5c554387f76534e
md5 Superman.jpg
MD5 (cat) = cdefa50d737dfcf8dc57886ea1a758c4
Tin-Phams-iMac:bin tinpham$

Search & Replace inside of Files

Try to memorize this command,

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}

{warning}More details should be added for this xargs command or if possible rewrite it in a way that is more clear.
{warning}

h2. Search & Replace inside of Files

Try to memorize this command,
{code:lang=html}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 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}

More details should be added for this xargs command or if possible rewrite it in a way that is more clear.

Disk Management

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

du -sk * | sort

Long Running Processes

To write.

Other Useful Commands

digest -a md5 -v /path/to/file
  • No labels