man
command
Path | Explanation |
---|---|
alpha | a file in your current directory |
beta/gamma | a file in the beta directory, which is in your current directory |
/delta/epsilon | a file in the delta directory, which is inside the root directory. |
. | the current directory |
.. | the parent directory, one level up |
/ | the root directory |
~ | your home directory (only known to the shell, not in a program) |
~zeta | User zeta’s home directory (only known to the shell, not in a program) |
$ cd ~/tmp $ pwd /s/bach/a/class/ct320/tmp $ ls backup-2019 final.csv IQ04.csv IQ07.csv IQ11.csv IQ15.csv TLD-save ch IQ01.csv IQ04-old.csv IQ08.csv IQ12.csv Music.ico vars enrolled IQ02.csv IQ05.csv IQ09.csv IQ13.csv r7 z FINAL IQ03.csv IQ06.csv IQ10.csv IQ14.csv st $ ls ~/bin checkin curve imv p scores wikidiff checkin-checker demo-script l peek stats wikiedit checkin-file-checker domoss ll playpen tools wikigrep checkin_prog e lsf pwget u wikiupdate chit grade moss ruler unold wikiwhence cls grade-busy new run untar code grade-file-checker note runner vman cronedit grades old save wikicat $ mkdir zulu $ cd zulu $ pwd /s/bach/a/class/ct320/tmp/zulu $ cd $ pwd /s/bach/a/class/ct320 $ rmdir tmp/zulu
cd
path — change current directory
ls
path — list current (specified) directory
pwd
— show current directory
mkdir
path — create a new directory
rmdir
path — remove a (empty) directory
cat
, less
, more
file — display file contents
vi
, gedit
file — edit file contents
cp
path path — copy file or directory
mv
path path — move (rename) file or directory
rm
path — remove file or directory
diff
path path — compare file or directory
$ date >now $ ls -l now -rw------- 1 ct320 class 29 Feb 2 11:46 now $ chmod -w now $ ls -l now -r-------- 1 ct320 class 29 Feb 2 11:46 now $ echo "hi" >now /tmp/pmwiki-bash-script4qr6LL: line 18: now: Permission denied
chmod
permissions path — change permissions
chown
owner path — change (user) ownership
chgrp
group path — change (group) ownership
ps
— display running processes
kill
— terminate running processes
bg
, fg
, nice
— process control commands
shutdown
time — system shutdown
reboot
— system reboot
date
— system date and time
time
command — measures command timing
whereis
command — find command instances
dmesg
— system boot messages
>
to create/overwrite with stdout, >>
to append, <
for stdin
$ echo "hello" >foo $ echo "Zeta Eta Theta" >foo $ date >>foo $ cat foo Zeta Eta Theta Sun Feb 2 11:46:02 MST 2025
The word “foo” is used as a general-purpose placeholder in Computer Science.
It’s much the same as how a mathematicians use x as a parameter:
No mathematician would ask “What does x mean? Why x? Why isn’t it Ω or q?” We all understand that x is merely a placeholder—it’s just a name.
Similarly, in Computer Science, foo is just a placeholder. It has no particular meaning—it’s just a name. CS people also often use bar and, together, Foobar.
$ echo "hello" >foo $ echo "Zeta Eta Theta" >foo $ date >>foo $ cat foo Zeta Eta Theta Sun Feb 2 11:46:02 MST 2025 $ cat foo | sort Sun Feb 2 11:46:02 MST 2025 Zeta Eta Theta $ sort <foo Sun Feb 2 11:46:02 MST 2025 Zeta Eta Theta $ sort foo Sun Feb 2 11:46:02 MST 2025 Zeta Eta Theta
ls | less
find . -name temp.dat | grep -v zulu
set -o vi
or set -o emacs
history
— shows previous commands
sort
— sort input lines to output lines
-b
: ignore white space
-f
: case insenstive sorting
-n
: compare fields as numbers
-r
: reverse sort order
-k
: specify sorting column
tee
— copy input to two places
head
, tail
— print beginning or head of file
grep
"regular-expression" — search for text
find
directory conditions — look for files
$ cd ~ct320/pub $ cat ducks Huey (red) Dewey (blue) Louie (green) $ sort ducks Dewey (blue) Huey (red) Louie (green) $ cat ducks Huey (red) Dewey (blue) Louie (green)
sort
does not change the input file.
$ cd ~ct320/pub $ sort ducks >foo $ cat ducks Huey (red) Dewey (blue) Louie (green) $ cat foo Dewey (blue) Huey (red) Louie (green) $ rm foo
foo
Sort in reverse order:
$ cd ~ct320/pub $ sort -r ducks Louie (green) Huey (red) Dewey (blue)
Sort by the second field:
$ cd ~ct320/pub $ sort -k2 ducks Dewey (blue) Louie (green) Huey (red)
tee
exampleThe tee
program, like a pipe fitting, sends the output to two
places—standard output, and the given file.
$ sort ~ct320/pub/ducks | tee foo Dewey (blue) Huey (red) Louie (green) $ echo "I wonder what’s in the file foo?" I wonder what’s in the file foo? $ cat foo Dewey (blue) Huey (red) Louie (green)
head
, tail
examples$ cat ~ct320/pub/dwarfs Bashful Doc Dopey Grumpy Happy Sleepy Sneezy $ head -3 ~ct320/pub/dwarfs Bashful Doc Dopey $ tail -3 ~ct320/pub/dwarfs Happy Sleepy Sneezy
grep
examplesWhat’s the name of Jack’s computer?
$ grep -i Applin ~info/machines
Oh, that’s right. What’s its IP address?
$ grep greybull /etc/hosts
find
examplesFind all the files in a given directory hierarchy whose names contain “bash”:
$ cd $ find public_html/CurrentSemester/ -iname '*bash*' | sort public_html/CurrentSemester/pmwiki/wiki.d/Lab.BashI public_html/CurrentSemester/pmwiki/wiki.d/Lab.BashII public_html/CurrentSemester/pmwiki/wiki.d/Lecture.BashInteractive public_html/CurrentSemester/pmwiki/wiki.d/Lecture.BashScripts public_html/CurrentSemester/pub/HW5-solution-bash.html public_html/CurrentSemester/pub/HW6-solution-bash.html
Find at most five lectures modified within a month:
$ cd ~/public_html/CurrentSemester $ find -name 'Lecture.*' -mtime -30 -exec ls -dhog {} + | head -5
Linux has a list of words:
$ grep -i applin /usr/share/dict/words Appling appling dappling grappling intergrappling ungrappling $ tail /usr/share/dict/words Zythia zythum Zyzomys Zyzzogeton zyzzyva zyzzyvas ZZ Zz zZt ZZZ
Let’s get just the first three letters (prefixes):
$ cut -c1-3 /usr/share/dict/words | tail Zyt zyt Zyz Zyz zyz zyz ZZ Zz zZt ZZZ
They’re probably already in order, but let’s make sure:
$ cut -c1-3 /usr/share/dict/words | sort | tail zyt Zyt zyz zyz Zyz Zyz Zz ZZ zZt ZZZ
Now, count the prefixes:
$ cut -c1-3 /usr/share/dict/words | sort | uniq -c | tail 3 Zyr 1 Zys 2 zyt 1 Zyt 2 zyz 2 Zyz 1 Zz 1 ZZ 1 zZt 1 ZZZ
Which the prefixes are the most common?
$ cut -c1-3 /usr/share/dict/words | sort | uniq -c | sort -n | tail 2628 und 2719 ant 2814 sub 2842 int 3044 dis 3210 con 3523 pro 4070 ove 5497 pre 7804 non