Shell Command

Shell Command

echo

Just like print in Python

1
$ echo 'Hello, World!'

1. cd: Change Directory

1
$ cd downloads

To parent directory ..

1
$ cd ..

2. ls: list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cd Pictures; ls
# chage directory to 'Pictures', and list it.

$ ls Pictures
# list 'Pictures' Directory

$ ls Pictures/../Pictures

$ ls .
# list current directory

$ ls

$ ls ~
# list home directory

2.2 ls Other command

1
2
3
4
5
6
7
8
$ ls -l
# Long information about files

$ ls -l Documents/*.pdf
# Long information for pdf files in Documents folder

$ ls Sub*
# Show all the files start with 'Sub'(Case Sensitive)

3. pwd print working directory

1
2
$ pwd
/Users/yukizhong/Dropbox/02_Coding/Local_Blog (Path)

4. Directory names

  1. ls ..: Parent directory
  2. ls .: Current Directory
  3. ls ~: Home Directory

5. mkdir make directory, mv move

EX: Move epub files to ‘Books’ Directory

1
2
$ mkdir Documents/Books
$ mv Documents/*.epub Documents/Books

curl: C URL —> See URL

1. Download

to download https://tinyurl.com/zeyq9vc and save it as the file dictionary.txt

1
$ curl -o dictionary.txt -L 'https://tinyurl.com/zeyq9vc'
  1. -o download as
  2. -L redirect

cat: Catenate (连接) /concatenate; less

1. Display Text Files

1
$ cat dictionary.txt

Multiple files also works

1
$ cat mytext.txt mytext2.txt

2. Create a new file

1
2
3
4
5
$ cat > filename.txt

#....(file Content)
# 键入回车
# 然后 Ctrl + D 退出

3. 合并/复制文件

1
2
3
$ cat file1 file2 > newFile3

$ cat file4 > newFile5

4. Append text file

1
$ cat file1.txt >> To_be_appended_File.txt

rm: remove rmdir: remove directory

$ rm filenme —> 直接删除,可以批量删除

$ rm -i filename —> 删前询问,批量的话每个文件问一遍 (i: interactive)

EX:

  1. Good File
  2. Bad Name Good File
  3. Bad File
  4. Good Name Bad Face

You want to remove files 3 and 4, while leaving 1 and 2 intact.

1
2
3
$ rm 'Bad File' 'Good Name Bad Face'

$ rm *'Bad F'*

rmdir (When the directory is not empty)

1
$ rm -rf filename

mkdir Create new directory

1
2
3
4
5
$ mkdir newDir

$ mkdir -p newDirPath new Dir

$ mkdir -p udacity-git-course/new-git-project && cd $_

rename

In Mac, there is no rename command by default.

So, firstly use homebrew to install rename

1
2
$ brew install rename
$ rename [switches|transforms] [files]

grep Search txt file, wc: word count

  1. $ grep ibo dictionary.txt

OUTPUT: all the words containing ‘ibo’ in the file

  1. $ grep ibo dictionary.txt | less

Pip to less, show only one page

  1. $ grep -c ibo dictionary.txt

OUTPUT: Count how many words containing ‘ibo’

  1. $ curl -L https://tinyurl.com | grep fish | wc -l

wc: Count how many words

-l: How many lines

Shell and environment variables

$Variable distinguish it

1. Establish variable

1
2
$ num='1,2,3'
$ echo $num

2. Shell Variable

Shell Variable is all about fixed for shell

1
$ echo $LINES x $COLUMNS

OUTPUT: 12 x 80

3. Environment variable

Change in diff environments

1
2
3
4
5
6
7
$ echo $PATH
#.....

$ PATH=$PATH:/new/dir/here
# add new directories in the path

$ echo $PWD

Aliases

  1. You can use Aliases just as common commands
  2. Aliases only work in this terminal window.
  3. If you hope to keep alias work for all terminal, keep it in ‘.bash_profile’
1
2
3
4
5
6
7
# Give alias to a command
$ alias ll='ls -la'
# Run 'ls -la'
$ ll

# Show all alias
$ alias

find mdfind

1. find

在当前文件夹下搜索

1
$ find filename

2. mdfind

类似Spotlight 强大版本

搜索包含keyword的文件

1
$ mdfind keyword

搜索名字为filename的

1
$ mdfind -name filename