The command line is a text interface for the computers’ operating system. It is important and convenient to use command window for coding, especially on Unix systems. This post is a study note from the course of Learn the Command Line, which is provided by codecademy. Enjoy reading!
pwd # Print working directory.
ls -a # List all contents, including hidden files and directories.
Combine the above three commands, i.e. list all contents, including hidden files and directories, in long format, ordered by the date and time they were last modified.
To copy a file into a directory, use
cp with the source file as the first argument and the destination directory as the second argument.
cp # Copy files or directories.
To copy multiple files into a directory, use
cp with a list of source files as the first arguments, and the destination directory as the last argument.
cp this_file.txt this_dir cp this_file.txt that_file this_dir
Copy all files in the working directory into
* is called wildcards, which is used to select group of files.
cp * this_dir
Copy all files in the working directory starting with “abc” and ending with “.txt” into
cp abc*.txt this_dir
mv to move files. Using
mv is as the same as using
mv old_name.txt new_name.txt # To rename a file, use `mv` with the old file as the first argument and the new file as the second argument.
rm some_file.txt # Delete files and directories.
echo "Hello" # The `echo` command accepts the string "Hello" as *standard input*, and echoes the string "Hello" back to the terminal as *standard output*.
- standard input, abbreviated as
stdin, is information inputted into the terminal through the keyboard or input device.
- standard output, abbreviated as
stdout, is the information outputted after a process is run.
- standard error, abbreviated as
stderr, is an error message outputted by a failed process.
echo "Hello" > hello.txt # The `>` command redirects the standard output to a file.
cat command outputs the contents of a file to the terminal.
The ‘>’ command takes the standard output of the command on the left, and redirects it to the file on the right. Note that
> overwrites all original content.
cat hello.txt > goodbye.txt
>> command takes the standard output of the command on the left and appends (adds) it to the file on the right.
cat hello.txt >> goodbye.txt
< command takes the standard input from the file on the right and inputs it into the program on the left.
cat < goodbye.txt
| command is a “pip”. The
| takes the standard output of the command on the left, and pipes it as standard input to the command on the right. It can be regarded as “command to command” redirection. The
wc command outputs the number of lines, words, and characters in
cat hello.txt | wc
| commands can be chained together.
cat hello.txt | wc | cat > goodbye.txt
sort command takes the standard input and orders it alphabetically for the standard output.
uniq command stands for “unique” and filters out adjacent, duplicate lines in a file. Note: only adjacent duplicate lines can be filtered.
It is more efficient to use
sort first, then use
uniq, which first orders the lines in alphabetically and then filter the adjacent duplicate lines.
sort hello.txt | uniq
grep command stands for “global regular expression print”. It searches files for lines that match a pattern and returns the results. It is also case sensitive.
grep sth hello.txt
grep -i command enables the command to be case insensitive. Note: “regular expression” means “正则表达式”.
grep -i sth hello.txt
grep -R command searches all files in a directory and outputs filenames and lines containing matched results.
-R means “recursive”.
grep -R some_dir
grep -Rl command searches all files in a directory and outputs only filenames with matched results.
-R stands for “recursive” and
l stands for “files with matches”.
grep -Rl some_dir
sed command stands for “stream editor”. It accepts standard input and modifies it based on an expression, before displaying it as output data. It is similar to “find and replace”.
sed 's/snow/rain/' hello.txt
sstands for “substitution”. It is always used when using
snowstands for the search string, the text to find.
rainstands for the replacement string, the text to add in place.
Redirection reroutes standard input, standard output, and standard error. The common redirection commands are:
>redirects standard output of a command to a file, overwriting previous content.
>>redirects standard output of a command to a file, appending new content to old content.
<redirects standard input to a command.
|redirects standard output of a command to another command.
sortsorts lines of text alphabetically.
uniqfilters duplicate, adjacent lines of text.
grepsearches for a text pattern and outputs it.
sedsearches for a text pattern, modifies it, and outputs it.
- The environment refers to the preferences and settings of the current user.
- environment variables are variables that can be used across commands and programs and hold information about the environment.
- The nano editor is a command line text editor used to configure the environment.
nanois a command line text editor. It works just like a desktop editor but only accepts keyboard input.
nano hello.txtopens a new text file named “hello.txt” in the nano text editor. In the bottom of the editor window, the
^stands for the
Osaves a file. “O” stands for output.
Xexits the nano. “X” stands for exit.
Gopens a help menu.
clearclears the terminal window, moving the command prompt to the top of the screen.
echo $PATHreturns the default path.
PATHis an environment variable that stores a list of directories separated by a colon.
echo $HOMEreturns the default home directory.
envstands for “environment”, and returns a list of the environment variables for the current user.
~/.zshrcare the profiles for
export VARIABLE="value"sets and exports an environment variable.
USERis the name of the current user.
PS1is the command prompt, such as
HOMEis the home directory. It is usually not customised.
PATHreturns a colon separated list of file paths. It is customised in advanced cases.
envreturns a list of environment variables.