bash examples
#!/bin/sh
## delete files in current folder
find -mindepth 1 -delete
## delete spaces from filenames in current folder
for f in *\ *; do mv "$f" "${f// /_}"; done
## tr (1) - translate or delete characters
echo 'hello world' | tr -d 'l'
# heo word
echo 'hello world' | tr l x
# hexxo worxd
echo 'Hello World' | tr A-Z a-z
# hello world
# https://en.wikipedia.org/wiki/Caesar_cipher
echo 'hello world' | tr a-z x-za-x
# ebiil tloia
# https://en.wikipedia.org/wiki/ROT13
echo 'hello world' | tr a-z n-za-m
# uryyb jbeyq
echo 'hello world' | tr a-z n-za-m | tr a-z n-za-m
# hello world
## cut (1) - remove sections from each line of files
head /etc/passwd | cut -d':' -f1
# root
# daemon
# bin
# sys
# sync
# games
# man
# lp
# mail
# news
head /etc/passwd | cut -d':' -f1,7
# root:/bin/bash
# daemon:/usr/sbin/nologin
# bin:/usr/sbin/nologin
# sys:/usr/sbin/nologin
# sync:/bin/sync
# games:/usr/sbin/nologin
# man:/usr/sbin/nologin
# lp:/usr/sbin/nologin
# mail:/usr/sbin/nologin
# news:/usr/sbin/nologin
## column (1) - columnate lists
echo "permission #hlink owner group size month day hh:mm fname"; ls -l /bin | tail -n +2 | head
# permission #hlink owner group size month day hh:mm fname
# -rwxr-xr-x 1 root root 1037528 Jun 24 18:44 bash
# -rwxr-xr-x 1 root root 31288 May 20 2015 bunzip2
# -rwxr-xr-x 1 root root 1964536 Aug 19 2015 busybox
# -rwxr-xr-x 1 root root 31288 May 20 2015 bzcat
# lrwxrwxrwx 1 root root 6 Jul 19 14:14 bzcmp -> bzdiff
# -rwxr-xr-x 1 root root 2140 May 20 2015 bzdiff
# lrwxrwxrwx 1 root root 6 Jul 19 14:14 bzegrep -> bzgrep
# -rwxr-xr-x 1 root root 4877 May 20 2015 bzexe
# lrwxrwxrwx 1 root root 6 Jul 19 14:14 bzfgrep -> bzgrep
# -rwxr-xr-x 1 root root 3642 May 20 2015 bzgrep
(echo "permission #hlink owner group size month day hh:mm fname"; ls -l /bin | tail -n +2 | head) | column -t
# permission #hlink owner group size month day hh:mm fname
# -rwxr-xr-x 1 root root 1037528 Jun 24 18:44 bash
# -rwxr-xr-x 1 root root 31288 May 20 2015 bunzip2
# -rwxr-xr-x 1 root root 1964536 Aug 19 2015 busybox
# -rwxr-xr-x 1 root root 31288 May 20 2015 bzcat
# lrwxrwxrwx 1 root root 6 Jul 19 14:14 bzcmp -> bzdiff
# -rwxr-xr-x 1 root root 2140 May 20 2015 bzdiff
# lrwxrwxrwx 1 root root 6 Jul 19 14:14 bzegrep -> bzgrep
# -rwxr-xr-x 1 root root 4877 May 20 2015 bzexe
# lrwxrwxrwx 1 root root 6 Jul 19 14:14 bzfgrep -> bzgrep
# -rwxr-xr-x 1 root root 3642 May 20 2015 bzgrep
head /etc/passwd | column -t -s':'
# root x 0 0 root /root /bin/bash
# daemon x 1 1 daemon /usr/sbin /usr/sbin/nologin
# bin x 2 2 bin /bin /usr/sbin/nologin
# sys x 3 3 sys /dev /usr/sbin/nologin
# sync x 4 65534 sync /bin /bin/sync
# games x 5 60 games /usr/games /usr/sbin/nologin
# man x 6 12 man /var/cache/man /usr/sbin/nologin
# lp x 7 7 lp /var/spool/lpd /usr/sbin/nologin
# mail x 8 8 mail /var/mail /usr/sbin/nologin
# news x 9 9 news /var/spool/news /usr/sbin/nologin
## sort (1) - sort lines of text files
echo -e "aaa\ndd\ncccc\nb" | sort
# aaa
# b
# cccc
# dd
du -d1 /usr
# 370032 /usr/bin
# 292716 /usr/local
# 53736 /usr/include
# 5072780 /usr/share
# 15804 /usr/sbin
# 2674208 /usr/lib
# 20 /usr/locale
# 664 /usr/games
# 250832 /usr/src
# 8730796 /usr
du -d1 /usr | sort -n
# 20 /usr/locale
# 664 /usr/games
# 15804 /usr/sbin
# 53736 /usr/include
# 250832 /usr/src
# 292716 /usr/local
# 370032 /usr/bin
# 2674208 /usr/lib
# 5072780 /usr/share
# 8730796 /usr
du -d1 /usr -h
# 362M /usr/bin
# 286M /usr/local
# 53M /usr/include
# 4.9G /usr/share
# 16M /usr/sbin
# 2.6G /usr/lib
# 20K /usr/locale
# 664K /usr/games
# 245M /usr/src
# 8.4G /usr
du -d1 /usr -h | sort -h
# 20K /usr/locale
# 664K /usr/games
# 16M /usr/sbin
# 53M /usr/include
# 245M /usr/src
# 286M /usr/local
# 362M /usr/bin
# 2.6G /usr/lib
# 4.9G /usr/share
# 8.4G /usr
cat /etc/passwd | cut -d':' -f1,7 | column -t -s':' | sort -k2
# root /bin/bash
# murat /bin/bash
# _apt /bin/false
# avahi /bin/false
# ..
# speech-dispatcher /bin/false
# systemd-bus-proxy /bin/false
# sync /bin/sync
# lp /usr/sbin/nologin
# bin /usr/sbin/nologin
# ..
# nobody /usr/sbin/nologin
# www-data /usr/sbin/nologin
## uniq (1) - report or omit repeated lines
cat /etc/passwd | cut -d':' -f7 | sort | uniq
# /bin/bash
# /bin/false
# /bin/sync
# /usr/sbin/nologin
cat /etc/passwd | cut -d':' -f7 | sort | uniq -c
# 2 /bin/bash
# 22 /bin/false
# 1 /bin/sync
# 16 /usr/sbin/nologin
cat /etc/passwd | cut -d':' -f7 | sort | uniq -c | sort -rn
# 22 /bin/false
# 16 /usr/sbin/nologin
# 2 /bin/bash
# 1 /bin/sync
## wc (1) - print newline, word, and byte counts for each file
cat /etc/passwd | wc -l
# 41
cat /etc/passwd | wc -w
# 70
cat /etc/passwd | wc -c
# 2286
cat /etc/passwd | wc
# 41 70 2286
## shuf (1) - generate random permutations
cat /usr/share/dict/words | shuf | head
# crabbed
# raceway
# scare's
# altercations
# multiple
# eerier
# doubling
# donned
# Browne
# caucussed
## fmt (1) - simple optimal text formatter
shuf /usr/share/dict/words | fmt | head
# Collins kindergärtner Soyuz Cinderella pricklier laxness Domesday
# Darryl's lighted departments delphiniums envisioned grapevine's
# Noel's Casablanca's eyrie's seeker's Brendan exhorted Septembers
# flea dyke burgers hashing laminating mattock servicewoman approving
# Basho's crusade omits filthy pare performance's peccadillo certainly
# retributions hinterland tresses displace moonlights exposed mellows
# careened syncopating spotter's divas Ruby's measurement's waterfowls
# curvatures Reunion moistening playback hooped Man equators cable's
# thumbing camerawoman's Tenochtitlan's somersault's pizazz's Nestle
# reformation Senior overshot enlivened impugns Kroc kickoffs rocketry
shuf /usr/share/dict/words | fmt -w 40 | head
# eyesore's cagy Shorthorn outrider's
# capaciousness flaunt's unbelief
# Gaul's unselfishness fetishist's
# Serbian's UNICEF's eventually
# impenetrability bodkin eclectic's
# researches smokeless geriatrics dowdily
# Roth misidentify Congregationalist
# trails saying disciplines Congreve
# mystery's offends Astrakhan tinseling
# dearness disengages glinted leprous
## colrm (1) - remove columns from a file
shuf /usr/share/dict/words | fmt | head | colrm 40
# simile trappers jawbone hurdle's sorori
# Mongolia's hallway's misquotation measu
# Harrington nomination's guessers risk s
# belie Charlemagne's upside's mines enve
# Dannie tightwad fruitfulness's sensuali
# scrapbook's asters dicta coexistence Lu
# squeaked expressionists undies dingy ja
# Baldwin Ricky librettos sac lobbyist pa
# meddler teachable silversmiths percolat
# broadsword's advisers multiplier robs m
shuf /usr/share/dict/words | fmt | head | colrm 20 40
# busting Ahab ligatuions mammoths etymologists
# crams logging's toddu's vacillation's VLF's
# Prius bitterly sheln's italic interstice Rickey's
# Shavian's cylindersfhound's hoarders psychokinesis
# Pb devil's mumbled enlightens pleasantries wicker
# timidity gelding amath portraiture's motion's
# blowers Bolshevist erring song's cash diameter
# juiciness bearded G obeisances mob's inkwell's wok
# paddocked jiffies Crneyron's Napster tidiness's
# need's aureola leg nnelling Mohawk craftsmanship's
#!/bin/sh
# see also `man grep` and `man 7 regex`
## concatenation
echo 'abc' | grep 'a' # abc
echo 'abc' | grep 'aa' #
echo 'abc' | grep 'ab' # abc
echo 'abc' | grep 'ab.' # abc
## brackets
echo 'abc' | grep 'a[bd]c' # abc
echo 'abc' | grep 'a\[bd\]c' #
echo 'a[bd]c' | grep 'a[bd]c' #
echo 'a[bd]c' | grep 'a\[bd\]c' # a[bd]c
## extended regular expressions
echo 'abc' | grep -E 'a[bd]c' # abc
echo 'abc' | grep -E 'a\[bd\]c' #
echo 'a[bd]c' | grep -E 'a[bd]c' #
echo 'a[bd]c' | grep -E 'a\[bd\]c' # a[bd]c
## ranges and bracket expressions
echo 'abc' | grep -E 'a[bcdef]c' # abc
echo 'abc' | grep -E 'a[b-f]c' # abc
echo 'abc' | grep -E 'a[[:alpha:]]c' # abc
echo 'a1c' | grep -E 'a[12345]c' # a1c
echo 'a1c' | grep -E 'a[1-5]c' # a1c
echo 'a1c' | grep -E 'a[[:digit:]]c' # a1c
## negative matching
echo 'abc' | grep -E 'a[^b]c' #
echo 'abc' | grep -E 'a[^bcdef]c' #
echo 'abc' | grep -E 'a[^b-f]c' #
echo 'abc' | grep -E 'a[^[:alpha:]]c' #
## anchoring
echo 'abc' | grep -E '^ab' # abc
echo 'abc' | grep -E '^bc' #
echo 'abc' | grep -E 'ab$' #
echo 'abc' | grep -E 'bc$' # abc
## special expressions
echo 'bc' | grep -E '\sb' #
echo ' bc' | grep -E '\sb' # bc
echo ' C_function_4_testing ' | grep -E '\s\w\s' #
## ? operator
echo 'abc' | grep -E 'abcd' #
echo 'abc' | grep -E 'abcd?' # abc
echo 'abcd' | grep -E 'abcd?' # abcd
## * operator
echo 'abbbc' | grep -E 'abc' #
echo 'abbbc' | grep -E 'ab*c' # abbbc
echo 'ac' | grep -E 'ab*c' # ac
## + operator
echo 'abbbc' | grep -E 'abc' #
echo 'abbbc' | grep -E 'ab+c' # abbbc
echo 'ac' | grep -E 'ab+c' #
## braces
echo 'abbbc' | grep -E 'abc' #
echo 'abbbc' | grep -E 'ab{1,2}c' #
echo 'abbbc' | grep -E 'ab{1,5}c' # abbbc
## alternation
echo 'abc' | grep -E 'abc|def' # abc
echo 'def' | grep -E 'abc|def' # def
echo 'abcggg' | grep -E '(abc|def)ggg' # abcggg
echo 'defggg' | grep -E '(abc|def)ggg' # defggg
## backreferences (not necessarily linear time)
echo 'abcabc' | grep -E '(abc|def)\1' # abcabc
echo 'abcdef' | grep -E '(abc|def)\1' #
echo 'defabc' | grep -E '(abc|def)\1' #
echo 'defdef' | grep -E '(abc|def)\1' # defdef
## catastrophic backtracking
# grep (1) implements basic regular expressions using a DFA engine
#
# perl (1) implements the more powerful Perl Compatible Regular Expressions
# (PCRE) using backtracking
#
# https://swtch.com/~rsc/regexp/regexp1.html
#
# run the following examples in grep and perl and compare their performances
echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' | grep -E 'a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' | perl -ne 'print if /a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?a?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/'
## parameter expansion
foo="hello"
echo $foo # hello
echo $foobar # (prints nothing since there is no $foobar)
echo ${foo}bar # hellobar
echo '$foo' # $foo
echo "$foo" # hello
## command substitution
which ls # /bin/ls
ls -l /bin/ls # -rwxr-xr-x 1 root root 126584 Feb 18 2016 /bin/ls
echo `which ls` # /bin/ls
echo `ls -l \`which ls\`` # -rwxr-xr-x 1 root root 126584 Feb 18 2016 /bin/ls
echo $(which ls) # /bin/ls
echo $(ls -l $(which ls)) # -rwxr-xr-x 1 root root 126584 Feb 18 2016 /bin/ls
## arithmetic expansion
echo 2+2 # 2+2
echo $((2+2)) # 4
echo $((4/2)) # 2
echo $((5/2)) # 2
echo '5/2' | bc # 2
echo 'scale=10; 5/2' | bc # 2.5000000000
echo '5/2' | bc -l # 2.50000000000000000000
## path expansion
echo * # foo foo.c bar bar.c ..
echo *.c # foo.c bar.c ..
echo '*' # *
echo "*" # *
## brace expansion (bash)
echo foo.{c,h} # foo.c foo.h
echo foo{.c,} # foo.c foo
## conditionals (0 means true, non-0 means false)
# you can put this in your ~/.bashrc to display error codes
EC() { echo -e '\e[1;33m'code $?'\e[m\n'; }
trap EC ERR
type -a true
# true is a shell builtin
# true is /bin/true
type -a false
# false is a shell builtin
# false is /bin/false
true; echo $? # 0
false; echo $? # 1
true && echo 'is true' # is true
false && echo 'is false' #
true && echo 'is true' || echo 'is false' # is true
false && echo 'is true' || echo 'is false' # is false
## test (1) - check file types and compare values
type -a test
# test is a shell builtin
# test is /usr/bin/test
test -e /bin; echo $? # 0
test -e /bin/ls; echo $? # 0
test -f /bin; echo $? # 1
test -f /bin/ls; echo $? # 0
test -f /path/to/non/existent/file; echo $? # 1
test -d /bin; echo $? # 0
test -d /bin/ls; echo $? # 1
test -d /path/to/non/existent/dir; echo $? # 1
test -r /bin/ls; echo $? # 0
test -w /bin/ls; echo $? # 1
test -r /bin/ls -a -w /bin/ls; echo $? # 1
test -r /bin/ls -o -w /bin/ls; echo $? # 0
test ! -r /bin/ls; echo $? # 1
test -n $HOSTNAME; echo $? # 0
test -z $HOSTNAME; echo $? # 1
test $(uname -s) = "Linux"; echo $? # 0
test $(uname -m) != "x86_64"; echo $? # 1
## [ (1) - check file types and compare values
type -a '['
# [ is a shell builtin
# [ is /usr/bin/[
[ -e /bin/ls ]; echo $? # 0
[ -e /bin/asd ]; echo $? # 1
[-e /bin/ls ]; echo $? # /bin/bash: [-e: command not found
[ -e /bin/ls]; echo $? # /bin/bash: line 0: [: missing `]'
[ $(uname -m)= "x86_64" ]; echo $? # /bin/bash: line 0: [: x86_64=: unary operator expected
[ $(uname -m) ="x86_64" ]; echo $? # /bin/bash: line 0: [: x86_64: unary operator expected
## if/elif/else/fi
arch=$(uname -m)
if [ $arch = "x86_64" ]
then
echo 'system is 64 bit'
fi
# system is 64 bit
if [ $arch = "x86_64" ]; then
echo 'system is 64 bit'
fi
# system is 64 bit
if [ $arch = "x86_64" ]; then echo 'system is 64 bit'; fi
# system is 64 bit
if [ $arch = "x86_64" ]; then
echo 'system is 64 bit'
elif [ $arch = "i686" ]; then
echo 'system is 32 bit'
fi
# system is 64 bit
if [ $arch = "x86_64" ]; then
echo 'system is 64 bit'
elif [ $arch = "i686" ]; then
echo 'system is 32 bit'
else
echo 'system is unknown'
fi
# system is 64 bit
## while/until/done
i=0
while [ $i -lt 5 ]; do
sleep 1
i=$((i+1))
echo "$i second(s) have passed"
done
# 1 second(s) have passed
# 2 second(s) have passed
# 3 second(s) have passed
# 4 second(s) have passed
# 5 second(s) have passed
i=0
until [ $i -ge 5 ]; do
sleep 1
i=$((i+1))
echo "$i second(s) have passed"
done
# 1 second(s) have passed
# 2 second(s) have passed
# 3 second(s) have passed
# 4 second(s) have passed
# 5 second(s) have passed
## for/done
for fruit in apple orange banana; do
echo $fruit
done
# apple
# orange
# banana
for fruit in 'apple orange banana'; do
echo $fruit
done
# apple orange banana
for fruit in "apple orange banana"; do
echo $fruit
done
# apple orange banana
for fruit in 'red apple' 'orange' 'banana'; do
echo $fruit
done
# red apple
# orange
# banana
for f in *.c; do
echo $f
done
# cat.c
# echo.c
# lock.c
# loop.c
# can NOT handle whitespace in filenames
for f in $(ls ~); do
echo "$f"
done
# bin
# Desktop
# Documents
# Downloads
# Dropbox
# examples.desktop
# Music
# Pictures
# Public
# src
# Templates
# Videos
# VirtualBox
# VMs
# path name expansion automatically escapes whitespace
for f in $HOME/*; do
echo $(basename "$f")
done
# bin
# Desktop
# Documents
# Downloads
# Dropbox
# examples.desktop
# Music
# Pictures
# Public
# src
# Templates
# Videos
# VirtualBox VMs
seq 5
# 1
# 2
# 3
# 4
# 5
seq 2 5
# 2
# 3
# 4
# 5
seq 2 3 9
# 2
# 5
# 8
for i in $(seq 10); do
if [ $i -eq 3 ]; then
continue
elif [ $i -eq 5 ]; then
break
fi
echo $i
done
# 1
# 2
# 4
## case/esac
os=$(uname -s)
case $os in
("Linux")
echo "setting up for Linux"
;;
"Darwin")
echo "setting up for MacOSX"
;;
"NetBSD"|"FreeBSD")
echo "setting up for BSD"
;;
*)
echo "unsupported system"
;;
esac
# setting up for Linux
## functions
greet() {
echo hello $1
}
greet world
# hello world
extract() {
case "$1" in
*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf "$1";;
*.tar.gz|*.tgz) tar xzvf "$1";;
*.tar.xz|*.txz) tar xJvf "$1";;
*.zip) unzip "$1";;
*.rar) unrar x "$1";;
*.7z) 7z x "$1";;
esac
}
#!/bin/sh
## manuals and documentation
whatis ls
# ls (1) - list directory contents
whatis -w ls*
# ls (1) - list directory contents
# lsattr (1) - list file attributes on a Linux second extended file system
# lsb_release (1) - print distribution-specific information
# lsblk (8) - list block devices
# lscpu (1) - display information about the CPU architecture
# lsdiff (1) - show which files are modified by a patch
# lsearch (3) - linear search of an array
# lseek (2) - reposition read/write file offset
# lseek64 (3) - reposition 64-bit read/write file offset
# lshw (1) - list hardware
# lsinitramfs (8) - list content of an initramfs image
# lsipc (1) - show information on IPC facilities currently employed in the system
# lslocks (8) - list local system locks
# lslogins (1) - display information about known users in the system
# lsmod (8) - Show the status of modules in the Linux Kernel
# lsof (8) - list open files
# lspci (8) - list all PCI devices
# lspcmcia (8) - display extended PCMCIA debugging information
# lspgpot (1) - extracts the ownertrust values from PGP keyrings and list them in ...
# lstat (2) - get file status
# lstat64 (2) - get file status
# lsusb (8) - list USB devices
apropos -a list directory
# chacl (1) - change the access control list of a file or directory
# dir (1) - list directory contents
# File::Listing (3pm) - parse directory listing
# ls (1) - list directory contents
# ntfsls (8) - list directory contents on an NTFS filesystem
# vdir (1) - list directory contents
man ls
# (displays manual page)
manpath
# /usr/local/man:/usr/local/share/man:/usr/share/man
info ls
# (displays info page)
help cd
# (displays shell help for builtins)
type ls
# ls is /bin/ls
type cd
# cd is a shell builtin
## file displaying/editing
cat << EOF > hello.c
#include <stdio.h>
int main() {
puts("hello world");
return 0;
}
EOF
# (creates `hello.c` file with content up to `EOF` (heredoc literal))
cat hello.c
# #include <stdio.h>
#
# int main() {
# puts("hello world");
# return 0;
# }
file hello.c
# hello.c: C source, ASCII text
file -i hello.c
# text/x-c; charset=us-ascii
gcc hello.c -o hello
# (compiles `hello.c` source into `hello` binary)
./hello
# hello world
file hello
# hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=5f4ee315765406f6c704eb194a5f624d834de000, not stripped
file -i hello
# application/x-executable; charset=binary
head -n 3 hello.c
# #include <stdio.h>
#
# int main() {
tail -n 3 hello.c
# puts("hello world");
# return 0;
# }
tail -f /var/log/syslog
# (follows log file -- interrupt with Ctrl-c to quit)
more hello.c
# (displays file one page at a time)
less hello.c
# (displays file as a pager)
gedit hello.c
# (opens graphical text editor by GNOME)
nano hello.c
# (opens terminal text editor by GNU)
vi hello.c
# (opens terminal text editor -- POSIX standard)
vim hello.c
# (opens vim -- vi improved)
vimtutor
# (opens tutorial for vim)
## pipes and redirection
cat << EOF > streams.c
#include <stdio.h>
int main()
{
fputs("this text goes to STDOUT\n", stdout);
fputs("this text goes to STDERR\n", stderr);
return 0;
}
EOF
gcc streams.c -o streams
./streams
# this text goes to STDERR
# this text goes to STDOUT
./streams > /dev/null
# this text goes to STDERR
./streams 1> /dev/null
# this text goes to STDERR
./streams 2> /dev/null
# this text goes to STDOUT
./streams &> /dev/null
./streams > /dev/null 2>&1
# print 5th number in a file (you should better use sed (1))
cat streams.c | head -n5 | tail -n1
# fputs("this text goes to STDOUT\n", stdout);
# create a random string of specified length (you should better use mkpasswd (1))
cat /dev/urandom | strings -n 1 | tr -d '[:space:]' | head -c 30
# K@_Q+a)4mLI'~4#%N?))~B@^L!;i@j
# word frequency challenge (http://dl.acm.org/citation.cfm?id=5948.315654)
cat basics.sh | tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | head -n5
# 24 hello
# 24 c
# 17 list
# 16 file
# 13 text
./streams | tee output.txt
# this text goes to STDERR
# this text goes to STDOUT
cat output.txt
# this text goes to STDOUT
./streams 2>&1 | tee output.txt
# this text goes to STDERR
# this text goes to STDOUT
cat output.txt
# this text goes to STDERR
# this text goes to STDOUT
## aliases and functions
# create alias for when a parameter is at the end
alias freq="cat basics.sh | tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | head -n"
freq 5
# 24 hello
# 24 c
# 17 list
# 16 file
# 13 text
unalias freq
# create a function for when a parameter is at an arbitrary location
freq() { curl $1 2> /dev/null | tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | head -n5; }
freq http://www.gutenberg.org/cache/epub/1342/pg1342.txt
# 4507 the
# 4243 to
# 3730 of
# 3658 and
# 2225 her
freq http://boun.edu.tr/
# 314 a
# 244 tr
# 176 class
# 158 div
# 154 href
## searching
grep hello hello.c
# puts("hello world");
grep hello hello.c -v
# #include <stdio.h>
#
# int main() {
# return 0;
# }
grep hello -r .
# ./basics.sh:# create `hello.c` file with content up to `EOF` (heredoc literal)
# ./basics.sh:cat << EOF > hello.c
# ./basics.sh: puts("hello world");
# ./basics.sh:# print `hello.c` file stdout
# ./basics.sh:cat hello.c
# ./basics.sh:# puts("hello world");
# ./basics.sh:file hello.c
# ./basics.sh:# hello.c: C source, ASCII text
# ./basics.sh:file -i hello.c
# ./basics.sh:gcc hello.c -o hello
# ./basics.sh:./hello
# ./basics.sh:# hello world
# ./basics.sh:file hello
# ./basics.sh:# hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=5f4ee315765406f6c704eb194a5f624d834de000, not stripped
# ./basics.sh:file -i hello
# ./basics.sh:head -n 3 hello.c
# ./basics.sh:tail -n 3 hello.c
# ./basics.sh:# puts("hello world");
# ./basics.sh:more hello.c
# ./basics.sh:less hello.c
# ./basics.sh:gedit hello.c
# ./basics.sh:nano hello.c
# ./basics.sh:vi hello.c
# ./basics.sh:vim hello.c
# ./basics.sh:# 24 hello
# ./basics.sh:# 24 hello
# ./basics.sh:# ./hello
# ./basics.sh:# ./hello.c
# ./basics.sh:# ./hello.c
# ./basics.sh:# ./hello
# ./basics.sh:# ./hello
# Binary file ./hello matches
# ./hello.c: puts("hello world");
which ls
# /bin/ls
which vim
# /usr/local/bin/vim
which -a vim
# /usr/local/bin/vim
# /usr/bin/vim
find .
# .
# ./streams.c
# ./basics.sh
# ./hello
# ./hello.c
# ./streams
find . -name '*.c'
# ./streams.c
# ./hello.c
find . -executable
# .
# ./basics.sh
# ./hello
# ./streams
find . -executable -type f
# ./basics.sh
# ./hello
# ./streams
# find the longest header files under `/usr/include`
find /usr/include -name '*.h' -type f -exec wc -l {} \; | sort -rn | head
# 18690 /usr/include/epoxy/gl_generated.h
# 11864 /usr/include/xcb/xproto.h
# 8424 /usr/include/xcb/glx.h
# 7126 /usr/include/valgrind/valgrind.h
# 6761 /usr/include/google/protobuf/descriptor.pb.h
# 5904 /usr/include/c++/5/bits/random.h
# 5587 /usr/include/c++/5/bits/basic_string.h
# 5546 /usr/include/c++/5/bits/stl_algo.h
# 4833 /usr/include/llvm-3.8/llvm/IR/Instructions.h
# 4698 /usr/include/valgrind/vki/vki-linux.h
ln -s basics.sh foo
test -e foo && echo 'file exists' || echo 'file does not exists'
# file exists
ln -s /path/to/non/existent/file bar
test -e bar && echo 'file exists' || echo 'file does not exists'
# file does not exists
find . -type l
# ./bar
# ./foo
find . -type l -print
# ./bar
# ./foo
find . -type l -print -print
# ./bar
# ./bar
# ./foo
# ./foo
# find existing links in current directory
find . -type l -exec test -e {} \; -print
# ./foo
# find broken links in current directory
find . -type l -! -exec test -e {} \; -print
# ./bar
locate stdio.h
# /usr/include/stdio.h
# /usr/include/c++/5/tr1/stdio.h
# /usr/include/glib-2.0/glib/gstdio.h
# /usr/include/x86_64-linux-gnu/bits/stdio.h
# /usr/lib/x86_64-linux-gnu/perl/5.22.1/CORE/nostdio.h
whereis gcc
# gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/man/man1/gcc.1.gz
## packages (debian)
# search a package
apt search wget
apt-cache search wget
# show package info
apt show wget
apt-cache show wget
# install a package
sudo apt install wget
sudo apt-get install wget
# remove a package
sudo apt remove wget
sudo apt-get remove wget
# update list of packages
sudo apt update
sudo apt-get update
# upgrade all installed packages
sudo apt upgrade
sudo apt-get upgrade
# list installed files of a package
dpkg -L tree
# /.
# /usr
# /usr/bin
# /usr/bin/tree
# /usr/share
# /usr/share/doc
# /usr/share/doc/tree
# /usr/share/doc/tree/TODO
# /usr/share/doc/tree/copyright
# /usr/share/doc/tree/README.gz
# /usr/share/doc/tree/changelog.Debian.gz
# /usr/share/man
# /usr/share/man/man1
# /usr/share/man/man1/tree.1.gz
# find the owner package of a file
dpkg-query -S /bin/ls
# coreutils: /bin/ls
## archives
# create a tar file from c files
tar cf files.tar *.c
# list contents of a tar file
tar tf files.tar
# hello.c
# streams.c
# uncompress a tar file
tar xf files.tar
# create a gunzipped tar file from c files
tar czf files.tar.gz *.c
# uncompress a gunzipped tar file
tar xf files.tar
## processes
cat << EOF > loop.c
#include <stdio.h>
#include <unistd.h>
int main()
{
int curr = 0;
while (1) {
printf("running for %d seconds\n", curr++);
sleep(1);
}
return 0;
}
EOF
gcc loop.c -o loop
# use Ctrl-C to quit
./loop
# running for 0 seconds
# running for 1 seconds
# running for 2 seconds
# running for 3 seconds
# ^C
# use Ctrl-Z to stop
./loop
# running for 0 seconds
# running for 1 seconds
# running for 2 seconds
# running for 3 seconds
# ^Z
# [1]+ Stopped ./loop
jobs
# [1]+ Stopped ./loop
fg
# ./loop
# running for 4 seconds
# running for 5 seconds
# running for 6 seconds
# ^Z
# [1]+ Stopped ./loop
bg
# [1]+ ./loop &
# murat@ext:~/Dropbox/cmpe230/basics$ running for 7 seconds
# running for 8 seconds
# running for 9 seconds
# running for 10 seconds
disown
# (abondons the last job)
disown -h
# (abondons the last job with nohup)
disown -r
# (abondons running jobs)
disown -a
# (abondons all jobs)
jobs
# (lists jobs)
ps
# (shows all processes in the current terminal)
ps -a
# (shows all processes attached to a terminal)
ps -e
# (shows all processes)
cat << EOF > lock.c
#include <stdio.h>
int main()
{
int curr = 0;
while (1) {
++curr;
}
return 0;
}
EOF
gcc lock.c -o lock
top
# (shows resource usage of all processes)
top -u murat
# (shows resource usage of all processes owned by murat)
pgrep lock
# (lists all processes with the name loop)
kill 1234
# (kills process with id 1234)
kill -9 1234
# (force kills process with id 1234)
pkill lock
# (kills all processes with the name lock)
pkill -9 lock
# (force kills all processes with the name lock)
killall lock
# (kills all processes with the exact name lock)
killall -9 lock
# (force kills all processes with the exact name lock)