10 Linux Commands You Should Know
Linux offers a wide range of commands to perform almost every operation on your system from the terminal. Using these commands, you can navigate between directories, monitor system resources, analyze networks, etc. Moreover, these commands save a lot of your time by removing the necessity of clicking through menus.
Keeping the significance of Linux commands in mind, I compiled this guide to illustrate some basic Linux commands that I use regularly to perform my routine tasks.
1. ls | List Directories
The ls command returns the list of all available files and directories on your system. We can run this command with or without any options:
ls [Options]
The ls command can accept one of the following options:
| Flag | Description |
|---|---|
| -a | Lists all files. |
| -F | Appends one of the following characters to file names (*/=>@|). These characters indicate file type, such as * for executable, / for directory, @ for symlinks, etc. |
| -lh | Returns the file/directory sizes in human-readable format, such as KB, MB, etc. |
| -l | l stands for long format. We can use this option to get a detailed output, including file permissions, creation date, etc. |
| -L | Lists actual files/directories when following a symbolic link. |
| -r | Shows the output in reverse order. |
| -R | List all data recursively, including files, directories, and subdirectories. |
| -t | Sorts files with respect to the latest modified time. |
| -X | Sort files in alphabetical order with respect to their extensions. |
| -1 | Retrieves all files in one column. One file per line. |
Let’s run the ls command without any options to see how it works:
ls
Here, blue names represent folders and white shows filenames:
The ls command lists the content of your working directory. So, it’s recommended to check your current working directory before using ls to avoid potential conflicts:
pwd
To list the content of a specific directory, you can specify the absolute path of the target directory with the ls command:
ls Pictures
Executing the ls command with the -a option will list the hidden files as well:
ls -a
Here, the filenames starting with a dot represent the hidden files or folders:
To list files with detailed information, we can run the ls command with the -l option:
ls -l
You can list folders’ content recursively using the -R option. It lists all the folders along with their subfolders and files:
ls -R
2. mkdir | Create a New Directory
mkdir or make directory is a Linux command that lets us create a new directory at the current location or a specific path:
mkdir [options] directoryName
To create multiple folders using mkdir, we need to specify the directory names with a space-separated syntax:
mkdir [options] directoryName1 directoryName2 directoryName3 …
We can replace options with different flags to achieve a particular purpose, such as creating a parent directory, a directory with specific permissions, as shown below:
| Option | Description |
|---|---|
| -p | It lets us create a parent directory. |
| -m a=[permissions][directoryName] | It enables us to specify permissions for the newly created directory. |
| -v | Shows a confirmation message for directory creation. |
Let’s create a new directory named Linuxgenie by executing:
mkdir Linuxgenie
We can verify the directory creation with the ls -l command:
When we run the mkdir command without any arguments, it creates a directory and silently moves the cursor to the next line, indicating that the directory has been created successfully. We can explicitly show the confirmation message by specifying the -v option with the mkdir command:
mkdir -v LG_Examples
If we try to create an already existing directory, mkdir returns the following error:
Similarly, we can use mkdir to create a directory at a specific path. To do this, we need to specify the complete directory path where we want to create a new directory:
mkdir Linuxgenie/examples
Use the -p option with the mkdir to create a parent directory:
mkdir -p genie/example1/example2/example3
Now use the ls -R command to list all the subdirectories recursively:
ls -R
The output shows that mkdir creates a directory tree:
- genie is a parent directory
- example1 is a subdirectory of genie ⇒ genie/example1
- example2 is a subdirectory of example1 ⇒ genie/example1/example2
- And example3 is a subdirectory of example2⇒ genie/example1/example2/example3:
By default, mkdir creates a directory with read, write, and execute permissions for the current user only. To create a new directory with specific permissions, run the mkdir command with the -m option as follows:
mkdir -m777 genieExamples
This command creates a directory named genieExamples and assigns read, write, and execute permission to all users:
3. touch | Create File or Update File’s Timestamp
The touch command creates an empty file if it doesn’t exist already and updates the timestamp if the file already exists. It works with or without any options:
touch [options] fileName
The touch command can accept the following options:
| Option | Description |
|---|---|
| -a | Modifies the access time. |
| -c, –no-create | It does not create a new file. However, if a file already exists, it updates the file’s timestamp. |
| -d=string, –date=string | Updates the access and modification time according to the provided date string. |
| -m | It updates only the modification time. |
| -r=<file>, –reference=<file> | Changes the file’s access and modification time based on the referenced file. |
| -t <stamp> | Specifies a particular timestamp instead of the current timestamp. |
Let’s create an empty file named genieExample.txt using the touch command:
touch genieExample.txt
If we create a file with the same name, it will update the file’s timestamp to the current timestamp:
touch genieExample.txt
The file permissions and its content remain the same, only the timestamp will be updated:
We can specify as many file names as we want to create using a space-separated syntax:
touch exampleFile genieData sampleFile
To create a file with a particular timestamp, you must use the following syntax:
touch -t YYYYMMDDHHMMSS fileName
In the following example, we create a file named linuxExample on the timestamp “202101030910”, where the first four digits from the left represent a year, the next two indicate the month, and the following two show the day. The next part represents time, where 09 represents hours, and 10 indicates minutes:
touch -t 202101030910 linuxExamples
Tip: You can also use the redirect operator “>” to create an empty file:
> emptyfile.txt
4. cat | Read, View, or Concatenate Text Files
cat is a short form of concatenate, which lets us concatenate, show, copy, and manipulate files’ content. The cat command uses the following syntax:
cat [options] [fileName]
The cat command can accept several options to perform a specific task, as discussed in the following table:
| Option | Description |
|---|---|
| -A | Displays all characters. |
| -b | It creates a numbered list and doesn’t contain empty lines. |
| -e | It returns a $ sign instead of showing non-printing characters and end lines. |
| -E | Puts a $ symbol at the end of every single line. |
| -n | It creates a numbered list, including blank lines. |
| -s | Deletes repeating blank lines from the output. |
Let’s execute the cat command to show the text of the exampleFile:
cat exampleFile
We can also use the cat command to create a new file and add content to it immediately:
cat > linuxDistros.txt
Press Enter to add content to the newly created file and Ctrl+D to stop adding more content:
Similarly, we can use the cat command to copy the content of one or more files to another file. For example, the following command copies the content of exampleFile and linuxDistros.txt files to a new file named concatenatedContent.txt:
cat exampleFile linuxDistros.txt > concatenatedFile.txt
We can run the cat command with the >> sign to add the data of one file to the end of another. The following example adds the content of the empBio.txt file to the end of the concatenatedFile.txt:
cat concatenatedFile.txt >> empBio.txt
Sometimes a file contains unnecessary repeated blank lines. We can trim those lines using the cat command as follows:
cat -s genie.txt
The following screenshot shows that the genie.txt file contains multiple blank lines in the output. However, using the -s option trimmed all the repeated blank lines:
Tip: You can use the echo command to print a string on the terminal.
5. rm | Remove Files or Directories
The rm command allows us to remove unnecessary files from our Linux system. However, we can execute this command with the -r option to remove empty or non-empty directories:
rm [option] [fileName or directoryName]
Here are some frequently used flags that can be used with the rm command to achieve different functionalities:
| Option | Description |
|---|---|
| -d, –dir | It lets us delete empty directories. |
| -f, –force | Ignores non-existent files and folders and never prompts before deletion. |
| -i | Confirm before deletion. |
| -I | Prompts before removing recursively or deleting more than three files. |
| -r, -R, –recursive | Removes the directory’s content recursively. |
| -v | Retrieves a confirmation message for the file or directory removal. |
Let’s run the rm command to delete some files:
rm -v genie genieData genieContent
Execute the “rm -r” command to remove a directory. Otherwise, you’ll encounter “rm: cannot remove directoryName: Is a directory”:
Let’s run rm with -r to remove a directory named “genieExamples”:
rm -rv genieExamples
You can use the rm command with the -i option to confirm the file or directory deletion. This practice avoids accidental data loss:
rm -r -i -v exampleDir
Press y to confirm the deletion:
Tip: You can use the rmdir command to delete one or more empty directories.
6. cd | Navigate Between Directories
cd (change directory) is a command-line utility in Linux that allows you to traverse between various directories. Using this command, we can access a specific directory, hidden directories, navigate back to the home directory, etc.
cd [options] [directoryName]
Here, the option can be -L or -p. The -L option is by default enabled and forces the symlink to be followed. However, if you use -p option, it ensures the cd command uses the physical directory structure without following the symlinks.
To access a specific directory, use the cd command followed by the directory to be accessed:
cd Desktop
Let’s provide the complete directory path with the cd command to access a particular directory:
cd Linuxgenie/examples
To move back to your home directory, execute cd without any flags or with the tilde sign (cd ~):
cd
Use the cd command with the hyphen sign “-” to navigate back one directory:
cd –
Run the cd .. command to navigate from CWD to the parent directory:
cd ..
Hidden files start with a dot. So, to access a hidden file or directory, you must specify a dot before the file to be accessed:
cd .cache
7. cp | Copy Files or Directories
The cp command stands for copy file. It lets us copy a single or multiple files/folders from the source to the provided path. It is used to transfer data, take backups, and organize the directory structure on a system:
cp [options] [source_file] [destination]
If a file with the same name exists, the cp command overwrites the existing file’s content with the source file. However, if the destination file doesn’t exist, the cp command first creates a new file and then copies the content of the source file to the destination.
Here are some commonly used options that modify the working of the cp command:
| Option | Description |
|---|---|
| -f, –force | If the destination file doesn’t open, it removes that file and tries to reopen it. |
| -i, –interactive | Prompt confirmation message before overwriting. |
| -r, -R, –recursively | Recursively copies the directories’ content. |
| -s, –symbolic-link | Creates a symbolic link. |
Suppose we have a file named genie.txt whose content is shown in the following snippet:
Let’s copy this content to another file named genieDuplicate.txt using the cp command:
cp genie.txt genieDuplicate.txt
Similarly, we can copy a file to a directory using the cp command as follows:
cp genie.txt genieDuplicate.txt Documents
This command copies genie.txt and genieDuplicate.txt and pastes them to the Documents folder:
To copy one directory to another, we need to specify the -r option with the cp command:
cp -r Linuxgenie Desktop
8. mv | Move or Rename Files and Folders
The mv command enables you to move files and directories from one folder to another. Moreover, it helps us rename a file/directory. Follow the syntax below to use this command in Linux:
mv [options] [fileName or directoryName]
The mv command accepts a -f option to overwrite the destination files without notifying the users and an -i option to confirm before overwriting a file at the destination.
In this example, we use the mv command to move the genie.txt and empBio.txt files to the Desktop:
mv genie.txt empBio.txt Desktop
Similarly, we can rename a file using the mv command as follows:
mv -v empBio.txt empInfo.txt
It renames an already existing file named empBio.txt to empInfo.txt:
9. Chmod | Change File Permissions
chmod, or change mode, is a Linux command that allows us to change the access permissions of the given file. A file can have the following permissions: read(r), write(w), and execute(x). The basic syntax of this command is:
chmod [options] [mode] [fileName]
Here, you can run the chmod command with the following options:
| Options | Description |
|---|---|
| -c, –changes | Shows the message only when permissions are changed. |
| -f, –silent, –quiet | Skips error messages silently. |
| -h | Modifies the permissions of symlinks rather than the original files. |
| -R, –recursive | Change permissions recursively. |
| -v, verbose | Shows a diagnostic message for each file that is processed. |
The chmod command sets the files or directories’ permissions either using symbolic or octal mode. Symbolic mode is the most common one that combines letters and operators to set the desired permissions. It uses the + operator to add new permissions, the – operator to remove existing permissions, and the = operator to set permissions with a particular value.
Moreover, it uses u to refer owner, g for the group, o for others, and a for all classes (owner, group, others).
To check the file permissions in Linux, you can run the ls command followed by the -l option:
ls -l
For example, you can set read, write, and execute permissions for the owner using the following command:
chmod u+rwx fileName
You can use the – operator to remove any permission. In the following example, we removed the execute permissions from the owner:
chmod u-x fileName
Similarly, you can assign the read, write, and execute permissions to the owner and group, while read-only permissions to the others using the following command:
chmod ug+rwx,o+r fileName
Alternatively, you can use the octal notation to add permissions to a file or directory. In this case, we use a three-digit number to specify the permission. The first one specifies the permissions for the owner, the second one for the group, and the last one for others.
These digits are computed using three values: 1 for execute, 2 for write, and 4 for read permission. For instance, 5 indicates read and execute, 6 represents read and write, and 7 represents all permissions.
In the following example, we use 777 to assign all three permissions to all classes:
chmod 777 fileName
The output snippet confirms that the chmod command assigns read, write, and execute permissions to the owner, group, and others:
Tip: Use the chown and chgrp commands to change the owner or group of a file, respectively. Also, you can use the umask command to set the default file permissions.
10. uname | Show System Information
The uname command stands for Unix name, which provides system information, such as machine name, kernel name, kernel version, etc.
uname [options]
It can accept one of the following options:
| Option | Description |
|---|---|
| -a, –all | Provides all the information about your system. |
| -i, –hardware-platform | It retrieves the hardware platform or shows unknown. |
| -m, — machine | Retrieves the machine’s hardware name. |
| -n, –nodename | It returns the machine’s network name. |
| -o, –operating-system | It shows the operating system. |
| -p, –processor | Provides the details about the processor type or returns unknown. |
| -r, –kernel-release | It displays the kernel release. |
| -s, –kernel-name | Returns the name of the kernel. |
| -v, –kernel-version | Retrieves the kernel version. |
Let’s get all the information about your system using the uname command:
uname -a
It returns all details, including kernel name, network name, kernel release, and so on:
You can use any of the above-discussed options to get only specific information. For instance, the following command retrieves the kernel release:
Tip: Use the whoami command to get the name of the current user:
That’s it! You have successfully learned the top ten Linux commands to perform your day-to-day tasks quickly and efficiently. You can read our extensive guide on Linux commands to learn more about network management commands, user and group management commands, and disk management commands.












































