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 ls
NAME
ls - list directory contents
SYNOPSIS
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 man
navigation (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/test
test1 test.txt
$ ls #show contents of current directory
test1 test.txt
$ ls . # '.' means current directory
test1 test.txt
$ ls -la # '-' leads arguments. 'l' means show complete list. 'a' means show all content.
total 20
drwxr-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 test1
test1.txt
cd - 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/test
file operations:
mkdir - make directory(s)
examples:
xxxxxxxxxx
$ cd /home/hwen5/test
$ mkdir test2 # make one directory
$ ls -l
total 12
drwxr-xr-x 2 hwen5 users 4096 Sep 27 02:19 test1
drwxr-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 -l
total 4
drwxr-xr-x 2 hwen5 users 4096 Oct 9 11:08 test3_grandchild
cp
examples:
xxxxxxxxxx
$ cd ~/test
$ cp test.txt test_copy.txt # make a copy of 'test.txt' named 'test_copy.txt'
$ ls
test1 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
$ ls
test1 test2 test2_copy test3 test_copy.txt test.txt
$ cp -r test1 test3 # if test3 exists, then copy 'test1' into 'test3'
$ ls test3
test1 test3_child
mv - move/rename a file or directory
examples:
xxxxxxxxxx
$ mv test1 test1_renamed # rename 'test1' into 'test1_renamed'
$ ls
test1_renamed test2 test2_copy test3 test_copy.txt test.txt
$ mv test_copy.txt test1_renamed # move 'test_copy.txt' into 'test1_renamed'
$ ls
test1_renamed test2 test2_copy test3 test.txt
$ ls test1_renamed
test1.txt test_copy.txt
rm - remove files/directories
examples:
xxxxxxxxxx
$ ls test1_renamed
test1.txt test_copy.txt
$ rm test1_renamed/test_copy.txt
$ ls test1_renamed
test1.txt
$ ls
test1_renamed test2 test2_copy test3 test.txt
$ rm -rf test3 # 'r' means recursively remove a directory, 'f' means go without any warnings
$ ls
test1_renamed test2 test2_copy test.txt
cat - concatenate files and output the result
examples:
xxxxxxxxxx
$ cat test.txt
testing.
testing again.
$ cat test.txt test1_renamed/test1.txt
testing.
testing again.
test1
head / tail - show the content in the front / at the end of files
examples
xxxxxxxxxx
$ cat test.txt
testing.
testing again.
$ head test.txt
testing.
$ tail test.txt
testing again.
$ head -c 4 test.txt # show the first 4 bytes of test.txt
test
grep - search for content/pattern in files and directories
examples
xxxxxxxxxx
$ grep again test.txt # show the line(s) containing 'again' in test.txt
testing 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:test1
archive 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
$ ls
test1_renamed test2 test2_copy test4.txt test5.txt test.txt
$ mkdir new_dir
$ mv *.txt new_dir # move all txt files to new_dir
$ ls
new_dir test1_renamed test2 test2_copy
$ ls new_dir
test4.txt test5.txt test.txt
$ rm -rf test* # remove all file/directories beginning with 'test'
$ ls
new_dir
Composing commands:
| - piping the output of one command into the input of another.
examples:
xxxxxxxxxx
$ $ cat text.txt
line 1
line 2
line 3
comment 1
comment 2
comment 3
$ cat text.txt | grep comment # find all lines that has pattern 'comment' (note: the spaces count!)
comment 1
comment 2
comment 3
$ cat text.txt | head -n 2 # return the first 2 lines of text.txt
line 1
line 2
$ cat text.txt | head -n 2 | tail -n 1 # return the second line of text.txt
line 2
> / >> - putting the output into a file.
examples (you can see the difference of '>' and '>>' in the following example):
xxxxxxxxxx
$ echo output1
output1
$ echo output1 > output
$ cat output
output1
$ echo output2 > output
$ cat output
output2
$ echo output3 >> output
$ cat output
output2
output3
Please 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