Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.
Code Block
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 Inside of Files

Try to memorize this command,

Code Block
langhtml
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.

Search & Replace Inside of Files

Try to memorize this command,

Code Block
langhtml
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}
Warning

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.

Code Block
du -sk * | sort

Long Running Processes

Warning

To write.

Other Useful Commands

...

langhtml

Table of Contents

Detailed Articles

Page Tree
root@self

Useful One Off Recipes

Disk Management

List directories from largest to smallest at the top level only. On older system h will not work and you must use k.

Code Block
languagebash
du -sh * | sort

Long Running Processes

Look for long running processes,

Warning

To write.

Information

Last

Last searches back through the file /var/log/wtmp  (or the file designated by the -f flag) and displays a list of all users logged in (and out) since that file was created. 

Lastb

Lastb is the same as last, except that by default it shows a log of the file /var/log/btmp, which contains all the bad login attempts.

Quck String Manipulation

Quick reference of manipulating standout,

Code Block
languagebash
echo "some_file_name.txt" | tr "_" " " # result will be "some file name.txt"
paste oldName.txt newName.txt > runMe.txt # connects line by line to contents of both files together

 

Integrity Checking

MD5

Check if a file is corrupt,

Code Block
languagebash
# On Ubuntu
md5sum /path/to/file

# On Solaris
digest -a md5 -v /path/to/file