Linux Commands
Linux commands are an essential part of using the Linux operating system. Linux is a command-line-based system, which means that most tasks are executed by typing commands into a terminal. These commands can be used for a variety of purposes, including managing files and directories, installing software, networking, and system administration.
This article introduces some of the most commonly used Linux commands.
pwd
The pwd
command in Linux stands for "Print Working Directory" and is used to display the current working directory in the terminal. The working directory is the directory where you are currently located or where your command prompt is pointing to.
Here is an example of how to use the pwd
command:
$ pwd
When you run this command, it will display the full path of the current working directory in the terminal. For example, if you are in the home directory of the user "john", the output would be:
/home/john
The pwd
command can be useful in many situations, such as when you need to know the current directory for a script or when you are navigating through the file system and want to confirm your current location.
cd
The Linux cd
command is used to navigate the file system by changing the current working directory. This command allows users to move between directories and access different files and directories on their system.
Suppose you want to navigate to the "Documents" directory. Here's how you can do it using the cd
command:
$ cd Documents/
This will change your current working directory to the "Documents" directory. Now, you can view the files and subdirectories within this directory.
If you want to navigate back to the previous directory, you can use the following command:
$ cd ..
This will change your current working directory to the parent directory of the current directory. You can continue to use the cd
command to navigate through the file system and access the files and directories you need.
ls
The Linux ls
command is used to list the files and directories in the current working directory.
Suppose you want to list the files and directories in the current working directory. Here's how you can do it using the ls
command:
$ ls
This will display a list of all the files and directories in the current working directory.
If you want to display additional information about the files and directories, you can use the following command:
$ ls -l
This will display a long listing of the files and directories in the current working directory, including the permissions, owner, size, and date modified.
You can also use the ls
command to list the files and directories in a specific directory. For example, to list the files and directories in the "Documents" directory, you can use the following command:
$ ls Documents/
This will display a list of all the files and directories in the "Documents" directory.
cp
The Linux cp
command is used to copy files and directories from one location to another.
Suppose you want to copy a file called "file1.txt" from the current working directory to a directory called "Documents". Here's how you can do it using the cp
command:
$ cp file1.txt Documents/
This will create a copy of "file1.txt" in the "Documents" directory.
If you want to copy an entire directory and its contents to a new location, you can use the following command:
cp -r directory1/ Documents/
This will create a copy of "directory1" and its contents in the "Documents" directory.
You can also use the cp
command to overwrite an existing file with a new file. For example, if you want to replace an existing file called "file1.txt" with a new file called "file2.txt", you can use the following command:
$ cp file2.txt file1.txt
This will overwrite "file1.txt" with "file2.txt".
mv
The Linux mv
command is used to move or rename files and directories on the file system.
Suppose you want to rename a file called "file1.txt" to "file2.txt". Here's how you can do it using the mv command:
$ mv file1.txt file2.txt
This will rename "file1.txt" to "file2.txt".
If you want to move a file called "file1.txt" from the current working directory to a directory called "Documents", you can use the following command:
$ mv file1.txt Documents/
This will move "file1.txt" to the "Documents" directory.
You can also use the mv
command to move an entire directory and its contents to a new location. For example, if you want to move a directory called "directory1" and its contents to a new location called "Documents", you can use the following command:
$ mv directory1/ Documents/
This will move "directory1" and its contents to the "Documents" directory.
rm
The Linux rm
command is used to remove files and directories from the file system.
Suppose you want to delete a file called "file1.txt" from the current working directory. Here's how you can do it using the rm
command:
$ rm file1.txt
This will delete "file1.txt" from the current working directory.
If you want to delete an entire directory and its contents, you can use the following command:
$ rm -r directory1/
This will delete "directory1" and its contents from the file system.
You can also use the rm command to delete multiple files at once. For example, if you want to delete all files with the ".txt" extension in the current working directory, you can use the following command:
$ rm *.txt
This will delete all files with the ".txt" extension in the current working directory.
It is important to note that the rm
command permanently deletes files and directories from the file system, so use it with caution.
mkdir
The Linux mkdir
command is used to create new directories on the file system.
Suppose you want to create a new directory called "Documents" in the current working directory. Here's how you can do it using the mkdir
command:
$ mkdir Documents
This will create a new directory called "Documents" in the current working directory.
You can also create multiple directories at once using the mkdir command. For example, if you want to create two directories called "Documents" and "Pictures" in the current working directory, you can use the following command:
$ mkdir Documents Pictures
This will create two new directories called "Documents" and "Pictures" in the current working directory.
If you want to create a directory with a specific path, you can use the following command:
$ mkdir /home/user/Documents
This will create a new directory called "Documents" in the "/home/user" directory.
cat
The Linux cat
command is used to concatenate and display the contents of one or more files on the terminal.
Suppose you want to view the contents of a file called "file1.txt" on the terminal. Here's how you can do it using the cat
command:
$ cat file1.txt
This will display the contents of "file1.txt" on the terminal.
If you want to view the contents of multiple files, you can use the following command:
$ cat file1.txt file2.txt
This will display the contents of both "file1.txt" and "file2.txt" on the terminal.
You can also use the cat command to create a new file or append to an existing file. For example, if you want to create a new file called "file3.txt" and add some text to it, you can use the following command:
$ cat > file3.txt
This will open a new line where you can enter text. Once you are finished, you can press Ctrl+D to save the file and exit.
touch
The Linux touch
command is used to create a new file or update the modification and access timestamps of an existing file. It is a simple tool that allows users to modify timestamps on files or create new empty files.
Suppose you want to create a new file called "file1.txt" in the current working directory. Here's how you can do it using the touch
command:
$ touch file1.txt
This will create a new empty file called "file1.txt" in the current working directory.
If you want to modify the timestamps of an existing file, you can use the following command:
$ touch file1.txt
This will update the modification and access timestamps of "file1.txt" to the current time.
You can also use the touch command to create multiple files at once. For example, if you want to create three new files called "file1.txt", "file2.txt", and "file3.txt" in the current working directory, you can use the following command:
$ touch file1.txt file2.txt file3.txt
This will create three new empty files in the current working directory.
chmod
The Linux chmod
command is used to change the file mode (permissions) of a file or directory. It is a simple tool that allows users to modify the permissions on files and directories to control who can read, write, and execute them.
Suppose you want to allow the owner of a file called "file1.txt" to read and write the file, but not allow anyone else to access it. Here's how you can do it using the chmod
command:
$ chmod 600 file1.txt
This will set the file mode of "file1.txt" to 600, which means the owner can read and write the file, but all other users will have no permissions.
If you want to allow the owner and a specific group to access the file, you can use the following command:
$ chmod 640 file1.txt
This will set the file mode of "file1.txt" to 640, which means the owner can read and write the file, users in the same group can read the file, but all other users will have no permissions.
You can also use the chmod command to change the permissions of multiple files at once or to modify the permissions recursively on directories and subdirectories.
chown
The Linux chown
command is used to change the owner and group of a file or directory. It is a simple tool that allows users to modify the ownership of files and directories to control who has access to them.
Suppose you want to change the owner of a file called "file1.txt" to a user named "alice". Here's how you can do it using the chown
command:
$ chown alice file1.txt
This will change the owner of "file1.txt" to "alice". If you want to change the group as well, you can use the following command:
$ chown alice:users file1.txt
This will change the owner of "file1.txt" to "alice" and the group to "users".
You can also use the chown command to change the ownership of multiple files at once or to modify the ownership recursively on directories and subdirectories.
apt-get
The Linux apt-get
command is a package management utility used to install, update, and remove software packages on Debian and Ubuntu-based systems.
Suppose you want to install the Apache web server on your Ubuntu system. Here's how you can do it using the apt-get
command:
$ apt-get install apache2
This will download and install the Apache web server along with its dependencies.
You can also use the apt-get
command to update your system packages:
$ apt-get update
This will download the latest package information from the configured repositories.
And to upgrade your system packages to the latest version:
$ apt-get upgrade
This will upgrade all the installed packages on your system to the latest version available in the configured repositories.
The apt-get command has many other options and capabilities that can help users manage their software on Linux systems effectively.
systemctl
The Linux systemctl
command is a system utility used to manage system services and daemons on modern Linux distributions that use the systemd system and service manager.
Suppose you want to start the Apache web server on your Linux system. Here's how you can do it using the systemctl
command:
$ systemctl start apache2
This will start the Apache web server service.
You can also use the systemctl command to stop a service:
$ systemctl stop apache2
This will stop the Apache web server service.
And to restart a service:
$ systemctl restart apache2
This will stop and then start the Apache web server service.
The systemctl
command also provides many other options and capabilities that allow users to manage their system services effectively, such as enabling or disabling services, checking the status of services, and inspecting the system log messages related to a specific service.
top
The Linux top
command is a system monitoring utility used to display information about the system's resource usage, including CPU, memory, and disk usage.
Here is an example of how to use the top
command:
When you run the top command in your terminal, you will see a real-time display of system resource usage, including CPU, memory, and disk usage.
$ top
This will display a list of all running processes on your system sorted by their CPU usage. The top
command also provides various options and interactive commands that allow users to sort and filter the process list, change the update interval, and adjust the display format.
One useful feature of the top
command is its ability to display a summary of system resource usage at the top of the screen. This summary includes information about the total number of processes, CPU usage, memory usage, and swap space usage.
The top
command also provides options to customize the display and output format, such as saving the output to a file or sorting the process list by a specific criteria.
ps
The Linux ps
command is a utility used to display information about the running processes on a Linux system. It is a powerful tool that allows users to view the status of all active processes, including their PID (Process ID), CPU usage, memory usage, and other system resources.
Here is an example of how to use the ps
command:
$ ps -ef
This command will display a list of all running processes on your Linux system, including their PID, parent process ID, CPU usage, and other information. The "-ef" option is used to display all processes in full detail and in a tree-like structure.
Another useful option of the ps
command is "-aux", which displays a list of all processes running on the system, including those owned by other users. This can be helpful for system administrators who need to identify processes that are consuming too many resources or causing system performance issues.
$ ps -aux
The ps
command also provides options to filter and sort the process list based on various criteria, such as CPU usage, memory usage, process name, and more.
$ ps -ef --sort=-%cpu | head -n 10
This command will sort the process list based on CPU usage in descending order and display the top 10 processes with the highest CPU usage.
kill
The Linux kill
command is a utility used to terminate a running process on a Linux system.
Here is an example of how to use the kill
command:
$ kill 1234
This command will send a termination signal to the process with PID 1234, causing it to stop running.
Another useful option of the kill command is "-9", which sends a forceful termination signal to the process, terminating it immediately.
$ kill -9 1234
The kill
command also provides options to specify the type of signal to send to the process, such as SIGTERM (terminate signal) or SIGKILL (forceful termination signal).
$ kill -SIGTERM 1234
This command will send a SIGTERM signal to the process with PID 1234, causing it to terminate.
tar
The tar
command in Linux is a tool used for archiving and compressing files and directories. It creates a single file, often called a tarball, that contains all the files and directories that you want to archive. The tar
command can also be used to compress the tarball with different algorithms such as gzip or bzip2 to reduce its size.
Here's an example of how to use the tar
command to create a tarball of a directory:
$ tar -cvf myfiles.tar mydirectory/
In this example, the -c
option tells tar
to create a new archive, the -v
option is used to print the names of the files and directories as they are being archived, and the -f
option specifies the name of the archive file to create. myfiles.tar
is the name of the tarball that will be created, and mydirectory/
is the directory that you want to archive.
Once the tarball is created, you can extract its contents using the tar
command with the -x
option:
$ tar -xvf myfiles.tar
In this example, the -x
option tells tar
to extract the contents of the archive, and the -f
option specifies the name of the archive file to extract. myfiles.tar
is the name of the tarball that you want to extract.
The tar
command is a powerful tool with many options and features. You can explore more of them by reading the manual pages or online resources.
grep
The grep
command in Linux is used to search for specific patterns of text within files or output from other commands. It is a very powerful tool for searching through large amounts of text.
Here is an example of how to use the grep
command to search for a pattern within a file:
$ grep "pattern" filename.txt
In this example, pattern is the text string you want to search for within the filename.txt file.
The grep
command can also be used to search for a pattern within the output of another command. For example, you could use the ps command to list all running processes on your system, and then use grep
to filter the output to only show processes containing a specific keyword:
$ ps aux | grep "keyword"
In this example, ps aux
lists all running processes on your system, and the | (pipe)
symbol is used to send the output of the ps command to the grep command. keyword is the text string you want to search for within the output of the ps
command.
The grep command also supports a wide range of options and can be used for more advanced search operations, such as searching for patterns using regular expressions.
sed
The sed
command in Linux is used to manipulate text. It is a stream editor that can perform text transformations on an input stream (a file or output from another command) and then output the result.
Here is an example of how to use the sed
command to replace a specific text string within a file:
$ sed 's/old_string/new_string/' filename.txt
In this example, old_string
is the text string you want to replace, and new_string is the text string you want to replace it with. filename.txt
is the file that you want to perform the replacement on.
The sed
command can also be used to delete specific lines from a file:
$ sed '3d' filename.txt
In this example, the 3d
command tells sed to delete the third line of filename.txt
.
The sed
command also supports regular expressions and can be used to perform more complex text transformations.
awk
The awk
command is a versatile tool used for text processing and data extraction in Linux. It reads data either from a file or from the output of another command and performs operations on it based on patterns.
Here is an example of how to use the awk
command to print the first column of a file:
$ awk '{print $1}' filename.txt
In this example, $1
represents the first column of the file. The awk command reads each line of the file and prints the first column.
The awk
command can also be used to perform arithmetic operations on columns of data:
$ awk '{total += $3} END {print total}' filename.txt
In this example, the awk
command reads the third column of filename.txt and adds up all the values. At the end, it prints the total.
The awk
command also supports regular expressions and can be used to search for and manipulate specific patterns in the data.
curl
The curl
command is a popular Linux tool used to transfer data to and from a server using various protocols like HTTP, FTP, SMTP, and many more. The command allows users to make requests and receive responses from a server through the command line interface.
Here is an example of how to use the curl
command to retrieve the HTML content of a website:
$ curl https://www.example.com
In this example, the curl
command is used to send an HTTP GET request to https://www.example.com
. The server responds with the HTML content of the website, which is displayed in the terminal.
The curl
command can also be used to send data to a server, such as submitting a form:
$ curl -d "username=john&password=secret" https://www.example.com/login
In this example, the -d
flag is used to specify the data to be sent to the server in the form of key-value pairs. The curl command sends a POST request to https://www.example.com/login
with the specified data. The server then processes the data and responds with a new page or a message indicating the success or failure of the request.
wget
The wget
command is a widely used Linux command that is used to download files from the internet. It supports various protocols such as HTTP, HTTPS, and FTP. With wget
, you can download a single file, multiple files, or even entire directories.
To download a single file using wget
, you simply need to provide the URL of the file you want to download. For example, to download a file named "example.zip" from a website, you would use the following command:
$ wget http://example.com/example.zip
To download multiple files or all files in a directory, you can use the -r
option. For example, to download all files in a directory named "docs" from a website, you would use the following command:
$ wget -r http://example.com/docs/
You can also use the -P
option to specify the directory where you want to save the downloaded files. For example, to download all files in a directory named "docs" and save them in a directory named "my-docs", you would use the following command:
$ wget -r -P my-docs http://example.com/docs/