Linux Commands 40 Most Used
Linux Commands 40 Most Used
o 1. sudo command
o 2. pwd command
o 3. cd command
o 4. ls command
o 5. cat command
o 6. cp command
o 7. mv command
o 8. mkdir command
o 9. rmdir command
o 10. rm command
o 11. touch command
o 12. locate command
o 13. find command
o 14. grep command
o 15. df command
o 16. du command
o 17. head command
o 18. tail command
o 19. diff command
o 20. tar command
o 21. chmod command
o 22. chown command
o 23. jobs command
o 24. kill command
o 25. ping command
o 26. wget command
o 27. uname command
o 28. top command
o 29. history command
o 30. man command
o 31. echo command
o 32. zip, unzip commands
o 33. hostname command
o 34. useradd, userdel commands
o 35. apt-get command
o 36. nano, vi, jed commands
o 37. alias, unalias commands
o 38. su command
o 39. htop command
o 40. ps command
o Bonus Tips and Tricks
o Download a Free Linux Commands Cheat Sheet
Linux Commands FAQ
o What Is the Basic Command of Linux?
o How Many Commands Does Linux Have?
2. pwd command
Use the pwd command to find the path of your current working directory.
Simply entering pwd will return the full current path – a path of all the
directories that starts with a forward slash (/). For
example, /home/username.
The pwd command uses the following syntax:
pwd [option]
It has two acceptable options:
-L or –logical prints environment variable content, including symbolic
links.
-P or –physical prints the actual path of the current directory.
3. cd command
To navigate through the Linux files and directories, use the cd command.
Depending on your current working directory, it requires either the full path
or the directory name.
Running this command without an option will take you to the home folder.
Keep in mind that only users with sudo privileges can execute it.
Let’s say you’re in /home/username/Documents and want to go
to Photos, a subdirectory of Documents. To do so, enter the following
command:
cd Photos.
If you want to switch to a completely new directory, for
example, /home/username/Movies, you have to enter cd followed by the
directory’s absolute path:
cd /home/username/Movies
Here are some shortcuts to help you navigate:
cd ~[username] goes to another user’s home directory.
cd .. moves one directory up.
cd- moves to your previous directory.
4. ls command
The ls command lists files and directories within a system. Running it
without a flag or parameter will show the current working directory’s
content.
To see other directories’ content, type ls followed by the desired path. For
example, to view files in the Documents folder, enter:
ls /home/username/Documents
Here are some options you can use with the ls command:
ls -R lists all the files in the subdirectories.
ls -a shows hidden files in addition to the visible ones.
ls -lh shows the file sizes in easily readable formats, such as MB,
GB, and TB.
5. cat command
Concatenate, or cat, is one of the most frequently used Linux commands.
It lists, combines, and writes file content to the standard output. To run the
cat command, type cat followed by the file name and its extension. For
instance:
cat filename.txt.
Here are other ways to use the cat command :
cat > filename.txt creates a new file.
cat filename1.txt filename2.txt >
filename3.txt merges filename1.txt and filename2.txt and stores
the output in filename3.txt.
tac filename.txt displays content in reverse order.
6. cp command
Use the cp command to copy files or directories and their content. Take a
look at the following use cases.
To copy one file from the current directory to another, enter cp followed by
the file name and the destination directory. For example:
cp filename.txt /home/username/Documents
To copy files to a directory, enter the file names followed by the destination
directory:
cp filename1.txt filename2.txt filename3.txt
/home/username/Documents
To copy the content of a file to a new file in the same directory,
enter cp followed by the source file and the destination file:
cp filename1.txt filename2.txt
To copy an entire directory, pass the -R flag before typing the source
directory, followed by the destination directory:
cp -R /home/username/Documents
/home/username/Documents_backup
7. mv command
The primary use of the mv command is to move and rename files and
directories. Additionally, it doesn’t produce an output upon execution.
Simply type mv followed by the filename and the destination directory. For
example, you want to move filename.txt to
the /home/username/Documents directory:
mv filename.txt /home/username/Documents.
You can also use the mv command to rename a file:
mv old_filename.txt new_filename.txt
8. mkdir command
Use the mkdir command to create one or multiple directories at once and
set permissions for each of them. The user executing this command must
have the privilege to make a new folder in the parent directory, or they may
receive a permission denied error.
Here’s the basic syntax:
mkdir [option] directory_name
For example, you want to create a directory called Music:
mkdir Music
To make a new directory called Songs inside Music, use this command:
mkdir Music/Songs
The mkdir command accepts many options, such as:
-p or –parents create a directory between two existing folders. For
example, mkdir -p Music/2020/Songs will make the new “2020”
directory.
-m sets the file permissions. For instance, to create a directory with
full read, write, and execute permissions for all users, enter mkdir -
m777 directory_name.
-v prints a message for each created directory.
9. rmdir command
To permanently delete an empty directory, use the rmdir command .
Remember that the user running this command should
have sudo privileges in the parent directory.
For example, you want to remove an empty subdirectory
named personal1 and its main folder mydir:
rmdir -p mydir/personal1
10. rm command
The rm command is used to delete files within a directory. Make sure that
the user performing this command has write permissions.
Remember the directory’s location as this will remove the file(s) and you
can’t undo it.
Here’s the general syntax:
rm filename
To remove multiple files, enter the following command:
rm filename1 filename2 filename3
Here are some acceptable options you can add:
-i prompts system confirmation before deleting a file.
-f allows the system to remove without a confirmation.
-r deletes files and directories recursively.
16. du command
If you want to check how much space a file or a directory takes up, use
the du command. You can run this command to identify which part of the
system uses the storage excessively.
Remember, you must specify the directory path when using
the du command. For example, to check /home/user/Documents enter:
du /home/user/Documents
Adding a flag to the du command will modify the operation, such as:
-s offers the total size of a specified folder.
-m provides folder and file information in MB
k displays information in KB.
-h informs the last modification date of the displayed folders and
files.
17. head command
The head command allows you to view the first ten lines of a text. Adding
an option lets you change the number of lines shown.
The head command is also used to output piped data to the CLI.
Here’s the general syntax:
head [option] [file]
For instance, you want to view the first ten lines of note.txt, located in the
current directory:
head note.txt
Below are some options you can add:
-n or –lines prints the first customized number of lines. For example,
enter head -n 5 filename.txt to show the first five lines
of filename.txt.
-c or –bytes prints the first customized number of bytes of each file.
-q or –quiet will not print headers specifying the file name.
System calls
Library calls
Games
Special files
File formats and conventions
System administration commands
Kernel routines
Miscellaneous
To display the complete manual, enter:
man [command_name]
For example, you want to access the manual for the ls command:
man ls
Enter this command if you want to specify the displayed section:
man [option] [section_number] [command_name]
For instance, you want to see section 2 of the ls command manual:
man 2 ls
31. echo command
The echo command is a built-in utility that displays a line of text or string
using the standard output. Here’s the basic syntax:
echo [option] [string]
For example, you can display the text Hostinger Tutorials by entering:
echo “Hostinger Tutorials”
This command supports many options, such as:
-n displays the output without the trailing newline.
-e enables the interpretation of the following backslash escapes:
\a plays sound alert.
\b removes spaces in between a text.
\c produces no further output.
-E displays the default option and disables the interpretation of
backslash escapes.
32. zip, unzip commands
Use the zip command to compress your files into a ZIP file, a universal
format commonly used on Linux. It can automatically choose the best
compression ratio.
The zip command is also useful for archiving files and directories and
reducing disk usage.
To use it, enter the following syntax:
zip [options] zipfile file1 file2….
For example, you have a file named note.txt that you want to compress
into archive.zip in the current directory:
zip archive.zip note.txt
On the other hand, the unzip command extracts the zipped files from an
archive. Here’s the general format:
unzip [option] file_name.zip
So, to unzip a file called archive.zip in the current directory, enter:
unzip archive.zip
33. hostname command
Run the hostname command to know the system’s hostname. You can
execute it with or without an option. Here’s the general syntax:
hostname [option]
There are many optional flags to use, including:
-a or –alias displays the hostname’s alias.
-A or –all-fqdns displays the machine’s Fully Qualified Domain
Name (FQDN).
-i or –ip-address displays the machine’s IP address.
For example, enter the following command to know your computer’s IP
address:
hostname -i
34. useradd, userdel commands
Linux is a multi-user system, meaning more than one person can use it
simultaneously. useradd is used to create a new account, while
the passwd command allows you to add a password. Only those with root
privileges or sudo can run the useradd command.
When you use the useradd command, it performs some major changes:
Edits the /etc/passwd, /etc/shadow, /etc/group,
and /etc/gshadow files for the newly created accounts.
Creates and populates a home directory for the user.
40. ps command
The process status or ps command produces a snapshot of all running
processes in your system. The static results are taken from the virtual files
in the /proc file system.
Executing the ps command without an option or argument will list the
running processes in the shell along with:
The unique process ID (PID)
The type of the terminal (TTY)
The running time (TIME)
The command that launches the process (CMD)
Here are some acceptable options you can use:
-T displays all processes associated with the current shell session.
-u username lists processes associated with a specific user.
-A or -e shows all the running processes.