The Shell

We use computer a lot.

But when it comes to the basic coding or skill, we may not know too much of the knowledge. There is a very good online course, The Missing Semester of Your CS Education. The motivation of the course is: "Classes teach you all about advanced topics within CS, from operating systems to machine learning, but there’s one critical subject that’s rarely covered, and is instead left to students to figure out on their own: proficiency with their tools. We’ll teach you how to master the command-line, use a powerful text editor, use fancy features of version control systems, and much more!"

Based on that, I have a better understanding of the command-line and the shell.

Always ask for help when you don't know about the usage of one command.

  • Option 1: man command_name
  • Option 2: search the command_name on tldr. (tldr may short for "Too Long; Didn't Read") The tldr pages are a community effort to simplify the beloved man pages with practical examples.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
sudo
# Executes a single command as the superuser or another user.

ls
# List directory contents.

ls -la
# Long format list (permissions, ownership, size and modification date) of all files.

ls -ls
# Long format list sorted by size (descending).
  • About the directory:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
pwd
# Print the current directory.

mkdir
# Creates a directory.

cd
# Go to home directory of current user.

cd {{path/to/directory}}
# Go to the given directory.

cd ..
# Go up to the parent of the current directory.
  • About the file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
mv {{source}} {{target}}
# Move files in arbitrary locations.

cp {{path/to/file.ext}} {{path/to/copy.ext}}
# Copy a file to another location.

cat {{file}}
# Print the contents of a file to the standard output.

chmod
# Change the access permissions of a file or directory.

chmod +x {{file}}
# Give the right to e[x]ecute it

chmod 775 {{file}}
chmod 777 {{file}}
# [owner permissions(r, w, x), group permissions(r, w, x), others permissions (r, w, x)]
# r stand for read .its value is 4.
# w stand for the write.its value is 2.
# x stand for the execute.its value is 1.

touch
# Change a file access and modification times (atime, mtime).

touch -t {{YYYYMMDDHHMM.SS}} {{filename}}
# Set the times on a file to a specific date and time:

  • Open / Editing a file
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
less {{source_file}}
# Open a file.
# To quit less and go back to the command line press q.

nano {{path/to/file}}
# Simple, easy to use command-line text editor.

vim {{path/to/file}}
# Vim (Vi IMproved), a command-line text editor, provides several modes for different kinds of text manipulation. Pressing i enters edit mode. <Esc> goes back to normal mode,

  • others
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# Pipes
# The | operator lets you “chain” programs

ls -l | tail -n1
# List the last line content.

tee
# Read from standard input and write to standard output and files (or commands).

ls -la | tee test.log
# Write the directory info to test.log

echo hello > hello.txt
# print hello to file hello.txt

grep
# Matches patterns in input text.

./semester | grep -i last-modified > ~/Desktop/last-modified.txt

cat {{file1}} {{file2}} > {{target_file}}
# Concatenate several files into the target file.

cat {{file1}} {{file2}} >> {{target_file}}
# Append several files into the target file.

CS101, long way to go, long time to learn.

updatedupdated2020-06-062020-06-06