One-liners I find to be useful

From Phospher
Jump to: navigation, search

Find and replace

This will search for the "OLD" variable in "FILE" and replace is with the "NEW" variable.

    awk '{sub("OLD","NEW");print}' FILE

Prefix every line

This will prefix every line in "FILE" with "PREFIX"

    sed -e 's/.*/PREFIX &/' FILE

Append every line

This will append every line in "FILE" with "APPEND"

    sed -e 's/$/ APPEND/' FILE

Grep for whitespace

This will search FILE for whitespace

    grep "^[[:space:]]*[[:space:]]*$" FILE

Find and chmod ONLY directories to 775

    find DIRECTORY -type d -exec chmod 775 {} \;

Find and chmod ONLY files to 664

    find DIRECTORY -type f -exec chmod 664 {} \;

Creating an ISO file

    mkisofs -o my.iso /source/directory

Delete the last character of every line

    sed -e 's/\(.*\)./\1/'


Delete the first character of every line

    sed -e 's/\(\.*\)./\1/'

Look for more than one line in a single grep

    egrep -i 'foo|man|chew' FILE


if then else and or
Basic "if then else and or" file tests

-d	Directory
-e	Exists (also -a)
-f	Regular file
-h	Symbolic link (also -L)
-p	Named pipe
-r	Readable by you
-s	Not empty
-S	Socket
-w	Writable by you
-N	Has been modified since last being read


In addition to the unary tests above, you can compare two files with the binary operators. Testing pairs of files Operator

-nt	Test if file1 is newer than file 2. The modification date is used for this and the next comparison.
-ot	Test if file1 is older than file 2.
-ef	Test if file1 is a hard link to file2.