Shell Scripting Cheat Sheet For Unix And Linux

ADVERTISEMENT

v1.3
Sept
2012
Shell Scripting Cheat Sheet
Online:
for Unix and Linux
Book:
File Redirection
Test Operators
Variable Substitution
> file
create (overwrite) file
if [ “$x” -lt “$y” ]; then
${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
fi
${V:?err}
$V, or “err” if unset
a | b
Pipe 'a' as input to 'b'
Conditional Execution
Numeric Tests
Common Constructs
lt
less than
cmd1 || cmd2
run cmd1; if fails, run cmd2
$ while read f
read text file
gt
greater than
cmd1 && cmd2
run cmd1; if ok, run cmd2
> do
line by line
eq
equal to
Files
> echo “Line is $f”
ne
not equal
> done < file
note: “$” prompt
ge
greater or equal
mv /src /dest
move /src into /dest
becomes “>”
le
less or equal
ls a*
list files beginning with “a”
$ grep foo myfile
find lines in
ls *a
list files ending with “a”
afoo
myfile
File Tests
ls -ltr
list oldest first, newest last
foo
containing the
nt
newer than
ls -lSr
list smallest first, biggest last
foobar
text “foo”
d
is a directory
ls -a
list all files, including hidden
f
is a file
find /src -print \
copy /src into current
$ cut -d: -f5 /etc/passwd
get 5
th
field
x
executable
| cpio -pudvm
directory, preserving
Steve Parker
delimited by colon
r
readable
links, special devices, etc.
$ cmd1 || cmd2
run cmd1; if fails,
w
writeable
Preset Variables
run cmd2
$ cmd1 && cmd2
run cmd1; if it
String Tests
$SHELL
what shell am I running?
works, run cmd2
=
equal to
$RANDOM
provides random numbers
case $foo in
act upon the
z
zero length
$$
PID of current process
a)
value of a
n
not zero length
$?
return code from last cmd
echo “foo is A” ;;
variable
$!
PID of last background cmd
b)
Logical Tests
Generally Useful Commands
echo “foo is B” ;;
note that “;;”
&&
logical AND
*)
is required
||
logical OR
file /etc/hosts
determine file type
echo “foo is not A or B”
at the end of
!
logical NOT
basename /bin/ls
strip directory name (ls)
;;
each section
dirname /bin/ls
get directory name (/bin)
Arguments
esac
ifconfig -a
show all network adapters
myvar=`ls`
get output of
$0
program name
netstat -r
show routers
st
ls into variable
$1
1
argument
netstat -a
show open ports
doubleit() {
function
$2
2
nd
argument
date +%Y%m%d
Year, Month, Day
expr $1 \* 2
declaration
date +%H%M
Hours, Minutes
}
and syntax
$#
no. of arguments
wc -l
count number of lines
doubleit 3
# returns 6
for calling it
$*
all arguments
pwd
present working directory
Misc Useful Commands and Tools
egrep “(foo|bar)” file
find “foo” or
find . -size 10k -print
files over 10Kb
“bar” in file
find . -name “*.txt” -print
find text files
th
awk '{ print $5 }' file
print the 5
word
find /foo -type d -ls
list all directories under /foo
of each line
less file
display file page by page
cal 3 1973
March 1973
sed s/foo/bar/g file
replace “foo” with “bar”
df -h
show disk mounts
sed -i s/foo/bar/g file
in file (-i: update file)
three=`expr 1 + 2`
simple maths
strace -tfp PID
trace system calls for PID
echo "scale = 5 ; \
better maths
tar cvf archive.tar file1 file 2 file3
create tar archive
5121 / 1024" | bc
(5.00097)
ssh
user@host
log in to host as user
time cmd
stopwatch on cmd
scp file.txt user@host:
copy file.txt to host as user
touch file
create blank file
scp user@host:/tmp/file.txt /var/tmp
copy /tmp/file.txt from user
alias ll='ls -l'
alias for ls -l
at host to /var/tmp locally
unalias ls
unset existing alias
cd -
return to previous directory

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go