Here is a brief list of bash commands, together with other command-line tools.
Please get familiar with them -- they can be super useful in linux/unix(mac OS). I'm not going into the details, or it'll can be a book of hundreds of pages.
I use '$' as the prompt (the existing string command lines before you type), so lines beginning with '$' are inputs, others are outputs. Don't type the leading '$''s in your commands.
I use '#' to start comments. Parts beginning with '#' is not a parts of commands.
In usage's, I use <something> as mandatory fields, [something_else] as optional fields.
Commands:
manual:
man - learn how to use a command/tool
usage:
xxxxxxxxxx$ man <command>examples:
xxxxxxxxxx$ man lsNAME ls - list directory contentsSYNOPSIS ls [OPTION]... [FILE]...DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabeti‐ cally if none of -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with ....to learn how to use man:
xxxxxxxxxx$ man mannavigation (examples in this section are continuous):
pwd - where am I
example:
xxxxxxxxxx$ pwd/home/hwen5/test #The directory starts with '/', like this one, which means it's a directory from <strong>root.</strong>ls - list directory contents
examples:
xxxxxxxxxx$ ls /home/hwen5/testtest1 test.txt$ ls #show contents of current directorytest1 test.txt$ ls . # '.' means current directorytest1 test.txt$ ls -la # '-' leads arguments. 'l' means show complete list. 'a' means show all content.total 20drwxr-xr-x 3 hwen5 users 4096 Oct 9 10:52 .drwxr-xr-x 10 hwen5 users 4096 Sep 29 12:49 .. # '..' means parent directory.-rw-r--r-- 1 hwen5 users 3 Oct 9 10:52 .I_am_invisible # files beginning with '.' are hidden.drwxr-xr-x 2 hwen5 users 4096 Sep 27 02:19 test1 # the beginning 'd' means it's a directory.-rw-r--r-- 1 hwen5 users 9 Sep 17 13:29 test.txt$ ls test1 # show what's inside test1test1.txtcd - change current directory
examples:
xxxxxxxxxx$ cd test1$ pwd/home/hwen5/test/test1$ cd .. # go to parent directory$ pwd/home/hwen5/test$ cd ~ # go to ~ (which is $HOME) directory, the directory you're in when logged in.$ pwd/home/hwen5$ cd ~/test$ pwd/home/hwen5/testfile operations:
mkdir - make directory(s)
examples:
xxxxxxxxxx$ cd /home/hwen5/test$ mkdir test2 # make one directory$ ls -ltotal 12drwxr-xr-x 2 hwen5 users 4096 Sep 27 02:19 test1drwxr-xr-x 2 hwen5 users 4096 Oct 9 11:05 test2-rw-r--r-- 1 hwen5 users 9 Sep 17 13:29 test.txt$ mkdir -p test3/test3_child/test3_grandchild # '-p' means: if parent directories don't exist, create them.$ cd test3/test3_child$ ls -ltotal 4drwxr-xr-x 2 hwen5 users 4096 Oct 9 11:08 test3_grandchildcp
examples:
xxxxxxxxxx$ cd ~/test$ cp test.txt test_copy.txt # make a copy of 'test.txt' named 'test_copy.txt'$ lstest1 test2 test3 test_copy.txt test.txt$ cp -r test2 test2_copy # if 'test2_copy' doesn't exist, create a copy of 'test2' named 'test2_copy' # '-r' means recursively copy a directory and its children$ lstest1 test2 test2_copy test3 test_copy.txt test.txt$ cp -r test1 test3 # if test3 exists, then copy 'test1' into 'test3'$ ls test3test1 test3_childmv - move/rename a file or directory
examples:
xxxxxxxxxx$ mv test1 test1_renamed # rename 'test1' into 'test1_renamed'$ lstest1_renamed test2 test2_copy test3 test_copy.txt test.txt$ mv test_copy.txt test1_renamed # move 'test_copy.txt' into 'test1_renamed'$ lstest1_renamed test2 test2_copy test3 test.txt$ ls test1_renamedtest1.txt test_copy.txtrm - remove files/directories
examples:
xxxxxxxxxx$ ls test1_renamedtest1.txt test_copy.txt$ rm test1_renamed/test_copy.txt$ ls test1_renamedtest1.txt$ lstest1_renamed test2 test2_copy test3 test.txt$ rm -rf test3 # 'r' means recursively remove a directory, 'f' means go without any warnings$ lstest1_renamed test2 test2_copy test.txtcat - concatenate files and output the result
examples:
xxxxxxxxxx$ cat test.txttesting.testing again.$ cat test.txt test1_renamed/test1.txttesting.testing again.test1head / tail - show the content in the front / at the end of files
examples
xxxxxxxxxx$ cat test.txttesting.testing again.$ head test.txttesting.$ tail test.txttesting again.$ head -c 4 test.txt # show the first 4 bytes of test.txttestgrep - search for content/pattern in files and directories
examples
xxxxxxxxxx$ grep again test.txt # show the line(s) containing 'again' in test.txttesting again.$ grep -r test . # search for line(s) containing 'test' in all files under current directory, recursively../test.txt:testing../test.txt:testing again../test1_renamed/test1.txt:test1archive and compressing:
tar - make/decompress tar (compressed) archives
examples:
xxxxxxxxxx$ tar -cvf test2.tar test2 test.txt # make tar file 'test2.tar' that contains directory test2 and test.txt. 'c' means compress.$ tar -xvf test2.tar # decompress 'test2.tar'.Wildcard - simple pattern matching
examples
xxxxxxxxxx$ lstest1_renamed test2 test2_copy test4.txt test5.txt test.txt$ mkdir new_dir $ mv *.txt new_dir # move all txt files to new_dir$ lsnew_dir test1_renamed test2 test2_copy$ ls new_dirtest4.txt test5.txt test.txt$ rm -rf test* # remove all file/directories beginning with 'test'$ lsnew_dirComposing commands:
| - piping the output of one command into the input of another.
examples:
xxxxxxxxxx$ $ cat text.txtline 1line 2line 3comment 1comment 2comment 3$ cat text.txt | grep comment # find all lines that has pattern 'comment' (note: the spaces count!)comment 1comment 2comment 3$ cat text.txt | head -n 2 # return the first 2 lines of text.txtline 1line 2$ cat text.txt | head -n 2 | tail -n 1 # return the second line of text.txtline 2> / >> - putting the output into a file.
examples (you can see the difference of '>' and '>>' in the following example):
xxxxxxxxxx$ echo output1output1$ echo output1 > output$ cat outputoutput1$ echo output2 > output$ cat outputoutput2$ echo output3 >> output$ cat outputoutput2output3Please come to my office hour or email me with any questions. ps. If you want to do something fancier with shell, google it! (or check up the man page!)
Have fun!
Hs