How to Use Command Line

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!

Frequently used commands

pwd # Print working directory.
ls # List all files and directories.
cd # Change directory.
cd .. # Move up one directory.
mkdir # Make a new directory.
touch # Create a new file inside the working directory.
ls -a # List all contents, including hidden files and directories.
ls -l # List all contents of a directory in long format.
ls -t # Order files and directories by the time they were last modified.

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.

ls -alt

Manipulating the files

Copying

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 this_dir. * 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 this_dir.

cp abc*.txt this_dir

Moving

Use mv to move files. Using mv is as the same as using cp.

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.

Removing

rm some_file.txt # Delete files and directories.
rm -r some_dir # Delete a directory and all of its child directories. `-r` stands for "recursive".

Redirecting input and output

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.

The cat command outputs the contents of a file to the terminal.

cat hello.txt

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

The >> 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

The < command takes the standard input from the file on the right and inputs it into the program on the left.

cat < goodbye.txt

The | 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 hello.txt, respectively.

cat hello.txt | wc

Multiple | commands can be chained together.

cat hello.txt | wc | cat > goodbye.txt

The sort command takes the standard input and orders it alphabetically for the standard output.

sort hello.txt

The uniq command stands for “unique” and filters out adjacent, duplicate lines in a file. Note: only adjacent duplicate lines can be filtered.

uniq hello.txt

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

The 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

The grep -i command enables the command to be case insensitive. Note: “regular expression” means “正则表达式”.

grep -i sth hello.txt

The grep -R command searches all files in a directory and outputs filenames and lines containing matched results. -R means “recursive”.

grep -R some_dir

The 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

The 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
  • s stands for “substitution”. It is always used when using sed for substitution.
  • snow stands for the search string, the text to find.
  • rain stands 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.
  • sort sorts lines of text alphabetically.
  • uniq filters duplicate, adjacent lines of text.
  • grep searches for a text pattern and outputs it.
  • sed searches for a text pattern, modifies it, and outputs it.

Configuring environment

  • 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.
  • nano is a command line text editor. It works just like a desktop editor but only accepts keyboard input.
  • nano hello.txt opens a new text file named “hello.txt” in the nano text editor. In the bottom of the editor window, the ^ stands for the Ctrl key.
    • Ctrl + O saves a file. “O” stands for output.
    • Ctrl + X exits the nano. “X” stands for exit.
    • Ctrl + G opens a help menu.
    • clear clears the terminal window, moving the command prompt to the top of the screen.
  • echo $PATH returns the default path. PATH is an environment variable that stores a list of directories separated by a colon.
  • echo $HOME returns the default home directory.
  • env stands for “environment”, and returns a list of the environment variables for the current user.
  • ~/.bash_profile and ~/.zshrc are the profiles for bash and zsh respectively.
  • export VARIABLE="value" sets and exports an environment variable.
  • USER is the name of the current user.
  • PS1 is the command prompt, such as $ and >>.
  • HOME is the home directory. It is usually not customised.
  • PATH returns a colon separated list of file paths. It is customised in advanced cases.
  • env returns a list of environment variables.

Reference

List of Command Line Commands