Ubuntu is a popular open-source operating system used on many computers. To effectively use Ubuntu or Linux, you need to master terminal command for Ubuntu or Linux/Unix to perform various tasks. In this article, we will summarize some of the most important and useful Linux terminal commands, along with illustrative examples to help you become familiar with them. With these commands, you can easily perform tasks such as file management, software installation, and more.
14 Ubuntu Terminal Commands to Supercharge Your Linux Experience
du;
The (Disk Usage) command du
is a terminal command for Ubuntu or Ubuntu operating systems that is used to display the disk space used by files and folders in a specific directory or in the entire file system of the machine. count. du
stands for “disk usage” and is used to check the space usage of files and folders on disk.
The options used with du
enable disk space information to be displayed in various ways, such as displaying capacity in different units (KB, MB, GB), sorting results by size size or modification time, and more.
Here are examples of how to use the command du
in Ubuntu or Linux debian to show disk space used by subdirectories of a particular directory, but excluding that directory:
du -h --max-depth=1 /path/to/directory
As you can see in the screenshot, 2.8G is all my MYSQL space used.
In there:
-s
: is to display only the results of the root directory/var/lib/mysql
, instead of displaying the results of the files and subdirectories inside.-h
: option to show used disk space in units (KB, MB, GB) instead of bytes.
Another example that combines the option -ah
to check the file size in a specific directory:
sudo du -ah ~/Downloads
In there:
-a
: option to show all files and folders, including hidden files.-h
: option to show used disk space in units (KB, MB, GB) instead of bytes.
There are some other options like: –time, -c, –total, -X, –exclude=Pattern, you also do the same as the above examples to see how the results are.
--time
: option to show modification times of files and folders.-c
: option to show total size of files and folders.--total
: option to show total size of all browsed files and folders.-X
: option to exclude files and directories listed inEXCLUDE
.--exclude=Pattern
: option to exclude files and folders whose names match the patternPattern
.
find;
Command find
is one of the very popular terminal command for ubuntu and linux/unix, it is used to search and display files and folders in a directory tree based on many criteria such as filename, creation date, size, etc. , access rights and many other criteria.
The command find
also supports many options for searching files and directories with various custom configurations, allowing users to define complex search conditions, including recursive searching in directories. con, regular file search, hidden files, etc.
which;
Commands which
use environment variables PATH
to search for commands or programs. PATH is an environment variable that stores the paths where the system will look for programs to be executed. When you enter a command on the command line, the system searches for that command in the directories listed in the environment variable PATH.
The command which
is one of the terminal command for Ubuntu or Linux debian that is very useful for developers and system administrators in determining the location of programs installed on their system.
Example test mysql working path:
which mysql
The returned result will be the absolute path of mysql the system. If mysql installed in the directory /usr/bin/
, the result will be /usr/bin/mysql
.
clear;
The terminal command clear
in Ubuntu and other Linux operating systems is used to wipe the display, remove any information, command lines, results or any displayed data on the terminal, and return to the original position. top of the first line of the screen.
clear
The command clear
is often used when you want to clean the terminal to easily observe and perform other tasks on the terminal more conveniently.
locate;
Command locate
is one of the commands in ubuntu, linux used to find the location of a particular file. This is one of the simplest commands you can take advantage of when you are unsure about the location of a particular file on your Linux computer.
sudo apt install mlocate
Syntax to use:
locate [options] pattern
The locate
and find
command are both used for searching files and directories in a computer file system. However, the locate command operates faster and is suitable for name-based searches, whereas the find command is more powerful with various features that allow users to search based on different criteria such as size, creation time, and ownership.
- Speed: locate usually faster find because it uses a pre-created database to store information about files and folders on the system. When using locate, the system only needs to search this database instead of scanning the entire file system. Meanwhile, find will search the entire file system, so will be slower.
- Update the database: Since locate it uses a pre-created database, if there are changes in the file system (such as adding, editing or deleting files), you must update the database again data by running the command sudo update db to make sure locate the latest files can be found. Meanwhile, find will perform a search directly on the filesystem, so will always give the latest results.
- Search options:
find
provides various search options, allows you to search by file name, file type, size, changed date, etc. Meanwhile, only locate supports searching by file name .
So, when you need to search for a specific file or perform searches based on various criteria using terminal command for Ubuntu or Linux/Unix, you should use the find command. When you need a quick search and searching by file name is sufficient, you can use locate.
For example, to search for files name .bashrc
with locate, you can use the command:
locate .bashrc
grep;
The grep command in linux helps you find strings and characters in the specified file. This is considered one of the most frequently used terminal command for Ubuntu/Linux.
sed;
The command sed
(short for Stream Editor) is one of the basic commands in Linux/Unix, used to edit and process text in files and data streams. It allows users to perform operations such as find and replace, delete, insert and modify lines of text in a file or data stream on the command line.
Such files and data streams can be entered using input from the keyboard or various files. The sed
command is widely used in word processing and is often combined with other commands to perform more complex tasks.
Syntax to use:
sed [option] commands [file]
Example: Suppose you have a text file named “test.txt”, the content of the file is:
Hello world! This is a test file.
You want to replace the word “world” with the word “universe”. You can use the command sed
as follows:
sed 's/world/universe/' test.txt
The result you get will be:
Hello universe! This is a test file.
This command uses the option s
to replace the first string ‘world’ with the second string universe
and prints the result to the screen.
reset;
The reset
command is used to erase the entire contents of the terminal and reset the default settings. This command can be useful in case an error is displayed on the terminal or to clear the previous command history.
reset
echo;
Command echo
is a very powerful command line utility that allows you to output a specific text to the terminal console. Type echo
then some text in parentheses to find out for yourself. What’s more interesting about this command is that you can pipe the output on terminal.
echo "Creating a new file." > sample.txt
If we use >
redirection operator, the file is created if it doesn’t exist. If the file exists, the output from echo overwrites any previous content of the file.
echo "Adding to the file." >> sample.txt
If we use >>
redirection operator, file is created if it doesn’t exist. The output from echo is appended to the end of the file and does not overwrite any existing contents of the file.
You can open the sample.txt file to view the content or use the cat command.
cat sample.txt Creating a new file. Adding to the file.
Another example to use echo
that command is that you can use it to test sending emails on Linux:
echo \ "Content: Test gmail" | mail -s \ "Subject: Test sendemail" -a \ "From: buivanloi.2010@gmail.com" -a \ "To: To <buivanloi.2010@gmail.com>"buivanloi.2010@gmail.com
chmod;
The command chmod in the terminal of the Ubuntu or Linux operating system allows you to change the permissions of a file or directory. This is one of the most powerful command in Linux and Ubuntu that you can use to adjust or modify the permissions of files or objects on the system. However, to use this command, you may need sudo or root privileges.
chown;
The command chown
is very similar to the chmod command. But instead of changing the permissions, it allows the user to change the ownership of the file or folder. chmod
and chown
both require root privileges to run.
For example to change the username and group for the sample.txt file, first run the command ls -lah
to see what the file permissions are:
ls -lah
As you can see in the screenshot the ownership of the file is itsmeit and the group is itsmeit. Now try to assign root permissions to the file as follows:
sudo chown root:root newfile.txt
Now run the command again ls -lah
and see if the permissions of the newfile.txt file have been changed.
whatis;
Commands whatis
in the terminal on Linux/Unix operating systems are used to display brief descriptive information about a particular command. More specifically, the whatis command returns a brief description of a command stored in the whatis database. The purpose of the whatis command is to help users quickly learn about a command they are unfamiliar with, or simply review the description of a command they have forgotten.
Example how to use whatis
command on terminal in Ubuntu:
whatis nginx
top;
The top
is one of the important terminal command in Linux, allowing you to display information about the processes running on the system and using the computer resources such as CPU, RAM, swap, and more. It provides an interactive command line interface to monitor and manage processes and system resources in detail.
When running the terminal command for Ubuntu with the command top
, it will display a list of processes running on the system in order of CPU and RAM priority. Each process is listed with detailed information such as process ID (PID), runtime, resource usage, and resource usage percentage.
You can also install htop
, which is a command-line process management tool on Linux systems, similar to the top
command but with a graphical interface and provides more detailed information about the processes running and system resources are used.
sudo apt install htop
Syntax to use htop on terminal:
htop
sl;
Command sl
, short for Stream Locomotive, is a simple terminal command on Linux used to display an ASCII train screen moving on the terminal. It is often confused with the ls command. Although it has no practical effect, it is a fun entertaining command for Linux/Unix users. When you run this command, the screen will show a steam locomotive running across the terminal.
Syntax to install and use the command sl
:
sudo apt install sl sl
Above we have compiled useful and commonly used command in Ubuntu and Linux operating systems. To better understand each statement, you can use the man command with the syntax man <command>
to help you see complete information about the function and syntax of that statement.
Reference link: Useful tool for Linux/macOS