Linux commands form the backbone of interacting with the operating system through the command-line interface (CLI). The CLI empowers users to efficiently navigate, manage files, configure settings, and perform tasks in the Linux world.
Linux, created in 1991 by Linus Torvalds, is an open-source OS based on Unix principles. It swiftly gained popularity for its flexibility, stability, and community-driven development. With a 2.9% global market share in servers, it is widely used in embedded systems and cloud computing.
This article presents a comprehensive list of commonly used Linux commands, providing a foundation for effectively navigating and managing Linux systems. Mastering them enhances productivity and confidence, whether you are a beginner or an intermediate user.
What is a Command-Line Interface (CLI)?
A Command-Line Interface is a text-based user interface that allows users to interact with a computer system by entering specific commands through a terminal or shell. Unlike the graphical user interface (GUI), it operates solely on text-based input, omitting visual elements like windows, icons, and menus.
Advantages of CLI over GUI
Efficiency: CLI commands typically require fewer system resources, making them faster and more efficient, especially on older or resource-constrained machines.
Flexibility: CLI commands provide greater flexibility and fine-grained control over system functions, enabling users to perform complex tasks with precision.
Automation: CLI allows for easy automation through scripting. Users can create and run scripts containing multiple commands, streamlining repetitive tasks.
Remote Access: CLI can be accessed and used remotely over a network connection, making it ideal for managing servers and devices located in different physical locations.
Scriptable and Reproducible: CLI commands can be easily saved as scripts, allowing users to share and reproduce specific actions, configurations, or installations.
Structure of Linux Commands
Linux commands follow a specific structure, consisting of the command itself, options, and arguments.
Command
The command is the core instruction that tells the system what action to perform. It is usually the first word entered in the command line. Examples of commands include ls, mkdir, cp, and rm.
Option
Options modify a command's behaviour, allowing users to customize its functions. They can be short (single-character) or long (word) options, preceded by a hyphen (-). Short options can be combined, e.g., -la for -l and -a together. Examples include -l, -a, and --recursive.
Argument
Arguments are input data for a command, indicating which files, directories, or resources the command should operate. Commands may require one or more arguments, and they come after the command and options. For example, in cp file.txt /home/user/, file.txt is the source argument, and /home/user/ is the destination argument.
Example:
Command: ls
Options: -l, -a
Arguments: /home/user/
In this example, the command is ls, the options are -l and -a, and the argument is /home/user/.
In the next section, we will explore common Linux commands, and how they combine commands, options, and arguments to perform tasks on the system.
Basic Linux Commands
These basic Linux commands provide fundamental functionality for navigating, managing files, and performing essential tasks on a Linux system.
1. pwd (Print Working Directory): Displays the full path of the current working directory.
Example:
$ pwd
/home/user/documents
When you enter this command in the terminal and press Enter, it will output the current working directory, which in this case is /home/user/documents.
2. ls (List): Lists files and directories in the current working directory.
Example:
$ ls
file1.txt file2.txt directory1 directory2
The output will display the files and directories in the current directory, which are file1.txt, file2.txt, directory1, and directory2.
3. cd (Change Directory): Changes the current working directory to the specified directory.
Example:
$ cd /home/user/documents
After running this command, your current directory will be changed to /home/user/documents.
4. mkdir (Make Directory): Creates a new directory with the specified name.
Example:
$ mkdir new_directory
5. rmdir (Remove Directory): Removes an empty directory (the directory must be empty for this command to work).
Example:
$ rmdir directory_to_remove
6. cp (Copy): Copies files or directories from a source location to a destination location.
Example:
$ cp file1.txt /home/user/documents/
7. mv (Move): Moves or renames files and directories. It can also be used to move a file to a different directory, effectively renaming it.
Examples:
$ mv file1.txt /home/user/documents/
# Moves file1.txt to /home/user/documents/
$ mv file1.txt newname.txt
# Renames file1.txt to newname.txt
8. rm (Remove): Deletes files or directories. Be cautious as this action is irreversible, and deleted files cannot be recovered easily.
Examples:
$ rm file1.txt
# Removes file1.txt
$ rm -r directory_to_remove
# Removes a directory and its contents recursively
9. Exit: Exits the current shell or terminal session.
Example:
$ exit
As you get comfortable with the CLI, explore more options and arguments for each command to boost productivity and system management skills. Be cautious when using commands like rm, as they can have permanent consequences on your data. Double-check your actions before executing them.
File and Directory Operations
With these commands, you can efficiently manage files and directories on your Linux system. Remember to use them with care, especially when deleting or modifying files. Always double-check your actions to avoid accidental data loss.
1. cat (Concatenate): Displays the content of one or multiple files in the terminal.
Example:
$ cat file.txt
# Display the content of file.txt
$ cat file1.txt file2.txt
# Display the content of both files
2. more: Displays the content of a file one screen at a time. Press the spacebar to view the next page and 'q' to quit.
Example:
$ more large_file.txt
# Display large_file.txt one screen at a time
3. less: Similar to 'more', but allows scrolling both forward and backward in the file. Use arrow keys to navigate and 'q' to quit.
Example:
ruby
Copy code
$ less long_file.txt
# Display long_file.txt with scrollable navigation
4. head: Displays the beginning (head) of a file. By default, it shows the first 10 lines of the file.
Example:
$ head file.txt
# Display the first 10 lines of file.txt
$ head -n 20 file.txt
# Display the first 20 lines of file.txt
5. tail: Displays the end (tail) of a file. By default, it shows the last 10 lines of the file.
Example:
$ tail file.txt
# Display the last 10 lines of file.txt
$ tail -n 5 file.txt
# Display the last 5 lines of file.txt
6. touch: Creates an empty file or updates the modification timestamp of an existing file.
Example:
$ touch new_file.txt
# Creates a new empty file named new_file.txt
$ touch existing_file.txt
# Updates the modification timestamp of existing_file.txt
7. find: Searches for files and directories in a specified location based on various criteria such as name, size, and type.
Example:
$ find /home/user -name "*.txt"
# Find all files with .txt extension in /home/user
8. Grep: Searches for a specific pattern or text in a file or multiple files.
Example:
$ grep "keyword" file.txt
# Search for 'keyword' in file.txt
$ grep -r "keyword" /home/user
# Search for 'keyword' recursively in /home/user directory
9. locate: Quickly finds files and directories on the system using a prebuilt database (updated by the updatedb command).
Example:
$ locate file.txt
# Find the path of file.txt in the database
These file and directory operation commands provide essential tools for working with files, searching for content, and navigating through their content.
System Information Commands
System info commands offer vital insights(kernel details, uptime, disk space usage, logged-in users, etc). into your Linux system They aid in understanding system status, resource monitoring, and troubleshooting. You can use these commands in terminal sessions for real-time Linux system data.
1. uname: Displays system information like the kernel name, machine architecture, operating system name, and release version.
Example:
$ uname -a
# Display all system information
2. hostname: Shows the system's hostname, which is the name assigned to the machine on the network.
Example:
$ hostname
# Display the hostname of the system
3. date: Prints the current date and time.
Example:
$ date
# Display the current date and time
4. uptime: Shows the time since the system was last booted and the system's current uptime.
Example:
$ uptime
# Display system uptime information
5. w: Displays information about currently logged-in users and their activities.
Example:
$ w
# Show who is logged in and their activities
6. who: Shows a list of users who are currently logged in.
Example:
$ who
# Display currently logged-in users
7. Id: Displays information about the current user, including the user's UID (user ID) and GID (group ID).
Example:
$ id
# Display user identification information
8. df (Disk Free): Shows disk space usage for file systems on the machine.
Example:
$ df
# Display disk space usage
$ df -h
# Display disk space usage in human-readable format
Network Commands
These network commands are essential for diagnosing network issues, managing remote systems securely, and transferring data over a network. They provide valuable information about network connectivity, active connections, and network performance.
You can use these commands to troubleshoot network problems and perform tasks related to network communication and data transfer. Always ensure that you have appropriate permissions and authorization, especially for remote access and file transfers.
1. ping: Tests the reachability of a host on a network by sending ICMP echo request packets and waiting for responses.
Example:
$ ping google.com
# Send ICMP echo requests to google.com
2. netstat: Displays network statistics, active network connections, and routing tables.
Example:
$ netstat -tuln
# Show all TCP and UDP listening ports
3. ssh (Secure Shell): Establishes a secure encrypted connection to a remote host over a network, providing a secure shell for remote access and command execution.
Example:
$ ssh username@remote_host
# Connect to the remote host using SSH
4. scp (Secure Copy): Securely copies files between a local host and a remote host using SSH for encryption.
Example:
$ scp file.txt remote_host:/home/user/
# Copy file.txt to the remote host's /home/user/ directory
5. curl: Command-line tool to transfer data from or to a server, supporting various protocols like HTTP, HTTPS, FTP, etc.
Example:
$ curl https://example.com
# Retrieve the content of example.com over HTTPS
6. wget: Non-interactive command-line tool to download files from the web using HTTP, HTTPS, or FTP protocols.
Example:
$ wget https://example.com/file.zip
# Download file.zip from example.com
Process Management Commands
These process management commands are vital for monitoring and controlling processes on Linux. They provide detailed information, prioritize tasks, terminate unwanted processes, and manage system shutdown/restart.
Proper usage ensures efficient system resource utilization and enhances system stability. Exercise caution when terminating processes to avoid data loss or system instability.
1. ps (Process Status): Displays information about currently running processes.
Example:
$ ps
# Display information about processes owned by the current user
$ ps aux
# Display detailed information about all processes on the system
2. top: Provides real-time dynamic view of the processes running on the system with updates at regular intervals.
Example:
$ top
# Display dynamic process information in real-time
3. bg (Background): Puts a suspended or stopped process in the background, allowing it to continue executing but not taking input from the terminal.
Example:
$ bg %1
# Resume the first suspended job in the background
4. fg (Foreground): Brings a background process to the foreground, allowing it to take input from the terminal.
Example:
$ fg %1
# Bring the first background job to the foreground
5. kill: Sends a signal to terminate a process. By default, the TERM signal is sent, but other signals can be specified.
Example:
$ kill PID
# Terminate the process with the specified PID
$ kill -9 PID
# Forcefully terminate the process with the specified PID
6. shutdown: Shuts down or restarts the system after a specified time or immediately.
Example:
$ shutdown now
# Shutdown the system immediately
$ shutdown -r +5
# Restart the system after 5 minutes
File Permissions Commands
File permission commands are vital for controlling access to files and directories in Linux. Setting permissions and ownership properly ensures authorized users can access, modify, or execute specific items. Exercise caution, especially with system-critical files, as incorrect permissions may impact system stability and security.
1. chmod (Change Mode): Changes the permissions (read, write, execute) of a file or directory.
Usage: The chmod command requires a three-digit octal code or a symbolic representation to set the permissions for the owner, group, and others (everyone else).
Octal Representation: The three digits represent the owner, group, and others, respectively. Each digit is a sum of three values: 4 for read, 2 for write, and 1 for execute. For example, to give read and write permissions to the owner, use 6 (4 for read + 2 for write).
Symbolic Representation: Uses symbols to represent the permissions. The symbols are "u" for the user (owner), "g" for the group, "o" for others, and "a" for all (user, group, and others combined). The operations are "+", "-", and "=" for adding, removing, and setting permissions, respectively. For example, to give read and write permissions to the owner, use "u+rw".
Examples:
$ chmod 644 file.txt
# Give read and write permissions to the owner, and read-only permissions to group and others.
$ chmod u+rw,go-w file.txt
# Add read and write permissions for the owner, and remove write permissions for group and others.
2. chown (Change Owner): Changes the ownership of a file or directory to a specific user.
Usage: Specify the new owner using the username or user ID (UID).
Examples:
$ chown user1 file.txt
# Change the owner of file.txt to user1
$ chown user1:group1 file.txt
# Change both the owner and the group of file.txt to user1 and group1, respectively
3. chgrp (Change Group): Changes the group ownership of a file or directory to a specific group.
Usage: Specify the new group using the group name or group ID (GID).
Examples:
$ chgrp group2 file.txt
# Change the group of file.txt to group2
Package Management Commands
Package management commands are vital for installing, updating, and deleting software on Linux. They automate software tasks, making it easier to manage systems. Use these commands with administrative privileges (sudo) to avoid permission problems. Remember that package management commands differ among Linux distributions.
1. apt (Advanced Package Tool) - Ubuntu/Debian:
apt is a package management tool used in Ubuntu and Debian-based distributions to install, update, and remove software packages. It is a front-end to dpkg, the low-level package manager.
Usage Examples:
$ sudo apt update
# Update the package lists from the repositories
$ sudo apt install package_name
# Install a package
$ sudo apt remove package_name
# Remove a package (keeps configuration files)
$ sudo apt purge package_name
# Remove a package along with its configuration files
2. yum (Yellowdog Updater Modified) - Fedora/RHEL/CentOS (legacy):
yum was the package management tool used in Fedora, RHEL (Red Hat Enterprise Linux), and CentOS until CentOS 8. It has been replaced by dnf in newer versions.
Usage Examples:
$ sudo yum update
# Update installed packages
$ sudo yum install package_name
# Install a package
$ sudo yum remove package_name
# Remove a package
3. dnf (Dandified Yum) - Fedora/RHEL/CentOS (modern):
dnf is the modern and improved package manager used in Fedora, RHEL 8, and CentOS 8 and later versions. It provides faster and more reliable package management.
Usage Examples:
$ sudo dnf update
# Update installed packages
$ sudo dnf install package_name
# Install a package
$ sudo dnf remove package_name
# Remove a package
4. zypper - openSUSE:
zypper is the package manager used in openSUSE to manage software packages. It can handle RPM packages and repositories.
Usage Examples:
$ sudo zypper refresh
# Refresh the package list from the repositories
$ sudo zypper install package_name
# Install a package
$ sudo zypper remove package_name
# Remove a package
Conclusion
Understanding these commands empowers users to perform various tasks like file management, resource monitoring, networking, and software installation.
Linux commands' versatility allows customization, scripting, and automation for advanced tasks. Practice regularly to enhance your skills, start with simple tasks, and gradually progress to more complex commands. Use documentation and online resources to expand knowledge and troubleshoot effectively.
If you’re looking for reliable hosting for Linux-based systems, Verpex provides robust Linux VPS Hosting solutions. Our secure, high-performance VPS servers offer an ideal environment for managing and deploying your Linux projects with confidence. Choose between unmanaged Linux VPS Hosting for full control and customization or managed Linux VPS Hosting for expert support and maintenance, allowing you to focus on your business while we handle the technical details.
Frequently Asked Questions
Are Linux commands case-sensitive?
Yes, Linux commands are case-sensitive. For example, ls, LS, and Ls are considered three different commands.
Can I undo a command or revert changes made with a command?
Some commands, like 'rm', are irreversible, leading to permanent data loss. Always double-check the command and its options before execution. To safeguard against irreversible actions, regularly back up critical data.
Are there graphical alternatives to Linux commands?
Yes, many Linux distributions offer graphical package managers and system utilities. However, mastering the command-line interface provides greater flexibility, efficiency, and control over the system.
How do I navigate directories using Linux commands?
You can use the cd command to change your working directory. For example, to navigate to a directory called "documents," type cd documents. To go up one level, use cd .., and to go back to the home directory, simply type cd.
Which Linux commands are used for file and directory operations?
Commonly used Linux commands for file and directory operations include ls, cp, mv, rm, mkdir, rmdir, touch, find, grep, cat, more, and less.
How can I get system information using Linux commands?
You can use commands like uname, hostname, date, uptime, w, who, id, and df to obtain various system information, such as kernel version, hostname, current date and time, system uptime, logged-in users, user IDs, and disk space usage.
What are the most commonly used Linux network commands?
Some commonly used Linux network commands are ping, netstat, ssh, scp, curl, and wget. These commands are used for testing network connectivity, displaying network statistics, remote access, and downloading files over the network.
Can I use multiple Linux commands at once?
Yes, you can chain multiple commands together using the && operator to execute them sequentially or | to pipe the output of one command as input to another.
How can I view the manual pages for Linux commands?
You can view the manual pages for Linux commands using the man command followed by the name of the command. For example, man ls will display the manual page for the ls command.
What should I do if I forget a Linux command?
If you forget a Linux command, you can use the help command or append --help to the command you are unsure about. Additionally, online resources, documentation, and Linux forums are great places to find information about specific commands.
Is it possible to automate tasks using Linux commands?
Yes, you can automate tasks using Linux commands by creating shell scripts or using tools like cron to schedule tasks to run at specific times or intervals.
Are there Linux commands that can potentially harm my system if used incorrectly?
Yes, some commands like rm -rf (recursive force delete) have the potential to cause data loss if used incorrectly. Always double-check your commands, especially those that involve deleting or modifying files or directories.
Yetunde Salami is a seasoned technical writer with expertise in the hosting industry. With 8 years of experience in the field, she has a deep understanding of complex technical concepts and the ability to communicate them clearly and concisely to a wide range of audiences. At Verpex Hosting, she is responsible for writing blog posts, knowledgebase articles, and other resources that help customers understand and use the company's products and services. When she is not writing, Yetunde is an avid reader of romance novels and enjoys fine dining.
View all posts by Yetunde Salami