Most Common Terminal Commands

Posted by Ray on March 10, 2022

This document is a guide for most common terminal commands.

To determine which shell is your terminal running, type

1
2
$ echo $0
# should output your shell

In this tutorial, a .zsh will be used on a Mac Operating System.

Command Manual (man)

The man command shows the manual of the command or utility in the terminal. It shows the name, description, and options of the command among others.

1
2
$ man <command_name>
# should output the manual of the command you entered

For example, if you want to check the manual of command pwd, type $ man pwd in the terminal: man_pwd_command man_pwd_command_output

Press q to quit the manual.

Quit sub-screen (q)

When inside a command sub-screen, type q to end subs-screen and return to the terminal. The keyboard combinations ctrl + c also does the work.

Close terminal (exit)

Type exit to close current terminal session. This will close the terminal tab if no sub process is running.

List Directories (ls)

To list the contents of a specific directory, the ls command is invoked. If no directory is specified, the contents of the current directory will be listed.

The -a flag shows all contents - including the hidden ones. The -l lists the details of the contents.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ ls [path_to_specific_directory] [-a | -l]
# ls command with options -a and/or -l, and directory path

$ ls
# should output the list of directories of current directory

$ ls [path_to_specific_directory]
# should output the list of directories of the specified directory

$ ls -a
# should output the list of all contents

$ ls -l
# should output the details of the contents

$ ls -la
# should output the list of all contents with their details

man_pwd_command

Make Directories (mkdir)

To make directories, type mkdir with the option -p followed by the directory name in the terminal.

1
2
3
4
5
6
7
8
9
10
11
$ mkdir <directory_name>
# should create a directory with the specified directory name

$ mkdir -p <new/path/directory_name/>
# should create a nested directory with the specified directory names

$ mkdir <"name with spacings">
# should create a directory with names having spaces

$ mkdir -p <existing/path/new_directory>
# should create a directory in specified path

mkdir_command

To display your current full working path in the terminal, the pwd command is invoked.

1
2
$ pwd
# should output your current working directory

pwd_command_output

Change Directories (cd)

To change your current directory in the terminal, invoke the cd command with the directory name. The cd command stands for Change Directory.

1
2
3
4
5
6
7
8
$ cd <directory_to_change_into>
# should change current directory into one that is specfied

$ cd ..
# should change directory into the parent of the current directory

$ cd ../..
# should change directory into the parent of the parent of the current directory

cd_command_output

Clear terminal (clear)

To clear the terminal screen, invoke the clear command.

1
2
$ clear
# should clear the terminal

Move/Rename (mv)

To move files and/or directories, invoke the mv command followed by the file/directory to move and its new path/folder. The command can also be used to rename files and directories

1
2
3
4
5
$ mv <path/to/file> <new/path/of/file>
# should move the specified file to the new specified location

$ mv <oldname> <newname>
# should rename the specified file/folder to the new specified name

mv_command_output

Copy (cp)

To copy files and/or directories, invoke the cp command followed by the file/directory to copy and its destination path/folder. The command can also be used to copy a directory with contents, by adding -r.

1
2
3
4
5
6
$ cp <path/to/file> <destination/path/of/copied/file>
# should copy the specified file to the specified destination

$ cp -r <directory_name> <destination/path>
# should copy the entire directory and its contents
# to the specified destination

cp_command_output

Delete File or Nonempty Folder (rm)

To delete a file, invoke the rm command followed by the file name. To delete a folder with contents, add the -r option.

1
2
3
4
5
$ rm <path/to/file>
# should delete the specified file

$ rm -r <path/to/nonempty/directory>
# should delete the specified directory and its contents

rm_command_output

Delete Folders (rmdir)

To delete an empty folder, invoke the rmdir command followed by the folder name.

1
2
$ rmdir <path/to/directory>
# should delete the specified empty directory

rmdir_command_output

Concatenate (cat)

To print the contents of a file, use the cat command.

1
2
$ cat <file_name>
# should output the contents of the file in the terminal

cat_command_output

Create text file (touch)

To create a file, use the touch command followed by the file name and file extension.

1
2
$ touch <file_name>
# should create the file with the file extension

touch_command_output

Edit files (nano)

To edit a file, use the nano command followed by the name of the file that you want to edit. Edit your file in the terminal text editor, then press ctrl + X to exit, Y to save changes, and enter to select the edited file.

1
2
$ nano <file_name>
# should display the terminal text editor

nano_command_output nano_command_output

Use superuser privilege to execute commands (sudo)

To use superuser privileges to execute commands that the general users are not allowed, use sudo followed by the command.

1
2
$ sudo <command>
# should use superuser privileges to execute specified command

sudo_command_output