30 Most Used Basic Linux Commands You Must Know
Table of Contents
Linux is a powerful and popular operating system that is widely used in the tech industry. If you are new to Linux, you may find the command line interface to be daunting. However, mastering the Linux command line is an important skill that can help you to be more efficient and productive. In this article, we have provided a list of 30 commonly used Linux commands with brief explanations and code examples. By learning these commands, you will be well on your way to becoming a Linux power user.
Commands
ls
ls is a Linux command that stands for “list.” It is used to display a list of files and directories in a specific directory or the current working directory. By default, it displays the list of files and directories in a columnar format, but it can also display them in a detailed or long format with additional information, such as file permissions, owner, size, and modification time.
The ls command can also be used with various options to customize the output. For example:
To show hidden files and directories:
ls -a
To show the long format with additional information:
ls -l
To show file sizes in human-readable format:
ls -h
To sort files by modification time:
ls -t
cd
cd is a command used to change the current working directory. When a user types cd followed by a directory name, the shell changes the working directory to the specified directory. For example, if a user is currently in the home directory and wants to change to the “Documents” directory, they can type cd Documents. The cd command is a fundamental tool for navigating the file system on Linux.
cd /path/to/directory
pwd
pwd stands for “print working directory.” It is a command used in Unix and Unix-based operating systems, including Linux, to display the current working directory. When you type the pwd command in the terminal, the system will print the full path of the current working directory, which is the directory in which you are currently located in the terminal.
pwd
mkdir
mkdir is a command on Linux and Unix operating systems used to create a new directory (folder) in the current working directory or in a specific location specified by a path. The name of the directory to be created is specified as an argument to the command. For example, to create a new directory named “new_directory” in the current working directory, the command would be:
mkdir new_directory
If you want to create a directory at a specific location, you can specify the path to that location:
mkdir /path/to/new_directory
rm
rm stands for “remove.” It is a command-line utility on Linux and Unix-like operating systems used to remove or delete files and directories from the file system. When used with the appropriate options, it can remove files and directories recursively, force deletion, and prompt for confirmation before deleting files. However, it should be used with caution, as it permanently deletes files and directories without moving them to a trash bin, and there is no way to undo the deletion. For example, to remove a file named “example.txt” in the current working directory:
rm example.txt
This command will permanently delete the file, so be sure that you want to remove it before running the command.
rmdir
rmdir is a command on Linux that is used to remove or delete empty directories. It removes the directory entries from the file system, and the directory is no longer accessible. However, it cannot remove non-empty directories, and you need to use the rm command to delete directories that have files or other directories inside them.
The basic syntax of the rmdir command is as follows:
rmdir [option] directory_name
Here, directory_name specifies the name of the directory that you want to remove.
Some commonly used options with the rmdir command are:
-v: Display a message for each directory that is removed.
-p: Remove the parent directory if it is empty after removing the specified directory.
For example, to remove an empty directory called mydir, you can use the following command:
rmdir mydir
If you want to remove the parent directory along with mydir if it becomes empty, you can use the following command:
rmdir -p parent/mydir
Here, the -p option tells the rmdir command to remove the parent directory parent if it becomes empty after removing mydir.
cp
cp stands for “copy” and is used to copy files or directories. It takes two arguments: the source file or directory to copy, and the destination where the file or directory should be copied to. For example:
cp file.txt /path/to/destination
This will copy the file.txt file to the /path/to/destination directory. You can also use the -r option to copy directories recursively:
cp -r directory /path/to/destination
This will copy the entire directory and its contents to the /path/to/destination directory.
mv
mv is a command used to move files and directories or rename them. It is a powerful utility for managing files and directories within the Linux terminal. The syntax for the command is as follows:
mv [options] source destination
Here, source is the name of the file or directory that you want to move or rename, and destination is the new location or new name of the file or directory.
If the destination is a directory, then the source will be moved to that directory. If the destination is an existing file, then the source will be renamed to that file.
Some of the commonly used options of the mv command include:
-i: Prompts the user before overwriting an existing file.
-v: Prints the name of each file as it is moved or renamed.
-f: Forces the move or rename operation without prompting for confirmation, even if the destination file already exists.
Examples:
To move a file to a different directory:
mv file.txt /path/to/new/destination
To rename a file:
mv old_name.txt new_name.txt
To move a directory to a different location:
mv /path/to/directory/ /new/path/
To move a file to a new directory and rename it:
mv file.txt /path/to/new/directory/new_name.txt
touch
touch is a Linux command used to create a new empty file or to update the modification and access time of an existing file without changing its contents. The touch command can be used with the name of the file to be created or modified.
Here are some examples of using the touch command:
To create a new empty file:
touch file.txt
To update the modification time of an existing file:
touch file.txt
This will change the modification time of file.txt to the current time, without changing the contents of the file.
To create multiple files at once:
touch file1.txt file2.txt file3.txt
This will create three new empty files named file1.txt, file2.txt, and file3.txt.
cat
cat is a command on Linux that is used to concatenate and display the contents of files on the terminal. The name “cat” is short for “concatenate.” It can be used to display the contents of a single file or multiple files concatenated together.
The syntax for using the cat command is:
cat [options] [file(s)]
Here, options are optional arguments that modify the behavior of the cat command, and file(s) are the file or files whose contents are to be displayed.
For example, to display the contents of a file named file.txt, you can use the following command:
cat file.txt
To concatenate the contents of two files, say file1.txt and file2.txt, and display them on the terminal, you can use the following command:
cat file1.txt file2.txt
The cat command is a versatile tool that can be used in many ways, such as creating new files, appending files, and more.
grep
grep is a command-line utility for searching plain-text data for lines that match a given pattern. It stands for “global regular expression print”.
The basic syntax of grep command is:
grep [options] pattern [files]
Here, pattern is the text pattern that you want to search for in one or more files. files is an optional argument that specifies the file or files you want to search in. If no files are provided, grep reads input from standard input.
Some of the most commonly used options with grep include:
-i: Ignore case while searching
-v: Invert the match, i.e., print all lines that do not match the pattern
-w: Match only whole words
-n: Display line numbers for matched lines
-c: Display only a count of matched lines
For example, to search for the word “example” in a file called file.txt, you can use the following command:
grep example file.txt
This will display all lines in file.txt that contain the word “example“. You can use various options to modify the search criteria and behavior of the grep command.
find
find is a command used to search for files and directories in a specified location based on various criteria such as name, size, modification time, and ownership. It can also be used to perform actions on the files and directories that match the search criteria. The basic syntax of the find command is:
find [path] [expression]
Where [path] specifies the directory to search in and [expression] specifies the search criteria.
Some examples of find commands are:
find /home/user -name “*.txt”
Search for all files with a .txt extension in the /home/user directory and its subdirectories.
find /home/user -type f -size +1M
Search for all files larger than 1MB in the /home/user directory and its subdirectories.
find /home/user -mtime -7
Search for all files modified within the last 7 days in the /home/user directory and its subdirectories.
find /home/user -user john -exec chown bob {} \;
Search for all files and directories owned by the user ‘john‘ in the /home/user directory and its subdirectories, and change the ownership to the user ‘bob‘.
find is a powerful and flexible command that can be used to perform complex searches and automate tasks.
chmod
chmod (short for “change mode”) is a command-line utility that allows you to change the permissions of files and directories in a Linux/Unix-based operating system. The permissions determine who can read, write, and execute the file or directory. The chmod command uses a three-digit code to set the permissions for the owner, group, and others.
Here are some examples of chmod commands:
Give the owner read, write, and execute permissions, and everyone else only read permissions:
chmod 744 file.txt
Give the owner and group read and write permissions, and everyone else no permissions:
chmod 660 file.txt
Give the owner and group read, write, and execute permissions, and everyone else only read and execute permissions:
chmod 775 directory/
Give the owner, group, and everyone else full read, write, and execute permissions:
chmod 777 file.txt
In each example, the three-digit code specifies the permissions for the owner, group, and others. The first digit specifies the permissions for the owner, the second digit specifies the permissions for the group, and the third digit specifies the permissions for everyone else.
The digits represent the following permissions:
4: Read
2: Write
1: Execute
So for example, a code of 744 means that the owner has read, write, and execute permissions (4+2+1=7), and everyone else has read permissions only (4).
chown
chown is a command on Linux and Unix-like operating systems that is used to change the owner and/or group of a file or directory. The command is short for “change owner”.
On Linux and Unix-like operating systems, every file and directory is associated with an owner and a group. The owner is the user who created the file or directory, and the group is a set of users who share common access permissions for the file or directory. The chown command allows the owner and/or group of a file or directory to be changed to a different user and/or group.
The basic syntax of the chown command is as follows:
chown [OPTIONS] [OWNER][:[GROUP]] FILE
Here, OWNER is the user that you want to change the ownership to, and GROUP is the group that you want to change the group ownership to. If you omit GROUP, it will default to the user’s primary group. FILE is the file or directory whose ownership you want to change.
For example, to change the owner of a file named file.txt to a user named johndoe, you would run the following command:
chown johndoe file.txt
To change the group ownership of the same file to a group named users, you would run the following command:
chown :users file.txt
To change both the owner and group ownership of the file, you would run the following command:
chown johndoe:users file.txt
Note that chown can only be used by the superuser to change the ownership of files that they don’t own.
tar
tar is a command-line utility used for creating and manipulating archive files, which are collections of one or more files and/or directories stored as a single file. The name “tar” stands for “tape archive”, reflecting its origin as a tool for creating backups to tape drives.
The basic syntax of tar command is:
tar [options] [archive-filename] [files/directories to include in the archive]
Some common options include:
-c to create a new archive
-x to extract files from an existing archive
-v to display verbose output
-f to specify the name of the archive file
-z to use gzip compression
-j to use bzip2 compression
-r to append files to an existing archive
-t to display the contents of an archive
For example, to create a new tar archive called archive.tar containing two files (file1.txt and file2.txt), you could use the following command:
tar -cvf archive.tar file1.txt file2.txt
To extract the contents of an archive, use the -x option followed by the archive filename:
tar -xvf archive.tar
The files and directories contained within the archive will be extracted to the current working directory.
unzip
unzip is a Linux command used for extracting files from ZIP archives. It is a utility for uncompressing zip files, which are files compressed using the ZIP compression format. The unzip command is very useful for downloading and extracting large amounts of files or folders that have been zipped to reduce their size for transmission over the internet or storage.
The basic syntax of the unzip command is as follows:
unzip [options] archive.zip
Here, archive.zip is the name of the ZIP archive that you want to extract files from, and [options] are the various options that you can use with the unzip command.
Some commonly used options with the unzip command are:
-l: Lists the contents of the ZIP file without extracting them.
-d: Specifies the directory where the files should be extracted to.
-v: Verbose output, which displays detailed information about the files being extracted.
-x: Extracts only the files that match the specified pattern or list of patterns.
For example, to extract all the files from a ZIP archive called example.zip, you would use the following command:
unzip example.zip
This will extract all the files from the archive into the current directory. If you want to extract the files to a specific directory, you can use the -d option followed by the path to the directory, like this:
unzip example.zip -d /path/to/directory
This will extract all the files from the archive to the specified directory.
df
df is a command on Linux that is used to display the amount of disk space available and used on file systems. Here are some command line examples of df:
Display the disk space usage of all mounted file systems:
df -h
This will display the disk space usage in human-readable format, with sizes in GB, MB, or KB.
Display the disk space usage of a specific file system:
df -h /dev/sda1
This will display the disk space usage for the file system mounted on the device /dev/sda1.
Display the disk space usage of multiple file systems:
df -h /dev/sda1 /dev/sdb1
This will display the disk space usage for the file systems mounted on the devices /dev/sda1 and /dev/sdb1.
Display the disk space usage in a specific format:
df -T
This will display the disk space usage along with the file system type.
Display the disk space usage of a file system in 1K blocks:
df -k /dev/sda1
This will display the disk space usage for the file system mounted on the device /dev/sda1 in 1K blocks.
du
du stands for “disk usage” and is a command-line utility used on Linux to display the amount of disk space used by files and directories. It can be used to determine the size of files and directories, as well as to monitor and manage disk space usage.
The basic syntax of the du command is:
du [options] [files or directories]
Some commonly used options with the du command are:
-h or –human-readable: Display sizes in human-readable format, such as “10K”, “2M”, “1G”, etc.
-s or –summarize: Display only the total size of each specified file or directory.
-c or –total: Display a total for all sizes listed.
For example, to display the disk usage of a directory in human-readable format, you would use the command:
du -h /path/to/directory
To display the total disk usage of a directory and its subdirectories, you would use the command:
du -sh /path/to/directory
The output of the du command lists the size of each file or directory, followed by the name of the file or directory. By default, du lists the size in bytes, but with the -h option, it displays the size in a human-readable format. The du command can be useful for identifying large files or directories that may be taking up significant amounts of disk space.
ps
ps is a command-line utility that displays information about the currently running processes on a system. Here are some examples of how to use ps:
To display a list of all running processes, use the following command:
ps aux
This will show a detailed list of all processes currently running on the system, including the process ID (PID), the user that owns the process, the CPU and memory usage, and the command used to start the process.
To display a list of all processes started by a specific user, use the following command:
ps -u username
Replace “username” with the name of the user you want to filter by.
To display a list of all processes running on a specific terminal, use the following command:
ps -t ttyname
Replace “ttyname” with the name of the terminal you want to filter by.
To display a list of all processes running a specific command, use the following command:
ps -C command_name
Replace “command_name” with the name of the command you want to filter by.
To display a process tree of all processes running on the system, use the following command:
ps axjf
This will show a tree structure of all processes and their parent-child relationships.
top
top is a command-line utility used to display system resource usage in real-time. It displays information about CPU usage, memory usage, running processes, and more. Here are some examples of using top command:
To run top, simply type top into the command prompt and press enter:
top
This will launch top in its default mode, displaying a list of all the running processes sorted by the percentage of CPU they are using.
To sort the list of processes by a specific column, press the corresponding key. For example, to sort by memory usage, press the M key. To sort by CPU usage, press the P key.
To display a specific number of processes, use the -n option followed by the number of processes you want to see. For example, to display only the top 10 processes, type:
top -n 10
To display a process tree, which shows the relationship between processes, use the -H option:
top -H
To highlight the processes owned by a specific user, use the -u option followed by the username:
top -u username
To display a specific process, use the -p option followed by the process ID:
top -p pid
To continuously monitor system resource usage with top, use the -b option to run top in batch mode:
top -b
This will output the current system resource usage to the command prompt every second. To exit top, press the q key.
ping
ping is a command-line utility used to test the reachability of a network host or device and measure the round-trip time for packets sent from the source to the destination. Here are some examples of how to use ping:
Basic usage:
ping google.com
This will send packets to the IP address associated with the domain name google.com and report the results.
Specify the number of packets to send:
ping -c 5 google.com
This will send 5 packets to google.com and then stop.
Set the time interval between packets:
ping -i 2 google.com
This will send packets to google.com with a 2-second interval between each packet.
Set the packet size:
ping -s 1000 google.com
This will send packets to google.com with a packet size of 1000 bytes.
Enable continuous ping:
ping -t google.com
This will send packets to google.com continuously until interrupted with Ctrl+C.
Note that the exact usage and options available for ping may vary depending on the operating system and version of the utility.
curl
curl is a command-line tool used for transferring data to or from a server. It supports a wide range of protocols such as HTTP, HTTPS, FTP, FTPS, and many more. curl can be used to upload or download files, fetch web pages, and perform various other tasks. It is often used in shell scripts and automated tasks where data transfer is required. The basic syntax of curl is:
curl [options] [URL]
Some common options used with curl are:
-o: Specify the output file name
-O: Use the remote file name for the output file
-L: Follow redirects
-H: Specify a custom header
-d: Send data in the request body (POST)
-u: Specify the user name and password for authentication
-X: Specify the HTTP method (GET, POST, PUT, DELETE, etc.)
Here are a few examples of how curl can be used:
Download a file:
curl -O http://example.com/file.zip
Upload a file:
curl -F “file=@/path/to/file” http://example.com/upload
Send data in the request body:
curl -d “param1=value1¶m2=value2” http://example.com/api
Set a custom header:
curl -H “Authorization: Bearer token” http://example.com/api
Authenticate with a user name and password:
curl -u username:password http://example.com/api
wget
wget is a command-line utility used for downloading files from the web. Here are some examples of how to use wget:
Download a single file:
wget http://www.example.com/file.txt
Download multiple files using a pattern:
wget http://example.com/files/*.txt
Download a file and save it to a specific location:
wget -O /path/to/save/file.txt http://example.com/file.txt
Download a file and specify the download rate:
wget –limit-rate=100k http://example.com/file.txt
Download a file recursively (including all files and directories within it):
wget -r http://example.com/directory/
Download a file and resume a previously interrupted download:
wget -c http://example.com/file.txt
These are just a few examples of how wget can be used. For more information and options, you can use the man wget command in your terminal.
ssh
SSH, or Secure Shell, is a network protocol that allows you to securely connect to a remote system over an unsecured network.
Here are some examples of ssh commands:
To connect to a remote system using SSH:
ssh username@remotehost
This command will prompt you for your password before connecting to the remote system.
To forward a local port to a remote port using SSH:
ssh -L local_port:remote_host:remote_port username@remote_host
This command will forward the local port local_port to the remote port remote_port on the remote system remote_host.
To run a command on the remote system using SSH:
ssh username@remote_host command
This command will run the specified command on the remote system after connecting via SSH.
scp
scp is a command-line utility for securely copying files between local and remote hosts. It uses the Secure Shell (SSH) protocol for authentication and encryption, providing a secure way to transfer files over a network.
The basic syntax of the scp command is as follows:
scp [options] source destination
Here, source refers to the file or directory you want to copy, and destination is the location where you want to copy the files. The options are used to modify the behavior of the command and can include options like -r to copy directories recursively or -p to preserve the file attributes.
For example, to copy a file named example.txt from your local machine to a remote host with the IP address 192.168.1.100, you would use the following command:
scp example.txt [email protected]:/path/to/destination/
Here, user is the username you use to log in to the remote host, and /path/to/destination/ is the location where you want to copy the file. If you want to copy a directory recursively, you would add the -r option, like this:
scp -r mydirectory [email protected]:/path/to/destination/
This would copy the mydirectory directory and all its contents to the remote host. When you use scp to copy files, you will be prompted to enter your password to authenticate the connection.
sudo
sudo is a command used in Unix/Linux operating systems to give users with the necessary permissions the ability to execute commands with elevated privileges. The term “sudo” stands for “superuser do,” and it allows authorized users to execute commands as the root user or another user with administrative privileges.
The sudo command is typically used before a command that requires elevated privileges, and it will prompt the user to enter their password to confirm their identity. Once the user has been authenticated, the command will be executed with the appropriate permissions.
For example, if a regular user needs to install software that requires root access, they can use the sudo command to run the installation command with elevated privileges. The command would look like this:
sudo apt install package-name
This would prompt the user to enter their password, and if authenticated, the installation would proceed with root privileges.
apt
apt is a command-line tool used on Linux-based operating systems (such as Ubuntu, Debian, and others) to manage software packages. It allows users to install, update, remove, and search for packages, as well as handle dependencies and conflicts between packages.
apt is short for “Advanced Package Tool” and is a powerful package management utility that simplifies the process of installing and maintaining software on a Linux system.
Some commonly used commands with apt are:
To update the package list from the repositories:
sudo apt update
To upgrade all installed packages to their latest versions:
sudo apt upgrade
To install a package:
sudo apt install <package_name>
For example, to install the text editor “nano”:
sudo apt install nano
To remove a package:
sudo apt remove <package_name>
For example, to remove the text editor “nano”:
sudo apt remove nano
To search for a package:
apt search <search_term>
For example, to search for packages related to Python:
apt search python
These are just a few examples of the many things you can do with apt on the command line.
yum
yum is a package management utility used in some Linux distributions such as CentOS, Fedora, and Red Hat Enterprise Linux. It is used to install, update, and remove packages and their dependencies. Here are some examples of yum command line usage:
To install a package:
sudo yum install <package_name>
For example:
sudo yum install nginx
To update all packages:
sudo yum update
To remove a package:
sudo yum remove <package_name>
For example:
sudo yum remove nginx
To search for a package:
sudo yum search <package_name>
For example:
sudo yum search apache
To list installed packages:
sudo yum list installed
To list available updates:
sudo yum check-update
service
service is a command used to control and manage system services on Linux. It can start, stop, and restart services, as well as display the status of running services. Here are some examples:
To start the Apache web server service:
sudo service apache2 start
To stop the Apache web server service:
sudo service apache2 stop
To restart the Apache web server service:
sudo service apache2 restart
To display the status of the Apache web server service:
sudo service apache2 status
systemctl
systemctl is a command-line utility on Linux systems that provides control and management of the system’s systemd system and service manager. It allows administrators to manage and control the state of system services and other aspects of the system.
Here are some examples of systemctl commands with their functionalities:
To check the status of a service:
systemctl status <service_name>
To start a service:
systemctl start <service_name>
To stop a service:
systemctl stop <service_name>
To restart a service:
systemctl restart <service_name>
To enable a service to start automatically at boot time:
systemctl enable <service_name>
To disable a service from starting automatically at boot time:
systemctl disable <service_name>
To list all available units:
systemctl list-unit-files
This will list all the available units on your system.
To list all active units:
systemctl list-units
This will list all the active units on your system.
To reload the systemd configuration:
systemctl daemon-reload
This will reload the systemd configuration and update the system’s state.
These are just a few examples of the many functionalities of systemctl. For more information on the systemctl command, you can refer to the man page by typing man systemctl in the terminal.
Conclusion
In conclusion, the Linux command line interface can seem intimidating at first, but it is an important tool for anyone who wants to work efficiently with the operating system. The 30 commands we’ve listed here are just the tip of the iceberg when it comes to what you can do with Linux, but they are a good starting point for beginners. As you become more comfortable with the Linux command line, you can explore more advanced commands and techniques to take your skills to the next level.