Linux is a popular open-source operating system that is widely used for servers, desktop computers, and mobile devices. It is known for its stability, security, and flexibility, and it is the foundation for many other operating systems.
One of the benefits of using Linux is that it provides access to a wide range of powerful command-line tools. These tools allow users to perform a variety of tasks, such as managing files, installing software, and configuring the system.
In this article, we will introduce the top ten commands in Linux that every user should know, along with examples of how to use them.
pwd
: This command stands for “print working directory” and it displays the path of the current directory.
To use the pwd
command, simply open a terminal window and type pwd
, then press Enter.
$ pwd
/home/user/documents
The pwd
command is useful when you want to know the location of the current directory. It can be especially helpful when you are working with multiple directories and need to keep track of your location in the file system.
cd
: This command stands for “change directory” and it allows you to navigate the file system.
To use the cd
command, type cd
followed by the path of the directory you want to navigate to. For example, to navigate to the documents
directory, you would type:
$ cd documents
$ cd /home/user/downloads
You can also use the cd
command to navigate up one directory level by using the ..
notation. For example, to navigate to the parent directory of the current directory, you would type
$ cd ..
The cd
command is essential for navigating the file system and accessing different directories and files.
ls
: This command stands for “list” and it displays the contents of a directory.
To use the ls
command, type ls
followed by the path of the directory you want to list. For example, to list the contents of the documents
directory, you would type:
$ ls documents
You can also use the ls
command with various options to customize the output. For example, the -l
option displays the contents in a long format, showing additional information such as file permissions and timestamps. The -a
option includes hidden files in the listing
$ ls -l documents
total 56
drwxr-xr-x 2 user user 4096 Jan 1 1970 documents
-rw-r--r-- 1 user user 12292 Jan 1 1970 report.pdf
$ ls -a documents
. .. documents report.pdf
$ ls
file1.txt file2.txt directory1
The ls
command is useful for quickly viewing the contents of a directory and obtaining information about the files and subdirectories it contains.
mkdir
: This command stands for “make directory” and it allows you to create new directories.
To use the mkdir
command, type mkdir
followed by the name of the directory you want to create. For example, to create a new directory called project
, you would type:
$ mkdir project
For example, to create a new directory called “project” inside the “documents” directory, you can use the following command:
$ mkdir documents/project
You can also use the -p
option to create intermediate directories as needed. For example, the following command creates the directories “documents/project/subproject” if they do not already exist:
$ mkdir -p documents/project/subproject
cp
: This command stands for “copy” and it allows you to copy files and directories.
For example, to copy a file called “report.txt” from the “documents” directory to the “backup” directory, you can use the following command:
$ cp documents/report.txt backup/
6. The rmdir
command in Linux is used to remove directories, also known as folders. It can only be used to delete empty directories, as it will not delete directories that contain files or other directories.
To use the rmdir
command, you can type rmdir
followed by the name of the directory you want to delete. For example:
rmdir -p directory/subdirectory
It is important to use the rmdir
command with caution, as it is not possible to recover deleted directories. If you want to delete a directory and all of its contents, you can use the rm
command with the -r
option, which stands for “recursive.”
rm -r directory_name
This will delete the specified directory and all of its contents, including any subdirectories and files.
7. The chmod
command in Linux stands for “change mode” and it is used to change the permissions of a file or directory. Permissions determine which users can access and modify a file or directory.
In Linux, there are three types of permissions: read (r), write (w), and execute (x). These permissions can be set for the owner of the file, the group owner of the file, and all other users.
The chmod
command allows you to specify which permissions to set for which users, using either symbolic or numeric notation.
Here is an example of using the chmod
command with symbolic notation:
chmod u+x file.txt
This command adds execute permission for the owner (u) of the file file.txt
.
Here is an example of using the chmod
command with numeric notation:
chmod 755 directory
This command sets read, write, and execute permissions for the owner (7) and read and execute permissions for the group owner and all other users (55) of the directory.
The chmod
command can be used to set permissions for multiple files or directories at once by specifying them as arguments. For example:
chmod 644 file1.txt file2.txt file3.txt
This command sets read and write permissions for the owner (6) and read permission for the group owner and all other users (44) for the files file1.txt, file2.txt, and file3.txt. I hope this helps! Let me know if you have any questions.
8. The grep
command in Linux is a tool for searching for patterns in text files. It stands for “global regular expression print.” grep
searches for a specified pattern in a given input file and prints any lines that match the pattern.
Here is the basic syntax for using grep
:
grep pattern input-file
For example, if you have a text file called “log.txt” and you want to search for all lines containing the word “error,” you can use the following command:
grep error log.txt
This will print all lines in “log.txt” that contain the word “error.”
grep
can also be used with multiple input files. For example, if you want to search for the word “error” in both “log1.txt” and “log2.txt,” you can use the following command:
grep error log1.txt log2.txt
grep
also supports the use of regular expressions, which are special strings that can match complex patterns. For example, if you want to search for lines that contain a three-digit number, you can use the following regular expression
grep '[0-9][0-9][0-9]' input-file
This will print all lines in “input-file” that contain a three-digit number.
9. The find
command in Linux is a utility that allows you to search for files and directories based on various criteria, such as name, size, date modified, and permissions. It is commonly used to locate specific files or to search for files that match certain criteria.
Here is an example of how to use the find
command:
find / -name "example.txt"
This command will search for a file named “example.txt” starting from the root directory (“/”). The find
command will search through all subdirectories and display the path of any files that match the search criteria.
You can also use the find
command to search for files based on other criteria, such as size, date modified, and file type. For example:
find / -size +100M
This command will search for files larger than 100 megabytes starting from the root directory.
find / -mtime +30
This command will search for files (as opposed to directories) starting from the root directory.
You can also use the find
command in combination with other commands, such as rm
to delete matching files or chmod
to change the permissions of matching files.
For more information on the find
command and its options, you can refer to the manual pages by using the man
command:
man find
10. The cat
command in Linux is a utility that is used to concatenate (combine) and display the contents of one or more files. It is also frequently used to create new files, view the contents of files, and redirect output from one command to another.
Here is an example of using the cat
command to view the contents of a file:
cat file.txt
This will display the contents of the file.txt
file on the terminal.
Here is an example of using the cat
command to create a new file and add some text to it:
cat > newfile.txt
This is the first line of text in the new file.
This is the second line of text in the new file.
To save the file and exit the cat
command, press CTRL+D
.
Here is an example of using the cat
command to concatenate the contents of two files and display the result on the terminal:
cat file1.txt file2.txt
This will display the contents of file1.txt
followed by the contents of file2.txt
.
The cat
command has many options and arguments that allow you to customize its behavior. For example, the -n
option can be used to display line numbers, and the -b
option can be used to display line numbers for non-empty lines only. You can use the man cat
command to view the manual pages for the cat
command and learn about all of its available options.