COMMAND-LINE

File Test Operators

Testing files in scripts is easy and straight forward. This is where shell scripting starts to show its glory! In Bash you can do file testing for permissions, size, date, filetype or existence.

FlagDescription
-eFile exists
-aFile exists (identical to -e but is deprecated and outdated)
-fFile is a regular file (not a directory or device file)
-sfile is not zero size
-dfile is a directory
-bfile is a block device
-cfile is a character device
-pfile is a pipe
-hfile is a symbolic link
-Lfile is a symbolic link
-Sfile is a socket
-tfile (descriptor) is associated with a terminal device; this test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script is a terminal
-rfile has read permission (for the user running the test)
-wfile has write permission (for the user running the test)
-xfile has execute permission (for the user running the test)
-gset-group-id (sgid) flag set on file or directory
-uset-user-id (suid) flag set on file.
-ksticky bit set.
-Oyou are owner of file
-Ggroup-id of file same as yours
-Nfile modified since it was last read
f1 -nt f2file f1 is newer than f2
f1 -ot f2file f1 is older than f2
f1 -ef f2files f1 and f2 are hard links to the same file
!Not – reverses the sense of the tests above (returns true if condition absent).

Integer Comparison Operators

How to compare integers or arithmetic expressions in shell scripts.

FlagDescription
-eqis equal to
-neis not equal to
-gtis greater than
-geis greater than or equal to
-ltis less than
-leis less than or equal to
<is less than (within double parentheses, i.e. (( )))
<=is less than or equal to (same rule as before)
>is greater than (same rule as before)
>=is greater than or equal to (same rule as before)

Single Comparison Operators

How to compare strings in Bash.

FlagDescription
=is equal to
==is equal to (synonymous with the above =)
!=is not equal to
<is less than ASCII alphabetical order
>is greater than ASCII alphabetical order
-zstring is null (i.e. zero length)
-nstring is not null (i.e. !zero length)

wget

The below wget command will download all HTML pages for a given website and all of the local assets (CSS/JS/etc) needed to correctly display the pages.

wget --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-file-names=unix http://example.com --no-check-certificate

grep

Searches all files in a directory

grep -rnw '/path/to/somewhere/' -e 'pattern'

Search only files with .c or .h extensions

grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

Exclude files with a .o extension

grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"

Exclude dirs dir1/, dir2/ and all of them matching *.dst/:

grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

find

delete all directories older than 10 days

find /path/to/base/dir/* -type d -ctime +10 -exec rm -rf {} \;

replace all spaces in filenames with an underscore

for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done

replace all strings in filename with other string

find . -name '*pdf' -exec bash -c ' mv "$0" "${0/string-to-find/string-to-replace}"' {} \;

swap filenames in a dir from FIRST LAST to LAST, FIRST

find * -maxdepth 1 -type d -print0 | sed -ze "p;s/^\(.*\) \([^ ]*\)/\2, \1/" | xargs -0 -n2 mv

sed

replace all instances of text in a file

sed -i 's/SEARCH_REGEX/REPLACEMENT/g' INPUTFILE

tar

basic options

examples

To list the contents of a .tar file before you extract it, enter:

tar –tzf documents.tar.gz

To instruct tar to put the extracted unzipped files into a specific directory, enter:

tar –xvzf documents.tar.gz –C /home/user/destination

To extract the files from a .tar file, enter:

tar –xvf documents.tar

for loop

Rename Files and Directories Add Prefix

for f in * ; do mv -- "$f" "PRE_$f" ; done

converting all mp4 to mp3 w/ ffmpeg

#!/bin/bash
MP4FILE=$(ls ~/Music/ |grep .mp4)
for filename in $MP4FILE
do 
 name=`echo "$filename" | sed -e "s/.mp4$//g"`
 ffmpeg -i ~/Music/$filename -b:a 192K -vn ~/Music/$name.mp3
done

ocrmypdf

pdfjoin

References