Twitter (not set)
Blog (not set)
Website (not set)

0

Alert me by email when a disconnected or unreachable server comes back online

 $ while ! ping -c1 the_host_down; do sleep 1; done && date | mail -s 'the host is back!' me@example.com

— by janos on Oct. 10, 2011, 8:26 p.m.

Explanation

  • ping -c1 host sends just one ping and exits with success or error.
  • while will keep running until the ping succeeds
  • When the ping succeeds the while loop will end, and an email will be sent with the date as the message

Limitations

  • Depending on your system the parameter for sending a single ping might be different from -c1
  • Depending on your system the mail command might be different or work differently

0

Add timestamp to the output of ping

 $ ping some_host | while read LINE; do echo $(date): $LINE; done

— by janos on Oct. 10, 2011, 8:13 p.m.

Explanation

The while loop reads the output of ping line by line, and echoes it back with $(date) prepended.

0

Test a one-liner with echo commands first, pipe to bash when ready

 $ paste <(ls) <(ls | tr A-Z a-z) | while read OLD NEW; do echo mv -v $OLD $NEW; done | sh

— by janos on Oct. 8, 2011, 1 p.m.

Explanation

You can do a lot of damage with a single one-liner. So while testing the sequence and syntax of commands it's good to just echo the commands that you will want to execute in the end, rather than really executing them. If the output looks good, don't bother removing the echo-s (which can be error prone), it's easier and safer to just pipe the echoed commands to bash to execute.

0

Find the most recently modified files in a directory and all subdirectories

 $ find /path/to/dir -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f, "\n" }' | tail

— by janos on Oct. 4, 2011, 10:25 p.m.

Explanation

  • find path_to_dir -type f prints all the files in the directory tree
  • chomp(@files = <>); reads all the lines into an array
  • stat($a) is an array of interesting info about a file. Index 7 is size, 8 is access time, 9 is modification time, etc. (See man perlfunc for details and search for stat EXPR.)
  • sort { (stat($a))[9] <=> (stat($b))[9] } @files sorts the files by modification time
  • print scalar localtime((stat($f))[9]), "\t", $f, "\n" - prints the modification time formatted nicely, followed by a tab and the filename

0

Delete unversioned files in a Subversion checkout directory and all subdirectories

 $ svn st | grep ^? | sed -e 's/^? *//' | xargs -i{} echo rm -fr "{}"

— by janos on Oct. 3, 2011, 8:22 p.m.

Explanation

If there are no spaces in the file names, a simpler command will be enough. This one-liner works even if there are spaces and certain special characters in the file names.

  • svn st shows the changes in the Subversion checkout
  • | grep ^? matches only lines starting with question mark (= unversioned files)
  • | sed -e 's/^? *//' removes the question mark at the beginning of the line and the space characters following it
  • | xargs -i{} echo rm -fr "{}" executes an echo command for each line in the input, where the command is formed by inserting the input line in the {} placeholder. Confirm the result looks good and remove the echo to perform the rm.

Limitations

The command will not work with files that have " (double quote) in the name...