Command Shell Snippets
Sort in alphabetical order:
$ sort myfile.txt
Sort in numerical order:
$ sort -n myfile.txt
Sort on multiple column (on column 2 in numerical order, then on column 1 in alphabetical order):
$ sort myfile.txt -k2n -k1
Sort a comma-separated table:
$ sort -k2 -k3 k1 -t ',' myfile.txt
Sort in reversed order:
$ sort -r myfile.txt
Sort a pip-separated file in reverses order by the second column:
$ sort myfile.txt -nrk 2 -st '|'
Search text files for lines matching regular expressions (regex) with grep <matching string> <source>
:
$ grep ArticleTitle webpage.html
An asterisk (*
) indicates any character (the matching expression starts with Ar
and ends with le
):
$ grep Ar*le mytext.txt
Search for starting metacharacter >
:
$ grep ^'>' mytext.txt
List the access rights for all files:
$ ls -lag
Run commands in background, kill the job running in the foreground, suspend the job running in the foreground, and background the suspended job:
$ command &
$ ^C
$ ^Z
$ bg
List the current jobs, foreground job number 1, and kill job number 1:
$ jobs
$ fg%1
$ kill%1
List current processes, and kill process number 26152:
$ ps
$ kill 26152
Help about commands:
$ man <command name>
$ whatis <command name>
Find and delete
# 1 find
find . -name "*.bak" -type f
find /dir/here...
# 2 delete
find . -name "*.bak" -type f -delete
find . -name "*.bak" -print0 | xargs -0 rm -rf
Rename many files
# 's/_13/_15/' replaces _13 by _15 in all files starting with 'cc' in the directory
rename 's/_13/_15/' cc*
# ... finishing by .jpg in the directory
rename 's/_13/_15/' *.jpg