The 45 essential Linux Commands for Every Userย
The 45 essential Linux Commands for Every Userย
Prerequisites
A system running Linux.
Access to the command line/terminal. Invoked by โจ๏ธ (Ctrl + Alt + T).
Basic Linux Commands
All Linux commands fall into one of the following four categories:
Shell builtins - Commands built directly into the shell with the fastest execution.
Shell functions - Shell scripts (grouped commands).
Aliases - Custom command shortcuts.
Executable programs - Compiled and installed programs or scripts.
pwd
The pwd command (print working directory) is a shell builtin command that prints the current location. The output shows an absolute directory path, starting with the root directory (/).
The general syntax is:ย
pwd <options>ย
ls
The ls command (list) prints a list of the current directory's contents. Run the following:ย
Show as a list:ย
ls -l
Show as a list and include hidden files:ย
ls -la
cd
The cd command (change directory) is a shell builtin command for changing the current working directory:
cd <directory>
For example, to move to the Document directory, run:
cd Documents
The working directory changes in the terminal interface. In a non-default interface, use the pwd command to check the current directory.ย
Use cd without any parameters to return to the home directory (~).
cat
The cat command (concatenate) displays the contents of a file in the terminal (standard output or stdout). To use the command, provide a file name from the current directory:
cat <filename>
Alternatively, provide a path to the file along with the file name:ย
cat <path>/<filename>
The command can also:
Display contents of multiple files:
cat <file 1> <file 2>
Display contents of multiple files:
cat <file 1> <file 2>
Create new files:
cat ><filename>
Add contents to the file and press CTRL+D to exit.ย
touch
The primary purpose of the touch command is to modify an existing file's timestamp. To use the command, run:
touch <file>
cp
The main way to copy files and directories in Linux is through the cp command (copy). Try the command with:
cp <source file> <target file>
The source and target files must have different names since the command copies in the same directory. Provide a path before the file name to copy to another location.ย
mv
Use the mv command (move) to move files or directories from one location to another. For example, to move a file from the current directory to ~/Documents, run:
mv <filename> ~/Documents/<filename>
mkdir
The mkdir command (make directory) creates a new directory in the provided location. Use the command in the following format:
mkdir <directory name>
rmdir
Use the rmdir command (remove directory) to delete an empty directory. For example:
rmdir <directory name>
rm
The rm command (remove) deletes files or directories. To use the command for non-empty directories, add the -r tag:
rm -r <file or directory>
locate
The locate command is a simple Linux tool for finding a file. The command checks a file database on a system to perform the search quickly. However, the result is sometimes inaccurate if the database is not updated.
To use the command, install locate and try the following example:
locate <filename>
find
Use the find command to perform a thorough search on the system. Add the -name tag to search for a file or directory by name:
find -name <file or directory>
grep
The grep command (global regular expression print) enables searching through text in a file or a standard output. The basic syntax is:
grep <search string> <filename>
The output highlights all matches. Advanced commands include using grep for multiple strings or writing grep regex statements.ย
sudo
The sudo command (superuser do) elevates a user's permissions to administrator or root. Commands that change system configuration require elevated privilege.
Add sudo as a prefix to any command that requires elevated privileges:
sudo <command>
Use the command with caution to avoid making accidental changes permanent.ย
df
The df command (disk free) is used to check available disk space on the file system. To see how df works, run the following:
df
The output shows the amount of space used by different drives. Add the -h tag to make the output in human-readable format (kilobytes, megabytes, and gigabytes).ย
du
The du (disk usage) command helps show how much space a file or directory takes up. Run the command without any parameters:
du
The output shows the amount of space used by files and directories in the current directory. The size displays in blocks, and adding the -h tag changes the measure to human-readable format.ย
head
Use the head command to truncate long outputs. The command can truncate files, for example:
head <filename>
For example, to see the first ten lines of the du command, run:ย
du | head
The output shows the first ten lines instead of everything.ย
tail
The Linux tail command does the opposite of head. Use the command to show the last ten lines of a file:
tail <filename>
For example, use tail to see the last ten lines of the du command:ย
du | tail
Both head and tail commands are helpful when reading Linux log files.ย
diff
The diff command (difference) compares two files and prints the difference. To use the command, run:
diff <file 1> <file 2>
For example, to compare files test1.txt and test2.txt, run:ย
diff file1.txt file2.txt
Developers often use diff to compare versions of the same code. ย
tar
The tar command (tape archiver) helps archive, compress, and extract archived files.
The command manages and creates files known as tarballs, which often appear during installation processes. The options provide different functionalities depending on the task.
chmod
Use the chmod (change mode) command to change file and directory permissions. The command requires setting the permission code and the file or directory to which the permissions apply.
For example:
chmod <permission> <file or directory>
The permission is a number code consisting of three numbers:
The first number is the permission of the current user (owner).
The second number is the permission for the group.
The third number is permissions for everyone else.
For example, to change the file permissions for a test.txt file so anyone can read, write, and execute, run:
chmod 777 file.txt
chown
The chown command (change ownership) changes the ownership of a file or directory. To transfer ownership, use the following command as sudo:
sudo chown <new owner name or UID> <file or directory>
For example:ย
sudo chown bob file.txt
Configuring ownership is a common task during installations. The chown command allows daemons and processes to access files during setup.ย
ps
The ps (process status) command lists processes currently running on the system. Every task creates a single or multiple processes running in the background.
Run ps without any options to see the running processes in the terminal session:
ps
The output shows the process ID (PID), the terminal type, CPU time usage, and the command that started the process.ย
top
The top command (table of processes) is an extended version of the ps command. Run the command without any options to see the result:
top
The output lists all running processes in real-time. To exit the viewer, press CTRL+C.ย
kill
Use the kill command to terminate an unresponsive process. The command syntax is:
kill <signal option> <process ID>
There are sixty-four different signal numbers, but the most commonly used are:
-15 saves all progress before closing the process.
-9 forces a stop immediately.
The process ID (PID) is unique for every program. Use the ps or top command to find the PID of a process.
ping
Use the ping command (packet internet groper) to check internet connectivity. The tool is valuable in troubleshooting networking issues. Add an address to test how it works, for example:
ping google.com
The output shows the response time from the website. Press CTRL+C to stop the ping. If no response shows, there's a problem connecting to the host.ย
wget
The wget command (WWW get) is used to download files from the internet. Use the following syntax to download a file:
wget <URL>
The command is robust and can continue downloads in unstable and slow networks.ย
uname
Use the uname command (Unix name) to print system information. Add the -a option to print a complete overview:
uname -a
The output shows the kernel version, OS, processor type, and other helpful information about the system.ย
history
The terminal session keeps a history log of commands. To see the list, use the history command:
history
Add a number after the command to limit the number of entries if the list is long. ย
echo
Use the echo command to print arguments to the terminal. The syntax is:
echo <argument>
For example, to print Hello, world! to the terminal run:
echo Hello, world!
The command helps append text to files, print program results, and display Linux environment variables.ย
hostname
To check the DNS name of the current machine, use the hostname command:
hostname
The hostname shows in the terminal as a result. Advanced features include changing the hostname, viewing and changing the system's domain, and checking the IP address.ย
useradd
The useradd command creates a new user on a Linux system. Since adding new users requires making changes to system files, add the sudo command to enable access.
The general syntax is:
sudo useradd <username>
The command creates a non-login user. Additional setup is necessary to activate the user account fully.ย
userdel
Use the userdel (user delete) command to remove a user from the system. Add sudo to enable the elevated privileges, for example:
sudo userdel <username>
The result does not show an output.ย
file
The file command provides information about a file, printing the file type and the contents type. To use the command, run:
file <filename>
The command does not take into account the file's extension. Instead, file performs testing on the file contents to determine the type.ย
wc
The wc command (word count) counts the number of lines, words, and bytes in a file. Provide a filename to count the elements in the file:
wc <filename>
Combine with other commands, such as cat, find, and ls, to perform advanced counts.ย
whoami
Use the whoami command to show the currently logged-in user for the shell session:whoa
whoami
The name of the effective user prints to the screen. Use the command in Bash scripts to show which user is running a script.ย
ip
The ip command contains many useful networking functionalities. For example, show the private IP address of the machine with:
ip addr
The command offers other networking functions, such as IP and routing table management.ย
apt, yum, RPM, zypper, pacman
Package managers help install, delete, and manage software packages on Linux systems. Different distributions of Linux use distinct package managers.
An example install looks like the following:
1. For Ubuntu, use the APT package manager:
apt install <package name>
2. 2. For CentOS and RHEL / Fedora, use yum, dnf or RPM:ย
yum install <package name>
dnf install <package name>
rpm -i install <package name>
3.For Arch, use pacman:ย
pacman -S <package name>
4. For opensuse, use the ZYPPER:
zypper <package name>
passwd
Use the passwd command to alter your password from the terminal. Run without any parameters:
passwd
The command also helps create a login for a new user added through useradd. Changing another account's password requires elevated privileges.
reboot
The reboot command restarts the system immediately from the terminal. First, save changes to all files and then run:
reboot
The system reboots instantly.ย
which
The which command shows the path of an executable program (command). To see a path for a command, run:
which <command>
For example:
which cat
The output shows the location of the command. Use which to troubleshoot installed programs that do not run. ย
nano
GNU nano (Nano's another editor) is a keyboard-oriented Linux text editor. You can use Nano to make a new file or open an existing one by running:
nano <filename>
The editor automatically opens, allowing you to append text or code to the file. To save and close, press CTRL+X, then Y, and confirm with Enter. ย
alias
Use the alias command to show and set customized command shortcuts. Without any parameters, alias lists the current shortcuts:
alias
To add a new alias, use the following format:ย
alias <name>=<command>
For example, to set cls as the alias for the clear command, run:ย
alias cls=clear
To remove a defined alias, use the unalias command. For example, to remove the cls alias, run:
unalias cls
Running the same command next time throws an error.
The cls command acts as an alias for the clear command and clear the screen of the terminal window.ย
clear
Use the clear command to clear the contents of the terminal quickly:
clear
The command clears the visual output and sets the terminal line at the top.ย
exit
To exit the current terminal session, run the exit command:
exit
If no other sessions were open, the command also closes the terminal.ย
ย To list all the commands supported by the system you can use compgen -cย