Cet index est un remix des fiches de cours de Florian Cramer (Command Line Cheat Sheet 12/10/2006) et de Femke Snelting, complèté par Alexia de Visscher dans le cadre du cours de Pratiques Numériques à l'erg.
Last update: 16/10/2018
= pipe; pass along the standard output of the previous command to the standard input of the next command. For example:
ps aux | grep apache
= execute one command after the other:
ls ; date
= logical “AND”: execute next command only if the previous completed without an error message:
mkdir pictures && cp *.jpg pictures
= logical “OR”: execute next command only if the previous completed with an error message:
cat gkrlmpfst.txt || echo this file doesn't exist
= redirect standard output into a new file:
ls > filelist.txt
= redirect standard error into a new file:
cat gkrlmpfst.txt 2> error-message.txt
= redirect standard error and standard output into a new file:
cat filelist.txt gkrlmpfst.txt &> filelist_plus_error_message.txt
= append standard output to the end of a file:
echo hello >> filelist_plus_error_message.txt
= append standard error to a file:
cat gkrlmpfst.txt 2>> filelist_plus_error_message.txt
= append both standard output and standard error to a file:
cat filelist.txt gkrlmpfst.txt &>> filelist_plus_error_message.txt
= execute command inside the backticks and return its output to standard output:
cat `grep -l hello *.txt` > search_result.txt
= alternative syntax for ”, can be nested:
cat $(grep -l hi $(grep -l hello *.txt)) > search_result.txt
(dumps the content of all text files that both contain “hi” and “hello” in the same line)
Wildcards [in shell operations]
= match one arbitrary character: ls h?llo.txt (will list hello.txt, hallo.txt, h9llo.txt etc.)
= match any number of arbitrary characters: cat * > allfilesin_one cp * /mnt/usbstick cp *.html /mnt/usbstick ls .htm
= match all specified symbols:
cp [abc]* /mnt/usbstick
(copies all files whose names begin with “a”, “b” or “c”)
cp [0-9]* /mnt/usbstick
(copies all files whose names begin with a number)
[in parameters of grep and sed, built into many programming languages such as Perl, PHP, Ruby…]
= match at beginning of a line:
grep "^a" xy.txt
(outputs all lines starting with the letter “a”)
grep "^<a href" xy.html
(outputs all lines starting with an HTML link)
= match at the end of a line:
grep "a$" xy.txt
(outputs all lines ending with the letter “a”)
= match any symbol
grep "h.llo" xy.txt
(outputs all lines containing “hallo”, “hbllo”, “hcllo”, “h1llo”, “h%llo” and so on)
= match an arbitrary number of any symbol
grep "h.*llo" xy.txt
(outputs all lines containing “hllo”, “hallo”, “h&1xllo” and so on)
grep "Piet.*Zwart.*students" xy.txt
(outputs all lines containing “Piet”, “Zwart” and “students”, regardless which other characters are in between)
= match specified symbols, just like in a wildcard:
grep "s[ei]ze" xy.txt
(outputs all lines containing “seze” or “size”)
grep "s[e-i]ze" xy.txt
(outputs all lines containing “seze”, “sfze”, “sgze”, “shze”, “size”)
= match an arbitary number of the specified symbols:
grep "s[ei]*ze" xy.txt
(outputs all lines containing “sze”, “seze”, “size”, “seize”, “sieze”, “seeeeze”, “seieiieize” and so on.)
= abbreviation for your home directory (i.e. /home/myname respectively $HOME)
= auto-completes almost everything, especially in the zsh shell: file and directory names, variable names, remote servers and even files (when you use ssh/scp), command names, command arguments, titles of man pages…
= repeats the argument of the previous command:
mkdir /mnt/usbstick/myfiles
(!$ is equivalent to /mnt/usbstick/myfiles)
= retype previous command (can be repeated)
= search engine for your last typed commands, press repeatedly to cycle through all commands matching your search pattern (standard in bash, not in the standard configuration of zsh)
[built-in shell command!] = give a different name to a command. (Take care not to unintentionally overwrite an existing alias with new alias definition!)
alias
(list all currently assigned aliases)
alias copy=cp
alias cpr="cp -R"
alias iamlucky =`find /bin -type f | head -n $(echo $RANDOM | cut -b 2-3) | tail -n 1`
(assigns random command)
= dump all specified files to standard output:
cat *.txt | less
(read all texts in the current directory)
cat a.txt b.txt > ab.txt
(combine two texts into one)
cat * | strings | less
(turn all your data into a pointless reading experience)
[built-in shell command!] = change to specified directory:
cd ~/public_html
[part of ImageMagick, needs to be installed]
= command line image processor:
convert -swirl 45 picture.jpg swirl.jpg
convert -fill red -annotate 0,0,10,100 "`date`" picture.jpg picture-stamped.jpg
(timestamp an image)
convert -fill red -annotate 0,0,10,100 "`fortune`" picture.jpg picture-stamped.jpg
(write the system’s fortune cookie message into the image)
= copy file
cp a.txt /mnt/usbstick
cp a.txt b.txt
cp -R ~/public_html .
[see also: mv]
= cut away all text from every line except the specified range of characters:
cat a.txt | cut -b "10-20"
(create pattern poetry from Unix file permission strings)
= dump current date and time
echo it is `date`
ls -l | grep `date +%H`:
(list all files that were created some day at the current hour)
cp mypaper.txt mypaper-`date +%d-%m-%y`.txt
(back up mypaper.txt into a file automatically named after today’s date, such as: mypaper-20-11-06.txt)
= disk usage, amount of disk space taken by a file or directory:
du -m picture.jpg
(-h for “megabytes”)
du -ms public_html
(-s for “summary”, i.e. total amount of disk space taken)
find ~/ -name "*.jpg" -exec du {} \; | sort -rg | less
(find all JPEG images in your files and sort them by size, with the largest on top)
find ~/ -name "*.jpg" -exec du {} \; | sort -rg | head
(top 10 of your largest JPEG files)
[built-in shell command!]
= write argument to standard output:
echo hello | wc -c
(count number of characters of “hello”)
echo hello | speak
echo hello | festival --tts
(let the computer say hello)
= text-to-speech programs/commands for Linux respectively Mac OS X. festival needs to be installed with “emerge festival”
[needs to be installed separately]
= universal video file/video stream converter and processor:
ffmpeg -i mymovie.mpg -an -y -ss 0:0:10 -t 0:0:0.001 -f imcat xy.html | tr "\n" " " | sed -e "s/.*href[^"]*"\([^"]*).*/\1/g"age2 mymovie.jpg
(by Michael, takes a snapshot of the movie at 10 seconds, saves as mymovie.jpg)
= find a file by going through the entire directory tree:
find .
(lists all files starting from the current directory)
find . -type d
(lists all directories)
find . -type f
(lists all files, not directories or symbolic links)
find . -type f -name "*.htm*"
(find all HTML files starting from the current directory)
find . -type f -maxdepth 1 -name "*.htm*"
(find all HTML files, but only the current directory)
find . -type f -size +100k -name "*.jpg"
(find all jpeg files that are larger than 100 Kilobyte)
find ~/ -mtime -1
(find all files, directories and symlinks in your home directory that were modified in the last 24 hours)
find ~/ -mtime -1 -type f exec -cp {} /mnt/usbstick \;
(find all files that were modified in the last 24 hours and copy them into the default directory of your USB stick)
find ~/ -type f -name "*.txt" -exec cat {} \; >> ~/all_my_texts.txt \;
(write the contents of all text files on your hard disk into one big file)
find ~/ -type f -name "*.txt" -exec cat {} \; | wc -w
(find out how many words are stored on your computer)
= filter the standard input or a file for all lines matching a regular expression:
grep "d[io]ng" *.txt
(retrouver les mots ding ou dong dans un texte)
ps aux | grep apache
[see whether apache is running; will also output the grep command itself.]
ps aux | grep [a]pache
[hack to exclude the grep command from the output] [see also: sed]
= output only the first few lines (default: 10) of the standard input:
ls -l | head
ls -lt | head
[list the last ten files you created or modified]
alias recent="ls -lt | head"
[turn the above into a command] [see also: tail]
[Built-in shell command!]
= output the last commands you typed in the shell:
history
= browser for reading and scrolling the standard output
ls -l | less
= create a link (same as “alias” in Mac OS and “shortcut” in Windows) to a file or directory:
ln -s ~/public_html/projects/mysite/pictures ~/webpics
ln -s ~/ /mnt/usbstick/
(Create links to all files in your home directory on your USB stick)
= list files:
ls
(same as “ls *”)
ls *.html
ls -l *.html
(file listing with details)
ls -1
(single-column file listing)
ls -a
(also show hidden files)
= show the manual page of a command, particularly for looking up options/switches of a command:
man cp
man ls
(by default, uses “less” for the actual display of the text)
= create a directory (a.k.a. “folder”):
mkdir public_html
mkdir public_html/projects
(create new directory within the previously created directory)
mkdir -p public_html/weblog/statistics/pics/misc
(create a cascade of new directories if necessary)
mkdir `fortune -s -n 30`
(create multiple directories from the single words of the system fortune cookie message)
= move file: mv ~/*.txt /mnt/usbstick (moves all text files from the home directory to the USB stick)
mv xy.txt abc.txt
(renames xy.txt to abc.txt)
mv xy.txt `fortune -s -n 30 | sed -e "s/[^a-z]//g"`
(Give your file a surprising new name)
[see also: cp, rm]
= simple text program for editing text files:
nano xy.txt
nano /etc/resolv.conf
vi/vim and Emacs are much more powerful and convenient for heavy typers, but also harder to learn
= prefix all lines of the standard input with line numbers
ls -l | nl
(Creates a numbered file listing)
ls -1 *jpg | nl | sed -e "s/^[ ]*\([0-9]*\)[ \t]*\(.*\)/mv \2 \1.jpg/" | sh
(Rename all JPEG files in a directory by numbers, i.e. into 1.jpg, 2.jpg etc. - see explanations of “sed” and “sh”)
[built-in shell command!]
= print working directory, i.e. shows you where you currently are in the file system:
echo just hanging out in the `pwd` directory
echo just hanging out in the `pwd` directory at `date +%H:m` o'clock
= remove a file
rm xy.txt
rm -r directory
(Classic, not recommended:) rm -rf / [see also: mv]
= non-interactive, command-triggered text editor for standard input / standard output streams; typically just used for regular expression search-and-replace:
cat xy.txt | sed -e "s/today/yesterday/g"
ls -1 | sed -e "s/\..*$//"
(list all files without suffixes)
cat xy.html | sed -e "s/<[^>]*>//g" > xy.txt
(remove all HTML tags; simple HTML-to-plain text converter]
cat xy.html | grep -i href | sed -e "s/.*href[^\"]*\"\([^\"]*\)\".*/\1/gi"
(extract HTML links)
wget -O - http://www.xy.com | sed -e "s/<[^>]*>//g" | less
(low-tech web browser)
function google () {wget -O - 'http://www.google.com/search?q='${(j:+:)* | sed -e "s/<[^>]*>//g | less}}
(low tech google search in the shell)
function google () {lynx 'http://www.google.com/search?q='${(j:+:)*}}
(by Cal: this makes more sense - using the lynx terminal browser instead of the sed/less hack) [see also: grep]
= start the shell, i.e. as a new shell within the shell: sh (results in a new shell)
sh xy.txt
(execute xy.txt as a shell script)
ls -1 *.png | sed -e "s/\(.*\)\.png/convert \1.png \1.jpg/" | sh
(convert PNG into JPEG image files by generating the command syntax with ls and sed and piping it into a shell)
=shuf is a command-line for creating a standard output consisting of random permutations of the input:
ls | shuf
(Shuffles input )
ls | shuf -n1
(Picks one random line from input)
= secure shell, start a remote shell on a different machine over an encrypted network connection:
ssh myname@pzwart2.wdka.hro.nl
(opens a remote shell on the pzwart2 server)
ssh myname@192.168.1.2
(opens a remote shell on another computer in your home network; “myname@” can be left out if you work under identical user names on both machines)
echo hello | ssh 192.168.1.2 "festival --tts"
(let your remote computer say “hello”)
echo hello | ssh 192.168.1.2 "cat - >> ~/messages.txt"
(append the line “hello” to the file messages.txt on the remote computer)
tar -cf - -C /home/myname . | ssh myname@192.168.1.2 "tar xpf - -C /home/myname/my-files"
(copy the entire contents of one directly over ssh using tar to
preserve all file attributes)
= sort lines of the standard input, alphabetically by default: cat xy.txt | sort
ls -1 | sort -r
(reverse sort file listing) [see also: uniq]
= output only ASCII characters of the standard input:
cat microsoft_word.doc | strings
cat picture.jpg | strings
cat /dev/urandom | strings | less
(random text generator)
= output only the last few (default: 10) lines of the standard input:
tail xy.txt
tail /var/log/messages
(check the latest system messages / kernel hickups)
ls -l | sort -r | tail
[see also: head]
= write the standard input simultaneously into a file and onto the standard output:
find . -type f | tee all_files.txt | grep "\.htm[l]*$" > html_files.txt
= quickly translate / replace characters from the standard input
cat xy.txt | tr e x
(x-out all “e"s, cheaply imitating George Perec’s novel without an e, "La Disparition”)
cat xy.txt | tr A-Z a-z
(replace all uppercase with lower case letters) [see also: sed]
= filter out two identical lines following each other from the standard input:
history | cut -b 8- | uniq
(removes all commands typed twice)
cat xy.txt | sort | uniq
(sorts all double lines in the alphabetically sorted text)
cat *.txt | tr " " "\n" | sort | uniq > dictionary.txt
(distill a dictionary from all text files in the current directory) [see also: sort]
= line, word and character count of either a file or the standard input
wc xy.txt
(outputs word statistics)
ps aux | wc -l
(number of processes currently running on the system)
find / -type f | wc -l
(total number of files on your system)
echo $(echo "$(ps aux | grep $(whoami) | wc -l) * 100 / $(ps aux | wc -l)" | bc)% of the processors were started by me
(find out the rate of the processes started by me)
= download a file from the World Wide Web or from an FTP server:
wget http://pzwart.wdka.hro.nl
wget -O - http://pzwart.wdka.hro.nl/mdma | sed -e "s/media/food/g"
(create an automatic parody of our course homepage)
[Built-in shell command!] = output location / full file system path of a command/program:
which cp
ls -l `which cp`
(extended listing of the “cp” program file without the need of manually browsing its location)
= output the name of the current user:
whoami
echo I am `whoami`
echo I am `whoami` on $HOSTNAME | speak
echo I am `whoami` on $HOSTNAME | festival --tts
ps aux | grep `whoami`
(lists all processes / programs you are currently running)
cat text.txt | tr " " "\n" | sort | uniq | shuf | tr "\n" " " | cut -c-100
Recrée un texte aléatoirement sur les 100 premières lettres
cat text.txt | tr " " "\n" | sort | uniq | shuf | tr "\n" " " | head
Recrée un texte aléatoirement sur les 10 premières lignes