Basic Linux Commands with Examples

Learn via video courses
Topics Covered

Overview

Linux is an open-source operating system that is widely used in the technology industry. Linux is known for its flexibility, security, and powerful command-line interface. The command-line interface in Linux allows users to interact with the operating system using commands that can be entered into a terminal window.

This article will cover the basic Linux commands and navigation that are used in the terminal and will also provide a brief description With a example of each command and its syntax.

What are Linux Commands and Why do you Need to Know Them?

Linux commands are instructions that are entered in the terminal, a text-based interface, to interact with the Linux operating system. They are used to perform various tasks such as managing files and directories, configuring system settings, controlling processes, networking, troubleshooting, and more. Few benefits of knowing Linux commands are:

Efficient System Administration : Linux commands are the primary means for system administrators to manage and maintain Linux-based servers and systems. They provide powerful and efficient ways to configure, monitor, and troubleshoot Linux systems, allowing administrators to perform tasks quickly and effectively.

Automation and Scripting : Linux commands can be combined in scripts to automate repetitive tasks, making it possible to perform complex operations with just a few lines of code. This can greatly enhance productivity and efficiency in managing Linux systems at scale.

Troubleshooting and Diagnostics : When issues arise in Linux systems, knowing relevant commands can help diagnose and resolve problems quickly. Commands such as grep, tail, top, netstat, ping, and traceroute can provide valuable information for troubleshooting and identifying root causes of issues.

Customization and Configuration : Linux commands allow users to customize and configure various aspects of the Linux system, including system settings, user accounts, file permissions, and network configurations. This flexibility enables users to tailor the system to their specific needs.

Learning and Education : Familiarity with Linux commands is essential for anyone who wants to learn and understand the Linux operating system. It provides a foundation for learning more advanced concepts and technologies related to Linux, such as shell scripting, system administration, and network administration.

In short we can say that Linux commands are powerful tools for managing, configuring, troubleshooting, and automating tasks in Linux-based systems. They are essential for system administrators, developers, and Linux users to efficiently interact with and manage Linux systems.

Linux Directory Commands

pwd - It is a command in Linux that stands for Print Working Directory. When executed, it displays the current working directory in the terminal. For example, running pwd in the terminal may output /home/user/documents to show the current working directory. The syntax of the command is: pwd

pwd

cd - It is a command in Linux that stands for Change Directory. When executed, it allows the user to navigate to a specific directory. For example, cd /home/user/documents would navigate to the documents directory located in the user's home directory. The Syntax is : cd [directory_name]

cd1

ls - It is a command in Linux that stands for List. When executed, it displays the files and directories in the specified directory. For example, running ls /home/user/documents would display all files and directories in the documents directory in the user's home directory. The Syntax of the command: ls [directory_path]

ls1

mkdir - This command in Linux that stands for Make Directory. When executed, it creates a new directory with the specified name in the current working directory. For example, if we type in this command mkdir test_file. It would create a new directory called test_file in the current working directory. Its syntax is: mkdir [directory_name]

mkdir1

rmdir - It is a command in Linux that stands for Remove Directory. When executed, it deletes the specified directory if it is empty. For example, rmdir test_file would delete the directory called test_file in the current working directory if it is empty. The syntax of this command is: rmdir [directory_name]

Linux File Commands

touch - This command in Linux is used to create a new empty file or update the modification time of an existing file. For example, touch textfile.txt creates a new empty file called "textfile.txt" in the current working directory, or updates the modification time if the file already exists. The syntax used is: touch [filename]

touch

rm - It is a command in Linux used to remove files and directories. For example, let's take "test" as the directory and "textfile.txt" as the file. Then rm textfile.txt would remove the file called "textfile.txt" from the current working directory. And to remove a directory, use the "-r" option rm -r test would remove the directory called "test" and its contents. The syntax used is: rm [filename]

mv - This command in Linux is used to move or rename files and directories. For example, to move a file named "testfile.txt" from the "Downloads" directory to the "Documents" directory, the command would be: mv ~/Downloads/file.txt ~/Documents/. Its syntax is as follows: mv [options] source destination

cp - It's a command in Linux used to copy files and directories. For example, to copy a file named "test.txt" from the "Downloads" directory to the "Documents" directory, the command would be: cp ~/Downloads/file.txt ~/Documents/. The syntax of the command is as follows: cp [options] source destination

Linux File Content Commands

cat - The command cat in Linux is used to display the contents of a file. The syntax of cat command is: cat [options] [file]; where options are optional flags that modify the output, and file is the name of the file(s) to display. For example, "cat file.txt" displays the contents of the "file.txt" file.: cat [file_name]

more - The more command in Linux is used to display the contents of a file page wise, one by one. Its syntax is more [options] [file]. For example, more file.txt displays the contents of "file.txt" one by one page at a time. Once pressing the spacebar moves to the next page.

linux file content commands

less - It is a command in Linux which is used to display the contents of a file page wise with advanced features of search and navigation. Its syntax is less [options] [file]. For example, less file.txt displays the contents of "file.txt" and allows for searching by pressing / followed by a search term.

less

Linux User Commands

whoami - This command in Linux is used to display the current logged-in user. For example, running the command whoami in the terminal will show the name of the user who is currently logged in. Its syntax is: whoami

su - The su command in Linux is used to switch to another user account from the current account, including the root user account. For example, running su root in the terminal will switch the user to the root user account and prompt for the root user's password. The syntax of su command is: su [options] [user].

useradd - This command in Linux is used to create a new user account. For example, if you run the command useradd mynewuser in the terminal, this will create a new user account with the username mynewuser. Additional options can be used to set up the password for the new account, home directory, and other properties. The syntax of this command is: useradd [options] [username].

passwd - The command passwd in Linux is used to change the password of a user account. For example, running passwd mynewuser in the terminal will prompt the user to enter and confirm a new password for the "mynewuser" user account. The syntax used is: passwd [options] username.

Linux User Commands

Linux Filter Commands

grep - The grep command in Linux is used to search for a pattern in a file or stream. For example, running grep ERROR logfile.txt in the terminal will search for the word "ERROR" in the "logfile.txt" file and will print any matching lines on screen. Its syntax is grep [options] pattern [file].

grep

sort - The sort command in Linux is used to sort the lines of a text file in ascending or descending order based on certain criteria. The syntax is sort [options] [file] where "options" are used to specify the sorting criteria and "file" is the name of the file to be sorted. Example: sort -n mobilenumbers.txt sorts the lines of the "mobilenumbers.txt" file in numerical order.

sort

uniq - The uniq command in Linux is used to display or remove adjoining duplicate lines from a sorted text file. The syntax is: uniq [options] [file]; where "options" can be used to specify the mode of operation. Example: uniq file.txt displays the unique lines in "file.txt" while removing adjoining duplicates.

Linux Utility Commands

top - This is a command-line utility in Linux used for real-time monitoring of system resources and system usage. As an example, for determining the resource usage of a system we use the command: top -c; which displays the processes running and sorted as per usage.

Basically the top command helps users to identify resource-intensive processes, monitor system performance, and manage system resources efficiently. The syntax used is: top [options]

ps - It is a Linux command for listing active processes. For an example: ps -ef; Command displays a list of running processes, their status, resource usage, and process hierarchy. This command is useful for monitoring system processes, identifying rogue processes, and managing processes in Linux. The syntax for this command is: ps [options]

Linux Utility Commands

kill - The kill command in Linux is used to terminate processes. Sometimes an unwanted or a rogue process tends to kill system resources. This is where the Kill command gets implemented. For an example: kill -9 1234; sends signal 9 (SIGKILL) to process ID (PID) 1234, and forcefully terminates the process. Syntax of this command is: kill [signal] [PID].

Linux Networking Commands

Networking commands are used to manage network configurations, for checking network status, and troubleshooting network related issues. Some essential Linux networking commands are as follows.

ifconfig - It is a Linux command used to configure network interfaces. As and example: ifconfig eth0 192.168.1.2 netmask 255.255.255.0 up configures the "eth0" interface with the IP address "192.168.1.2" and netmask "255.255.255.0", bringing the interface up.

ifconfig

Options include setting up broadcast addresses, assigning IPv6 addresses, enabling or disabling interfaces on a network. ifconfig is commonly used for network troubleshooting, setting up static IPs, and managing network interfaces in Linux systems. The Syntax of ifconfig command is: ifconfig [interface] [options].

ping - This is a Linux command used to test network connectivity between a local host and a remote host. As an example: ping 192.168.1.1 sends ICMP Echo Request packets to the IP address "192.168.1.1" to check if it's reachable and measure the round-trip time (RTT).

ping1

Options include setting packet size, interval, count, and more. Ping is commonly used for network troubleshooting, diagnosing network connectivity issues, and measuring network latency. Note: 'Ping' requires root or superuser privileges to send ICMP Echo Request packets, and some hosts or firewalls may block ICMP traffic. The syntax of the command is: ping [options] [host].

netstat - It is a Linux command used to display network connections and related network statistics. As an example: netstat -tuln; shows a list of all TCP (t) and UDP (u) listening (l) and established (n) connections present in the network.

netstat

Options include filtering by protocol, displaying process IDs (PIDs), resolving hostnames, and more. netstat provides information about active network connections, listening ports, and network utilization, making it useful for monitoring network activity, diagnosing network issues, and identifying network services running on a system. The Syntax of the command is: netstat [options].

traceroute - This is a Linux command used to trace the route that packets take from a source host to a destination host over an IP network. For an Example: "traceroute google.com" traces the route that packets take to reach the "google.com" domain, displaying the IP addresses of intermediate hops along with their response times.

traceroute

Options include setting the maximum number of hops, specifying the type of packets to send. "traceroute" is commonly used for network troubleshooting, diagnosing network path issues, and identifying network bottlenecks. The Syntax of the command is: traceroute [options] [host].

These are some of the major networking commands used to manage a network. For more extensive learning on networking on Linux check-out our page on Linux networking commands.

Conclusion

  • Linux commands are instructions that are used to perform various tasks on a Linux operating system.

  • Basic knowledge of Linux commands can help you perform tasks faster, automate tasks, and troubleshoot problems.

  • Some basic Linux commands include directory commands, file commands, file content commands, user commands, filter commands, utility commands, and networking commands.

  • Linux networking commands are used to manage the network configuration, check network status, and troubleshoot network-related issues.