
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Shell Scripting Cheat Sheet for Unix and Linux: File Redirection, Common Constructs, Misc Useful Commands and Tools, and so on.
Typology: Cheat Sheet
1 / 1
This page cannot be seen from the preview
Don't miss anything!
file create (overwrite) file ${V:-default} $V, or “default” if unset
file append to file # do something ${V:=default} $V (set to “default” if unset) < file read from file ${V:?err} $V, or “err” if unset a | b Pipe 'a' as input to 'b'
$ while read f read text file greater than cmd1 && cmd
do line by line equal to
done < file note: “$” prompt greater or equal becomes “>” le less or equal ls a* list files beginning with “a” find lines in ls *a list files ending with “a” File Tests list oldest first, newest last containing the newer than list smallest first, biggest last d is a directory ls -a list all files, including hidden f is a file x executable directory, preserving Steve Parker delimited by colon r readable links, special devices, etc. $ cmd1 || cmd2 run cmd1; if fails, writeable run cmd $ cmd1 && cmd2 run cmd1; if it String Tests $SHELL what shell am I running? works, run cmd2 = equal to $RANDOM provides random numbers act upon the z zero length $$ PID of current process a) value of a n not zero length $? variable $!
b) Logical Tests
*) is required || logical OR determine file type at the end of! logical NOT strip directory name (ls) ;; each section get directory name (/bin)
get output of $0 program name show routers ls into variable $1 show open ports function $2 Year, Month, Day declaration … … date +%H%M Hours, Minutes } and syntax $# no. of arguments count number of lines for calling it $* all arguments present working directory
find. -size 10k -print files over 10Kb “bar” in file find text files of each line less file display file page by page March 1973 show disk mounts simple maths trace system calls for PID echo "scale = 5 ; \ better maths create tar archive (5.00097) log in to host as user touch file create blank file alias ll='ls -l' alias for ls -l unset existing alias return to previous directory
if [ “$x” -lt “$y” ]; then fi lt gt run cmd1; if ok, run cmd eq ne ge mv /src /dest move /src into /dest $ grep foo myfile afoo myfile ls -ltr foo nt ls -lSr foobar text “foo” find /src -print \ copy /src into current $ cut -d: -f5 /etc/passwd get 5th^ field | cpio -pudvm w
case $foo in return code from last cmd echo “foo is A” ;; PID of last background cmd echo “foo is B” ;; file /etc/hosts echo “foo is not A or B” basename /bin/ls dirname /bin/ls esac ifconfig -a myvar=ls
netstat -r 1 st^ argument netstat -a doubleit() { 2 nd^ argument date +%Y%m%d expr $1 * 2 wc -l doubleit 3 # returns 6 pwd egrep “(foo|bar)” file find “foo” or find. -name “*.txt” -print awk '{ print $5 }' file print the 5th^ word find /foo -type d -ls list all directories under /foo cal 3 1973 sed s/foo/bar/g file replace “foo” with “bar” df -h sed -i s/foo/bar/g file in file (-i: update file) three=expr 1 + 2
strace -tfp PID tar cvf archive.tar file1 file 2 file 5121 / 1024" | bc ssh user@host time cmd stopwatch on cmd scp file.txt user@host: copy file.txt to host as user scp user@host:/tmp/file.txt /var/tmp copy /tmp/file.txt from user at host to /var/tmp locally unalias ls cd -