Command Line Crash Course

Mastering the CLI: Essential Unix Commands Explained

·

8 min read

In the world of computing, the command line interface (CLI) is a powerful tool that allows users to interact with their operating systems and applications through text-based commands.

While graphical user interfaces (GUIs) have become the norm for most users, the CLI remains an essential tool for advanced users, developers, and system administrators.

In this blog, we'll explore some commonly used commands.

Beginner

Clear the Terminal Screen

clear

Print the Current Working Directory

pwd

Move Around Your Directory Structure

# change your current working directory to the specified directory
cd <directory_name> 
# navigate one level up in the directory hierarchy, taking you to the directory that contains your current working directory
cd ..
# navigate to your home directory, regardless of your current location in the file system
cd ~
💡
cd stands for "change directory".

If the directory you want to go to is nested deep, you need to know the path to get to it. You can type one command, with the different items in the path separated by forward slashes:

cd Desktop/project/src

List Directory Contents

# list the contents of a directory
ls
# list the contents of a directory including hidden files and directories
ls -a
# list the contents of a directory with detailed information about each item
ls -l

By default, the ls command only shows non-hidden files and directories. Hidden files and directories in Unix-based systems (like Linux and macOS) typically have names that start with a period (.). These files are often used to store configuration settings, preferences, or other metadata related to applications or the system itself.

Create Directories and Files

# creates a new directory in the current working directory
mkdir <directory_name>
# create a new empty file in the current working directory
touch <file_name>
💡
mkdir stands for "make directory".

Example

$ pwd
/home/user/Documents
$ mkdir Projects
$ ls
Projects
$ touch todo.txt
$ ls
Projects  todo.txt
$ open todo.txt

In this example, we first create a new directory named "Projects" using mkdir. Then, we create a new empty file called "todo.txt" using touch. Finally, we open the "todo.txt" file with the default text editor application using open.

Copy Files or Directories

# create a copy of a file
cp <file_name> <duplicated_file_name>
# creates a copy of a directory and all its contents (files and subdirectories)
cp -R <directory_name> <duplicated_directory_name>
💡
The -R (or -r) option stands for "recursive" and is required to copy directories and their contents.

Example 1

$ pwd
/home/user/Documents
$ ls
file.txt
$ cp file.txt backup_file.txt
$ ls
backup_file.txt  file.txt

In this example, we create a copy of the file file.txt named backup_file.txt in the current directory (/home/user/Documents).

Example 2

$ pwd
/home/user
$ ls
project
$ cp -R project project_backup
$ ls
project  project_backup

In this example, we create a copy of the file file.txt named backup_file.txt in the current directory (/home/user/Documents).

Move Files or Directories

# move a file from its current location to a different directory
mv <file_name> <destination_directory_name>
# move a directory from its current location to a different directory
mv <directory_name> <destination_directory_name>

Example 1

$ pwd
/home/user/Documents
$ mv report.txt /home/user/Desktop
$ ls /home/user/Desktop
report.txt

In this example, we move the file report.txt from the current directory (/home/user/Documents) to the Desktop directory (/home/user/Desktop).

Example 2

$ pwd
/home/user
$ mv my_new_project /opt/projects
$ ls /opt/projects
my_new_project

In this example, we move the my_new_project directory from the home directory (/home/user) to the /opt/projects directory.

Rename Files or Directories

# rename a file
mv <old_file_name> <new_file_name>
# rename a directory
mv <old_directory_name> <new_directory_name>

Example 1

$ pwd
/home/user/Documents
$ ls
file.txt
$ mv file.txt renamed_file.txt
$ ls
renamed_file.txt

In this example, we rename the file file.txt to renamed_file.txt in the current directory.

Example 2

$ pwd
/home/user
$ mkdir my_project
$ mv my_project my_new_project
$ ls
my_new_project

In this example, we create a new directory called my_project, then rename it to my_new_project.

Delete Files or Directories

# delete the specified file
rm <file_name>
# forcibly delete the specified file
rm -f <file_name>
# delete the specified directory and all its contents (files and subdirectories) 
pwd
rm -R <directory_name> 
# forcibly delete the specified directory and all its contents
pwd
rm -fR <directory_name>
💡
The -f option stands for "force" and allows the rm command to remove write-protected files without prompting for confirmation.
💡
The -R option stands for "recursive" and is required to delete directories and their contents.

It's a good practice to run pwd before deleting a directory to ensure you are in the correct location and won't accidentally delete the wrong directory.

Example

$ pwd
/home/user/Documents
$ ls
file.txt  directory
$ rm file.txt
$ ls
directory
$ pwd
/home/user/Documents
$ rm -R directory
$ ls

In this example, we first delete the file file.txt using rm file.txt. Then, we print the current working directory using pwd and delete the directory directory and all its contents using rm -R directory.

Open Files

# open files with their associated default applications
open <file_name>
# open with Visual Studio Code
code <directory_name>

Display Disk Usage for Files or Directories

# Display the disk usage summary for the current working directory
du -sh
# Display the disk usage for a specific file
du -sh <file_name>
# Displays the disk usage for a specific directory
du -sh <directory_name>
💡
du stands for "disk usage".
💡
-s means "summarize". It will display the total disk space occupied by the specified file, including any hard links.
💡
-h means "human-readable". It will display the file size in a human-readable format (e.g., KB, MB, GB, etc.) instead of raw bytes.

Example

$ du -sh file.txt
1.2M  file.txt

This shows that the file file.txt occupies 1.2 megabytes of disk space.

Intermediate

echo

echo is used to display text or strings on the terminal or to write them to a file.

Example 1

echo "Hello, World!"

This command simply prints the text "Hello, World!" to the terminal.

Example 2

message="Good morning"
echo $message

This command prints the value of the variable message ("Good morning") to the terminal.

Example 3

echo ".env" >> .gitignore

This command appends the text ".env" to the end of the file .gitignore. If the file doesn't exist, it will be created.

export

export is used to set and export environment variables. Environment variables are named values that can be accessed and used by various programs and processes running in the operating system.

Example

export https_proxy=http://127.0.0.1:33211 http_proxy=http://127.0.0.1:33211 all_proxy=socks5://127.0.0.1:33211

The command is used to set up proxy settings as environment variables.

alias

alias is a command in Unix-based systems (including Linux and macOS) that allows you to create shortcuts or alternative names for other commands or command sequences. It's a way to define custom commands or abbreviations for frequently used commands or command combinations.

When you create an alias, you associate a new command name with an existing command or a series of commands. This can make it easier to type and remember complex or long commands, as well as personalize your shell environment.

alias alias_name='command_to_alias'

Example

alias ll='ls -l'

This command creates an alias called ll for the command ls -l (which lists files and directories with detailed information). After defining this alias, you can simply type ll in the terminal instead of ls -l to get the same output.

Aliases are temporary and only exist for the current shell session. If you want to make them persistent across new sessions, you need to add them to your shell configuration file (e.g., .bashrc for Bash, .zshrc for Zsh).

It's important to be cautious when creating aliases, especially for potentially destructive commands like rm, and to avoid overriding existing commands or creating confusing aliases that could lead to unintended consequences.

source

source (also known as . or the dot operator in some shells) is a shell built-in command used to execute commands from a file in the current shell session.

# reads and executes the commands in file.sh as if you had typed them directly in the current shell
source file.sh

Example

💡
When you execute a script directly (e.g., ./script.sh), the commands in the script run in a subshell, and any changes made to the environment are lost when the subshell terminates. By using source, you ensure that the changes made by the script persist in your current shell session.

Suppose you have a script file my_script.sh with the following contents:

# my_script.sh
MY_VAR="Hello, World!"
alias ll='ls -l'
hello() {
    echo "Hello, $MY_VAR"
}

If you run this script directly with ./my_script.sh, the variable MY_VAR, the alias ll, and the function hello will only exist in the subshell created for running the script.

However, if you use source my_script.sh, these changes will be applied to your current shell environment, allowing you to use the variable, alias, and function immediately after sourcing the script.

curl

curl is a command-line tool for transferring data using various protocols, including HTTP, HTTPS, FTP, SFTP, and more. It is a versatile tool that allows you to interact with servers, download files, send requests, and perform many other tasks related to data transfer and web communication.

Example 1

curl https://example.com/file.zip --output file.zip

This command downloads the file file.zip from the specified URL and saves it to the current directory with the same name.

Example 2

curl https://example.com

This command sends an HTTP GET request to the specified URL and displays the response in the terminal.