Essential Linux Commands: A Comprehensive Guide for Beginners
Linux commands are the best ways to navigate on a Linux system. Linux experts usually control Linux clients and servers using commands instead of a graphical user interface for faster usage and automation purposes. Not to mention, commands free you from mouse usage and make you more productive.
Let me introduce you to the basic Linux commands to advanced ones and everything in between so you can also aspire to master Linux systems.
Introduction to Linux Commands
Command line interface is the preferred mode for all kinds of interactions with Linux systems, like client-side devices, locally hosted Linux servers, or Linux Virtual Dedicated Servers.
As a beginner, you can start with the basic ones, like ls and cd. However, as you gain experience navigating a Linux server using commands, you’ll leverage powerful tools like awk, and sed, as well as shell scripting for automation.
The Basic Linux Command Structure
A Linux command has the following parts and appears as a structured syntax.
command [options] [arguments]
- Command: This is the main instruction (e.g., ls to list files).
- Options (Flags): They modify the behavior of the command (e.g., ls -l shows detailed file information).
- Arguments: These specify targets for the command (e.g., ls /home lists files in the /home directory).
Essential Requirements
You must have access to the command-line interface of the Linux virtual private server (VPS) or the client desktop. Whether you’re using a Ubuntu user account with root access or not also matters. Most basic commands (e.g., ls, cd, mkdir, touch, find, etc.) do not require root access. Only system-level commands (e.g., apt install, systemctl restart) need sudo/root. If you’re not sure, you can learn to create a user in Linux with sufficient access to use all the commands explained in this guide.
Now that you’re all set to begin, let me introduce you to these popular and essential 21 Linux terminal commands to administer Linux clients and servers.
Basic Linux Commands for Beginners
1. ls lists files and directories in the current directory.
Syntax
ls [OPTION] [FILE]
Explanation of Syntax
ls → The command itself.
[OPTION] → Optional flags to modify the output (e.g., -l for detailed list, -a for hidden files).
[FILE] → Optional file or directory names to list specific items. If omitted, it lists the current directory.
Example Usage
ls -l /home/user/Documents
2. pwd displays the current working directory.
Syntax
pwd
Example Usage
Shows the full path of the current working directory.
pwd
3. cd changes the current directory.
Syntax
cd [DIRECTORY]
Explanation of Syntax
cd → The command itself; is used to navigate directories.
[DIRECTORY] → The target directory to move into. If omitted, it defaults to the home directory.
Example Usage
Changes the current directory to /home/user/Documents.
4. mkdir creates a new directory.
Syntax
mkdir [OPTION] DIRECTORY
Explanation of Syntax
mkdir → The command itself; is used to create directories.
[OPTION] → Optional flags (e.g., -p to create parent directories if they don’t exist).
DIRECTORY → The name(s) of the directory to create.
Example Usage
Creates a directory named NewFolder inside /home/user/Documents.
mkdir /home/user/Documents/NewFolder
5. rm removes files or directories.
Syntax
rm [OPTION] FILE
Explanation of Syntax
rm → This is the command used to delete files or directories.
[OPTION] → Optional flags (e.g., -r for recursive deletion, -f for force deletion).
FILE → The file(s) or directory(ies) to remove.
Example Usage
Deletes a file named sample.txt inside /home/user/Documents.
rm /home/user/Documents/sample.txt
Commonly Used Linux Terminal Commands
6. cp copies files and directories.
Syntax
cp [OPTION] SOURCE DESTINATION
Explanation of Syntax
cp → This part is the actual command which copies files or directories.
[OPTION] → Optional flags (e.g., -r to copy directories recursively, -i for interactive mode).
SOURCE → The file(s) or directory(ies) to copy.
DESTINATION → The target location where the copied file(s) or directory(ies) will be placed.
Example Usage
Copies file.txt from /home/user/Documents to /home/user/Backup.
cp /home/user/Documents/file.txt /home/user/Backup
7. mv moves or renames files and directories.
Syntax
mv [OPTION] SOURCE DESTINATION
Explanation of Syntax
mv → The command itself; is used to move or rename files and directories.
[OPTION] → Optional flags (e.g., -i for interactive mode, -v for verbose output).
SOURCE → The file(s) or directory(ies) to move or rename.
DESTINATION → The target location or new name for the file(s) or directory(ies).
Example Usage
Renames oldname.txt to newname.txt in /home/user/Documents.
mv /home/user/Documents/oldname.txt /home/user/Documents/newname.txt
8. cat displays the contents of a file.
Syntax
cat [OPTION] [FILE]
Explanation of Syntax
cat → This is the command itself. It’s used to read and display file contents.
[OPTION] → Optional flags (e.g., -n to show line numbers, -E to show line endings).
[FILE] → The file(s) whose contents will be displayed.
Example Usage
Displays the contents of notes.txt in /home/user/Documents.
cat /home/user/Documents/notes.txt
Working With Files and Directories in Linux
9. touch creates an empty file or updates the timestamp of an existing file.
Syntax
touch [OPTION] FILE
Explanation of Syntax
touch → The command itself; is used to create new empty files or update the timestamp of existing files.
[OPTION] → Optional flags (e.g., -c to avoid creating a file if it doesn’t exist, -t to specify a timestamp).
FILE → The file(s) to create or update.
Example Usage
Creates a new empty file named newfile.txt inside /home/user/Documents.
touch /home/user/Documents/newfile.txt
10. find it searches for files and directories based on name, type, size, or other attributes.
Syntax
find [DIRECTORY] [OPTION] [EXPRESSION]
Explanation of Syntax
find → The command itself; used to search for files and directories.
[DIRECTORY] → The starting directory for the search (default is the current directory).
[OPTION] → Optional flags (e.g., -name to search by filename, -type to filter by file type).
[EXPRESSION] → The search criteria (e.g., the file name or size).
Example Usage
Searches for a file named example.txt inside /home/user/Documents.
find /home/user/Documents -name “example.txt”
11. du displays the disk usage of files and directories.
Syntax
du [OPTION] [FILE]
Explanation of Syntax
du → The command itself; is used to check disk usage.
[OPTION] → Optional flags (e.g., -h for the human-readable format, -s for a summary of total size).
[FILE] → The file(s) or directory(ies) to analyze.
Example Usage
Shows the disk usage of the /home/user/Documents directory in a human-readable format.
du -h /home/user/Documents
Linux Commands for Networking
12. ping sends ICMP echo requests to a host to check network connectivity.
Syntax
ping [OPTION] DESTINATION
Explanation of Syntax
ping → The command itself; used to test network connectivity to a host.
[OPTION] → Optional flags (e.g., -c to specify the number of packets, -i to set the interval between requests).
DESTINATION → The target hostname or IP address to check connectivity.
Example Usage
Sends 4 ping requests to google.com to test connectivity.
ping -c 4 google.com
13. netstat displays network connections, routing tables, and interface statistics.
Syntax
netstat [OPTION]
Explanation of Syntax
netstat → The command itself; used to monitor network connections and statistics.
[OPTION] → Optional flags (e.g., -t to show TCP connections, -u to show UDP connections, -a to show all connections).
Example Usage
Displays all active TCP connections
netstat -t
14. curl transfers data from or to a server using various protocols (HTTP, FTP, etc.).
Syntax
curl [OPTION] URL
Explanation of Syntax
curl → The command itself; used to retrieve or send data over a network.
[OPTION] → Optional flags (e.g., -o to save output to a file, -I to fetch only the headers).
URL → The web address of the resource to be accessed.
Example Usage
Fetches the HTML content of example.com and displays it in the terminal.
curl https://example.com
Linux Commands for System Information
15. uname displays systems information such as kernel name, version, and architecture.
Syntax
uname [OPTION]
Explanation of Syntax
uname → The command itself; used to print system details.
[OPTION] → Optional flags (e.g., -a for all details, -r for kernel version, -m for machine hardware name).
Example Usage
Displays the system’s kernel version.
uname -r
16. uptime shows how long the system has been running, along with the number of logged-in users and load average.
Syntax
uptime
Explanation of Syntax
uptime → The command itself; it provides system uptime and load statistics.
Example Usage
Displays how long the system has been running.
uptime
17. df displays disk space usage for file systems.
Syntax
df [OPTION] [FILE]
Explanation of Syntax
df → The command itself; is used to check available and used disk space.
[OPTION] → Optional flags (e.g., -h for human-readable output, -T to show file system type).
[FILE] → The specific file system or directory to check (optional).
Example Usage
Displays disk space usage in a human-readable format.
df -h
Advanced Linux Shell Commands
18. awk processes and analyzes text files, typically for pattern scanning and data extraction.
Syntax
awk [OPTION] ‘PROGRAM’ FILE
Explanation of Syntax
awk → The command itself; is used for text processing.
[OPTION] → Optional flags (e.g., -F to specify a field separator).
‘PROGRAM’ → The script or pattern to apply to each line of input.
FILE → The file(s) to process.
Example Usage
Extracts and prints the first column of data.txt, assuming columns are space-separated.
awk ‘{print $1}’ data.txt
19. sed edits text in a stream or file using search, replace, and pattern-matching techniques.
Syntax
sed [OPTION] ‘SCRIPT’ FILE
Explanation of Syntax
sed → The command itself; is used for stream editing.
[OPTION] → Optional flags (e.g., -i for in-place editing, -e to specify multiple scripts).
‘SCRIPT’ → The instructions for modifying the text (e.g., search and replace).
FILE → The file(s) to edit.
Example Usage
It replaces all occurrences of “apple” with “orange” in fruits.txt without modifying the original file.
sed ‘s/apple/orange/g’ fruits.txt
Cool and Useful Linux Commands to Know
Syntax
20. htop displays an interactive and real-time view of system processes and resource usage.
htop [OPTION]
Explanation of Syntax
htop → The command itself; is used to monitor system performance interactively.
[OPTION] → Optional flags (e.g., -u to filter processes by the user, -p to show specific process IDs).
Example Usage
Launches htop to view system processes interactively.
htop
21. ncdu analyzes and displays disk usage in a user-friendly, interactive interface.
Syntax
ncdu [OPTION] [DIRECTORY]
Explanation of Syntax
ncdu → The command itself; provides a visual breakdown of disk usage.
[OPTION] → Optional flags (e.g., -x to stay within one file system, -r for read-only mode).
[DIRECTORY] → The directory to analyze (default is the current directory).
Example Usage
Analyzes disk usage of the /home directory interactively.
ncdu /home
FAQs About Linux Commands
Find below common questions on basic Linux commands to advanced ones so you don’t need to invest more time in searching for answers instead, you can practice the commands listed above:
What are the basic Linux commands every beginner should know?
You must master the following Linux codes as a beginner user of Linux machines:
ls [options] [directory]
cd [directory]
cp [options] source destination
mv [options] source destination
rm [options] target
How do I use the terminal in Linux?
Find below the effortless steps to use the terminal in your Linux client, servers, VPS, etc.:
- You can open the terminal by pressing Ctrl + Alt + T or searching for “Terminal” in the applications menu.
- Check the current directory using pwd to see where you are in the file system.
- Navigate between folders by using cd [directory] to move to a different location.
- Use ls at the end of a command to list files and folders in the current directory.
- Run a command by typing it (e.g., mkdir newfolder) and pressing Enter.
- Close the terminal using exit or by pressing Ctrl + D.
What is the most common Linux command?
The most common Linux command in a hosting environment like a Linux VPS is ssh. It allows secure remote access to the server. Administrators use it to connect to a remote machine. Running ssh user@server-ip enables server management from anywhere.
How do I list all commands available in Linux?
You can list all available commands in Linux by using the compgen -c command. Here are the basic steps to try:
- Press Ctrl + Alt + T to open the Terminal app.
- Run the command using compgen -c and press Enter to execute it.
- Scroll through the output by using Shift + Page Up and Shift + Page Down.
- Filter specific commands with compgen -c | grep keyword by searching for a keyword.
- Save the list by running the command compgen -c > commands.txt to store it in a file.
What are the most useful Linux commands for system management?
Here are the top 3 most useful Linux system management commands:
1. Top: It helps monitor system performance by displaying real-time CPU, memory, and process usage.
2. Systemctl: This command manages system services by starting, stopping, restarting, and checking their status.
Syntax: systemctl [command] [service]
Example: systemctl status apache2
3. df -h: It shows disk space usage by displaying available and used storage in a human-readable format.
Syntax: df -h [directory]
Example: df -h /
Are there any cool or advanced Linux commands worth learning?
You can try the fortune Linux command to do fun things when taking a break from your work. It displays a random quote, joke, or witty saying for fun and inspiration.
How can I create a custom command in Linux?
You can create a custom command using an alias for shortcuts. Alternatively, you can also use a shell script for more complex tasks. Aliases are temporary unless added to a configuration file. Contrarily, scripts can be made executable and placed in the system path. Here’s an example with steps:
- Define an alias by running alias customcmd=’actual_command options’ in the Terminal.
- Make the alias permanent by adding alias customcmd=’actual_command options’ to ~/.bashrc or ~/.bash_aliases.
- Create a shell script using nano myscript.sh and write the desired command inside.
- Add executable permission by running chmod +x myscript.sh to allow execution.
- Copy the script to /usr/local/bin/ using sudo mv myscript.sh /usr/local/bin/customcmd.
- Run the custom command by typing customcmd in the terminal.