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.

Navigating the file system

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.

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.