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.
| Flag | Description |
|---|---|
-e | File exists |
-a | File exists (identical to -e but is deprecated and outdated) |
-f | File is a regular file (not a directory or device file) |
-s | file is not zero size |
-d | file is a directory |
-b | file is a block device |
-c | file is a character device |
-p | file is a pipe |
-h | file is a symbolic link |
-L | file is a symbolic link |
-S | file is a socket |
-t | file (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 |
-r | file has read permission (for the user running the test) |
-w | file has write permission (for the user running the test) |
-x | file has execute permission (for the user running the test) |
-g | set-group-id (sgid) flag set on file or directory |
-u | set-user-id (suid) flag set on file. |
-k | sticky bit set. |
-O | you are owner of file |
-G | group-id of file same as yours |
-N | file modified since it was last read |
f1 -nt f2 | file f1 is newer than f2 |
f1 -ot f2 | file f1 is older than f2 |
f1 -ef f2 | files 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.
| Flag | Description |
|---|---|
-eq | is equal to |
-ne | is not equal to |
-gt | is greater than |
-ge | is greater than or equal to |
-lt | is less than |
-le | is 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.
| Flag | Description |
|---|---|
= | 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 |
-z | string is null (i.e. zero length) |
-n | string 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'
-ror-Ris recursive,-nis line number, and-wstands for match the whole word.-l(lower-case L) can be added to just give the file name of matching files.-istands for ignore case-eregex pattern to match
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 {} \;
find: the unix command for finding files / directories / links etc./path/to/base/dir: the directory to start your search in.-type d: only find directories-ctime +10: only consider the ones with modification time older than 10 days-exec ... \;: for each such result found, do the following command in...rm -rf {}: recursively force remove the directory; the{}part is where the find result gets substituted into from the previous part.
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
-i- By default, sed writes its output to the standard output. This option tells sed to edit files in place. If an extension is supplied (ex -i.bak), a backup of the original file is created.s- The substitute command, probably the most used command in sed./ / /- Delimiter character. It can be any character but usually the slash (/) character is used.SEARCH_REGEX- Normal string or a regular expression to search for.REPLACEMENT- The replacement string.g- Global replacement flag. By default, sed reads the file line by line and changes only the first occurrence of the SEARCH_REGEX on a line. When the replacement flag is provided, all occurrences are replaced.INPUTFILE- The name of the file on which you want to run the command.
tar
basic options
xinstructs tar to extract the files from the zipped filevmeans verbose, or to list out the files it’s extractingzinstructs tar to decompress the files – without this, you’d have a folder full of compressed filesftells tar the filename you want it to work on\
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