Useful Terminal Commands for Linux, macOS and Ubuntu

Using the terminal is a powerful way to get things done on a computer and in some cases it is the only way to achieve things.

 

With that In mind, I decided to create this list of my favourite Terminal commands. Some of them are essential for properly working with Terminal but I have included them anyway since there will be many who are not aware of them.

 

And by the way,  all of these commands should work on any popular Unix-like system including, Linux, Ubuntu and macOS.

 

List Files and Directories

ls

The ls command is fundamental to working in the terminal, however, there are some additional features it offers to make it more useful. Find out more information about the ls command

 

Change Directory

cd new_directory

One must know the cd command to navigate the file system using a terminal. In the second argument of cd you can provide a relative or absolute path to the directory you wish to move to.

Move Up The Directory Hierarchy 

cd ..

cd followed by .. (two dots) will move the current working directory up one level. To traverse up the file system multiple times you can use the cd command like this:

cd ../../..

 

pwd

Typing pwd will return the full path to your current working directory. This can be super-handy to quickly identify if the next command you are to execute will indeed run from the location you intended it to be.

 

Autocomplete

This isn't a “command” but very useful. Every time you are typing in a terminal you can press the tab key to autocomplete whatever you began typing. This can be used on file names, directories, even command utilities.

 

nano /path/to/long/fil

Press tab to autocomplete the long file name:

nano /path/to/long/file-name023423425.txt

 

Piping Terminal Utilities

Piping is one if the most powerful features of Unix and allows one to chain multiple utilities into one command. Let's say we want to tail the output of a log file but we only want lines containing certain keywords to be returned in the output. After the tail command, we can type | (pipe) followed by grep which will get the output from tail and filter-out lines we are not looking for.

 

sudo tail -f /var/log/nginx/access.log | grep "example.com"

Pro-tip - you can chain as many pipes as you wish.

 

Copy Files

cp source_file target_file

Allows one to conveniently copy a source file to a new location. Directories can be copied by adding the -R flag before the source_file.

 

Move Files

mv source_file target_file

mv allows one to move a source file to a new location in the file system. It is also a handy way to rename files.

 

Securely Copy Files

scp [options] [user@source] [user@target]

The scp command provides functionally for securely uploading and downloading files within local networks and between remote servers. I have produced an article on the scp command which covers how to use it in detail.

 

Grep

grep [options] pattern [file...]

Grep is a powerful terminal utility that you can use to search for patterns in files across your system. You can use it to search a single file or multiple files and it will return matches on a line-by-line basis. Read more about the grep command.

 

Which

Which is one of my favourite terminal commands because it will tell you where command utilities are being run from. If you have recently updated a terminal utility and need to check if you are indeed running what you just installed which can let you know right away.

which php
/usr/bin/php

 

Wget

Wget is a command-line utility for downloading files from remote locations using URL's. It is available most Linux distributions, though for others and macOS you will need to install it.

 

Ubuntu Installation

sudo apt install wget

macOS Installation

The easiest way to get wget for macOS is to install it VIA Homebrew.

brew install wget

Wget Syntax

wget [options] [url]

 

Touch

The touch command can be used to create an empty file or update the timestamps on an existing file by passing in the -a flag.

touch -a existing_file.txt

 

Top

The top command provides a system overview of resource usage. It provides a list of currently running programs and how much memory and CPU cycles they are using.

top

 

Htop

Htop is essentially a feature-rich version of top. It provides a much cleaner, customisable interface as well as the ability to order the processes by any of the columns in the overview table such as CPU usage. This can be done by simply clicking on the column header.

 

Htop screenshot

 

There is a good chance you will need to install htop.

For Ubuntu

sudo apt install htop

For macOS

brew install htop

To run it just type in htop and hit enter:

htop

 

Rm

rm file.txt

Rm is another standard Linux command-line utility that packs in a lot more functionality than many are aware of. I have written a separate article on how to delete directories using rm as well as some of its other useful functions.

 

Sudo

Again another “must-know” command-line utility. Sudo allows a user to run commands like they were the root user. Of course, you will need to enter the root user account password for the command to run.

sudo nano /etc/hosts

Typically you will need to run a command as sudo for system-level files or other files with restricted read/write permissions.

 

Chown

This command stands for change owner and can be used to change the owner and group of a single file or recursively through directories using the -R flag.

chown [options] owner[:group] files...

 

Chmod

This command stands for change mode and is used to change the permissions of files. I have made an article which explains how to use the chmod command and what the file permissions flags mean.

chmod [permissions] [options] files...

 

Mkdir

The mkdir command is used to create new directories. 

mkdir [-pv] [-m mode] directory...

 

Nano

Nano is a simple command-line text editor that you can use to create and edit plain text files. If the file name you specify in the nano command does not exist on the file system it will create a new one.

nano /path/to/file.txt

 

Vim

Vim is another popular command-line text editor. It is similar to nano though it has many more rich features for searching and navigating through files making it a good option for editing code VIA the command-line. It does require some extra knowledge to use, which I have detailed in this article.

vim

 

SSH

SSH is a powerful terminal utility used for connecting to remote servers securely. For server administrators who need to quickly perform some maintenance, ssh is often the preferred method of connecting. Once connected to a remote machine VIA SSH you can type terminal commands like you were doing them on your local machine.

ssh [user@]IP-ADDRESS[:port]

 

Show Disk Usage

df is one of the most useful terminal commands in my opinion because it is so simple and will quickly show you an overview of file system usage. You can add the -h option to display disk usage in a human-readable format.

df -h
Filesystem		Size   Used  Avail Capacity iused				   ifree %iused  Mounted on
/dev/disk1s1   233Gi  231Gi  1.2Gi   100% 2254441 9223372036852521366    0%   /
devfs		   196Ki  196Ki    0Bi   100%     678						 0  100%   /dev
/dev/disk1s4   233Gi  966Mi  1.2Gi    45%		 1 9223372036854775806    0%   /private/var/vm

 

Rsync

Rsync is a powerful utility which provides similar functionality to auto-syncing file backup services such as Dropbox. The great thing about rsync is it only downloads files that are missing/different from your backup, saving server bandwidth.

rsync -av -e ssh [email protected]:/mnt/volume_1 /Volumes/Portable\ HD

I am going to be writing a full post on how to use rsync in detail because there is quite a lot involved in using it and several things to consider when backing-up files.

 

Tail

Tail is a handy utility, which prints the last ten lines of a file. Tail is quite nice for monitoring files that are auto-generated such as log-files as you will get a live feed of the new updates to the file.

tail access.log

 

du

This command displays disk usage based on your current working directory. It can be quite powerful when other commands are piped-in to organise the output from du. Let's say we wanted to find the top ten largest files in a directory:

/Users/johnhoward/Dropbox/web-design/sites/skillsugar
du -hsx * | sort -rh | head -10
432M node_modules
55M public
52M vendor
15M Archive.zip
13M storage
2.1M resources
816K package-lock.json
424K app
256K yarn.lock
244K database

 

Stat

stat can use used to provide detailed information about a set of given files or file systems.

stat [OPTION]... FILE...

 

Ping

ping is one of the most popular terminal commands and is used for testing the reachability of hosts. A simple way to use ping would be to check if you are connected to the internet by pinging google.com

ping google.com

 

Cycle Through Previous Commands

Did you type a long command in but made a tiny mistake? You can get your previous command and ones before that by pressing the arrow up key. It's not a "command" though certainly something you will find helpful.

 

Show Command History

Type history to get a full list of your previously entered commands.

history

 

Clear

Clears the terminal screen. Useful if you have a load of mess on the terminal due to something crashing.

clear

 

Curl

Curl provides a convenient way of downloading files from a server. It supports a variety of protocols including; HTTP, FTP, IMAP, POP3, SCP, SFTP and SMTP.

curl -O http://example.com/download.pdf

 

Reboot

Reboots the machine. You will most likely have to run this command in conjunction with sudo.

reboot [options]

 

Mkfile

Make a file of any user-specified size.

mkfile 1g text.txt

 

Conclusion

These are some of my favourite terminal commands for a variety of reasons. Yes, many people reading this will already know the basic ones but the idea of this article is to bring a mix of useful commands for people of all levels of knowledge.

terminal