Linux Basic Command
1. ls command
The ls command lists files and directories in your system. Here’s
the syntax:
ls [/directory/folder/path]
If you remove the path, the ls command will show the current
working directory’s content. You can modify the command using
these options:
-R – lists all the files in the subdirectories.
-a – shows all files, including hidden ones.
-lh – converts sizes to readable formats, such as MB, GB,
and TB.
2. cd command
Use the cd command to navigate the Linux files and directories.
To use it, run this syntax with sudo privileges:
cd /directory/folder/path
Depending on your current location, it requires either the full path
or the directory name. For example,
omit /username from /username/directory/folder if you are
already within it.
Omitting the arguments will take you to the home folder. Here are
some navigation shortcuts:
cd ~[username] – goes to another user’s home directory.
cd .. – moves one directory up.
cd- – switches to the previous directory.
3. mkdir command
Use the mkdir command to create one or multiple directories and
set their permissions. Ensure you are authorized to make a new
folder in the parent directory. Here’s the basic syntax:
mkdir [option] [directory_name]
To create a folder within a directory, use the path as the
command parameter. For example, mkdir music/songs will
create a songs folder inside music. Here are several
common mkdir command options:
-p – creates a directory between two existing folders. For
example, mkdir -p Music/2024/Songs creates a new 2024
directory
-m – sets the folder permissions. For instance, enter mkdir
-m777 directory to create a directory with read, write, and
execute permissions for all users.
-v – prints a message for each created directory.
4. rmdir command
Use the rmdir command to delete an empty directory in Linux .
The user must have sudo privileges in the parent directory.
Here’s the syntax:
rmdir [option] directory_name
If the folder contains a subdirectory, the command will return an
error. To force delete a non-empty directory, use the -p option.
5. rm command
Use the rm command to permanently delete files within a
directory. Here’s the general syntax:
rm [filename1] [filename2] [filename3]
Adjust the number of files in the command according to your
needs. If you encounter an error, ensure you have
the write permission in the directory.
To modify the command, add the following options:
-i – prompts a confirmation before deletion.
-f – allows file removal without a confirmation.
-r – deletes files and directories recursively
6. cp command
Use the cp command to copy files or directories, including their
content, from your current location to another. It has various use
cases, such as:
Copying one file from the current directory to another folder.
Specify the file name and target path:
cp filename.txt /home/username/Documents
Duplicating multiple files to a directory. Enter the file names
and the destination path:
cp filename1.txt filename2.txt filename3.txt /home/username/Documents
Copying a file’s content to another within the same
directory. Enter the source and the destination file:
cp filename1.txt filename2.txt
Duplicating an entire directory. Pass the -R flag followed by
the source and destination directory:
cp -R /home/username/Documents /home/username/Documents_backup
7. mv command
Use the mv command to move or rename files and directories.
To move items, enter the file name followed by the destination
directory:
mv filename.txt /home/username/Documents
Meanwhile, use the following syntax to rename a file in
Linux with the mv command:
mv old_filename.txt new_filename.txt
8. touch command
The touch command lets you create an empty file in a specific
directory path. Here’s the syntax:
touch [option] /home/directory/path/file.txt
If you omit the path, the command will create the item in the
current folder. You can also use touch to generate and modify a
timestamp in the Linux command line.
9 file command
The file command lets you check a file type – whether it is a text,
image, or binary. Here’s the syntax:
file filename.txt
To bulk-check multiple files, list them individually or use their
path if they are in the same directory. Add the -k option to display
more detailed information and -i to show the file’s MIME type.
10. cat command
Concatenate or cat is one of the most used Linux commands. It
lists, combines, and writes file content to the standard output.
Here’s the syntax:
cat filename.txt
There are various ways to use the cat command:
cat > filen.txt – creates a new file.
cat file1.txt file2.txt >
file3.txt – merges file1.txt with file2.txt and stores the
output in filename3.txt.
tac file.txt – displays content in reverse order.
11. grep command
The global regular expression or grep command lets you find a
word by searching the content of a file. This Linux command
prints all lines containing the matching strings, which is useful for
filtering large log files.
For example, to display lines containing blue in
the notepad.txt file, enter:
grep blue notepad.txt
12 head command
The head command prints the first ten lines of a text file or piped
data in your command-line interface. Here’s the general syntax:
head [option] [file]
For instance, to view the first ten lines of note.txt in the current
directory, enter:
head note.txt
The head command accepts several options, such as:
-n – changes the number of lines printed. For
example, head -n 5 shows the first five lines.
-c – prints the file’s first customized number of bytes.
-q – disables headers specifying the file name.
13. sort command
The sort command rearranges lines in a file in a specific order. It
doesn’t modify the actual file and only prints the result as
Terminal outputs. Here’s the syntax:
sort [option] [file]
By default, this command will sort the lines in alphabetical order,
from A to Z. To modify the sorting, use these options:
-o – redirects the command outputs to another file.
-r – reverses the sorting order to descending.
-n – sorts the file numerically.
-k – reorders data in a specific field.
14. diff command
The diff command compares two files’ content and outputs the
differences. It is used to alter a program without modifying the
code. Here’s the general format:
diff [option] file1 file2
Below are some acceptable options:
-c – displays the difference between two files in a context
form.
-u – shows the output without redundant information.
-i – makes the diff command case insensitive.
15. df command
Use the df command to check a Linux system’s disk space
usage in percentage and kilobyte (KB). Here’s the syntax:
df [options] [file]
If you don’t specify the item, this command will display
information about every mounted file system. These are some
acceptable options:
-m – displays information on the file system usage in MBs.
-k – prints file system usage in KBs.
-T – shows the file system type in a new column.
16 du command
Use du to check a file or directory’s storage consumption.
Remember to specify the directory path when using this
command, for example:
du /home/user/Documents
The du command has several options, such as:
-s – shows the specified folder’s total size.
-m – provides folder and file information in MB.
-k – displays information in KB.
-h – informs the displayed folders and files’ last modification
date.
17. top command
The top command displays running processes and the system’s
real-time condition, including resource utilization. It helps identify
resource-intensive processes, enabling you to disable them
easily.
To run the command, enter top into your command-line
interface.
18 htop command
The htop command is an interactive program for monitoring
system resources and server processes. Unlike top, it offers
additional features like mouse operation and visual indicators.
Here’s the command syntax:
htop [options]
It supports options such as:
-d – shows the delay between updates in tenths of seconds.
-C – enables monochrome mode.
-h – displays the help message and exits.
19 uname command
The uname or unix name command prints information about
your machine, including its hardware, system name, and Linux
kernel. Here’s the basic syntax:
uname [option]
While you can use it without an option, add the following to
modify the command:
-a – prints all the system information.
-s – outputs the kernel name.
-n – shows the system’s node hostname.
20 hostname command
Run the hostname command to display the system’s hostname.
Here’s the syntax:
hostname [option]
You can run it without an option or use the following:
-a – displays the hostname’s alias.
-A – shows the machine’s Fully Qualified Domain Name
(FQDN).
-i – outputs the machine’s IP address.
21 time command
Use time to measure commands’ execution time. Here’s the
syntax:
time [commandname]
To measure a series of commands, separate them using
semicolons or double ampersands (&&). For example, we
will measure cd, touch, and chmod commands’ overall
execution time:
time cd /home/directory/path; touch bashscript.sh; chmod +x
bashscript.sh
22. shutdown command
The Linux shutdown command lets you turn off or restart
your system at a specific time. Here’s the syntax:
shutdown [option] [time] "message"
You can use an absolute time in a 24-hour format or a
relative one like +5 to schedule it in five minutes.
The message is a notification sent to logged-in users about
the system shutdown.
Instead of shutting down, restart the system using the -
r option. To cancel a scheduled reboot, run the command
with the -c option.
Linux Commands for Network Management and
Troubleshooting
Here are commonly used Linux commands for managing
and troubleshooting network connections.
23 ping command
The ping command is one of the most used commands in
Linux. It lets you check whether a network or server is
reachable, which is useful for troubleshooting connectivity
issues. Here’s the syntax:
ping [option] [hostname_or_IP_address]
For example, run the following to check the connection and
response time to Google:
ping google.com
24. wget command
Use the wget command to download files from the internet
using HTTP, HTTPS, or FTP protocols. Here’s the syntax:
wget [option] [url]
For example, enter the following to download the latest version
of WordPress:
wget https://wordpress.org/latest.zip
25. netstat command
The netstat command is used to display your system’s network
information, like sockets and routing. Here’s the command
syntax:
netstat [option]
Use various options to modify the displayed information. Some
common ones are:
-a – displays listening and closed sockets.
-t – shows TCP connections.
-u – lists UDP connections.
-r – displays routing tables.
-i – shows information about network interfaces.
-p – lists programs’ names and process IDs.
-c – continuously outputs network information for real-time
monitoring.
26. pwd command
The pwd command prints your current working directory’s
path, like /home/directory/path. Here’s the command
syntax:
pwd [option]
It supports two options. The -L or –-logical option prints
environment variable content, including symbolic links .
Meanwhile, -P or –physical outputs the current directory’s
actual path.
27. tac Command
The tac command is the reverse of cat command, as its name
specified.
It displays the file content in reverse order (from the last line).
Syntax:
1. tac <file name>
28. more command
The more command is quite similar to the cat command, as it is used to
display the file content in the same way that the cat command does. The
only difference between both commands is that, in case of larger files, the
more command displays screenful output at a time.
In more command, the following keys are used to scroll the page:
ENTER key: To scroll down page by line.
Space bar: To move to the next page.
b key: To move to the previous page.
/ key: To search the string.
Syntax:
1. more <file name>
29. less Command
The less command is similar to the more command. It also includes some
extra features such as 'adjustment in width and height of the terminal.'
Comparatively, the more command cuts the output in the width of the
terminal.
Syntax:
1. less <file name>
30. passwd Command
The passwd command is used to create and change the password for a
user.
Syntax:
1. passwd <username>
31. comm Command
The 'comm' command is used to compare two files or streams. By default,
it displays three columns, first displays non-matching items of the first file,
second indicates the non-matching item of the second file, and the third
column displays the matching items of both files.
Syntax:
1. comm <file1> <file2>
32. tee command
The tee command is quite similar to the cat command. The only
difference between both filters is that it puts standard input on
standard output and also write them into a file.
Syntax:
cat <fileName> | tee <newFile> | cat or tac
33. wc Command
The wc command is used to count the lines, words, and characters in a
file.
Syntax:
1. wc <file name>
34 od
The od command is used to display the content of a file in different s, such
as hexadecimal, octal, and ASCII characters.
Syntax:
1. od -b <fileName> // Octal format
2. od -t x1 <fileName> // Hexa decimal format
3. od -c <fileName> // ASCII character format
35. gzip Command
The gzip command is used to truncate the file size. It is a compressing
tool. It replaces the original file by the compressed file having '.gz'
extension.
Syntax:
1. gzip <file1> <file2> <file3>...
36 locate
The locate command is used to search a file by file name. It is quite
similar to find command; the difference is that it is a background process.
It searches the file in the database, whereas the find command searches
in the file system. It is faster than the find command. To find the file with
the locates command, keep your database updated.
Syntax:
1. locate <file name>
37. cal Command
The cal command is used to display the current month's calendar with the
current date highlighted.
Syntax:
1. cal<
38. sleep Command
The sleep command is used to hold the terminal by the specified amount
of time. By default, it takes time in seconds.
Syntax:
sleep <time>
39 uniq Command
The uniq command is used to form a sorted list in which every word will
occur only once.
Syntax:
command <fileName> | uniq
40 . gunzip Command
The gunzip command is used to decompress a file. It is a reverse
operation of gzip command.
Syntax:
gunzip <file1> <file2> <file3
41 . cal Command
The cal command is used to display the current month's calendar with the
current date highlighted.
Syntax:
Cal
42. mount Command
The mount command is used to connect an external device file system to
the system's file system.
Syntax:
1. mount -t type <device> <directory>
43. clear Command
Linux clear command is used to clear the terminal screen.
Syntax:
1. clear
44 ip Command
Linux ip command is an updated version of the ipconfig command. It is
used to assign an IP address, initialize an interface, disable an interface.
Syntax:
ip a or ip addr
45. mail Command
The mail command is used to send emails from the command line.
Syntax:
1. mail -s "Subject" <recipient address>
Output:
46 host Command
The host command is used to display the IP address for a given domain
name and vice versa. It performs the DNS lookups for the DNS Query.
Syntax:
1. host <domain name> or <ip address>
47. zip, unzip commands
2. The zip command lets you compress items into
a ZIP file with the optimal compression ratio. Here’s the
syntax:
3. zip [options] zipfile file1 file2….
4. For example, this command
compresses note.txt into archive.zip in the current working
directory:
5. zip archive.zip note.txt
6. Use the unzip command to extract the compressed file .
Here’s the syntax:
7. unzip [option] file_name.zip
48. nano, vi, jed commands
8. Linux lets users edit files using a text editor like nano , vi,
or jed. While most distributions include nano and vi, users
must install jed manually. All these tools have the same
command syntax:
9. nano filename to exit ctrl x
10. vi filename to exit ctrl z
jed filename
49. find command
Use the find command to search for files within a specific
directory. Here’s the syntax:
find [option] [path] [expression]
For example, to find a file called file1.txt within
the directory folder and its subdirectories, use this command:
find /home -name file1.txt
If you omit the path, the command will search the current working
directory. You can also find directories using the following:
find ./ -type d -name directoryname
Linux Commands for User and Permission Management
Below are several Linux commands for managing the system’s
users and permissions.
50. sudo command
Superuser do or sudo is one of the most basic commands in
Linux. It runs your command with administrative or root
permissions. Here’s the general syntax:
sudo (command)
When you run a sudo command, Terminal will request the root
password. For example, this snippet runs useradd with the
superuser privilege:
sudo useradd username
You can also add an option, such as:
-k – invalidates the timestamp file.
-g – executes commands as a specified group name or ID.
-h – runs commands on the host.
51. su command
The su command lets you run a program in the Linux shell as a
different user. It is useful to connect via SSH while the root user
is disabled. Here’s the syntax:
su [options] [username [argument]]
Without any option or argument, this command runs
through root and prompts you to use the sudo privileges
temporarily. Some options are:
-p – keeps the same shell environment, consisting
of HOME, SHELL, USER, and LOGNAME.
-s – lets you specify another shell environment to run.
-l – runs a login script to switch users. It requires you to
enter the user’s password.
To check the current shell’s user account, run
the whoami command:
52. chmod command
The chmod command modifies directory or file permissions
in Linux. Here’s the basic syntax:
chmod [option] [permission] [file_name]
In Linux, each file is associated with three user classes
– owner, group member, and others. It also has three
permissions – read, write, and execute. If an owner wants to
grant all permissions to every user, the command looks like this:
chmod -rwxrwxrwx note.txt
53. chown command
The chown command lets you change a file, directory, or
symbolic link’s ownership to the specified username. Here’s the
syntax:
chown [option] owner[:group] file(s)
For example, to make linuxuser2 the owner of filename.txt, use:
chown linuxuser2 filename.txt
54 useradd, userdel commands
Use useradd to create a new Linux user account and change its
password with the passwd command. Here are the syntaxes:
useradd [option] username
passwd username
Both the useradd and passwd commands require sudo
privileges. To delete a user, use the userdel command:
userdel username
Linux Commands for System Information and Management
This section will list common Linux commands for querying
system information and management.