Linux Command
Linux Command
ls command
The ls command lists the content of a folder, including files and
directories. Here’s the syntax:
ls [options] [directory_or_path]
If you omit the path, the ls command will check the content of your
current directory. To list items inside subfolders, add the -R option.
Meanwhile, use -a to show hidden content.
2. pwd command
To check the full path of your current working directory, use
the pwd command. Its syntax is as follows:
pwd [options]
3. cd command
Use cd to navigate between directories in your Linux VPS. It doesn’t
have any option, and the syntax is simple:
cd [path_or_directory]
Depending on your location, you might only need to specify the parent
directory. For example, omit path from path/to/directory if you are
already inside one. The cd command has several shortcuts:
4. mkdir command
The mkdir command lets you create one or multiple directories. The
syntax looks like this:
mkdir [options] directory_name1 directory_name2
By default, mkdir allows the current user to read, write, and execute files
in the new folder. You can set custom privileges during the creation by
adding the -m option. To learn more about permission management,
read the chmod section below.
5. rmdir command
Run rmdir to delete empty directories in your Linux system . The
command syntax looks like this:
rmdir [options] directory_name
6. rm command
The rm command deletes files from a directory. You must have the write
permission for the folder or use sudo. Here’s the syntax:
rm [options] file1 file2
You can add the -r option to remove a folder and its contents, including
subdirectories. Use the -i flag to display a confirmation message before
the removal or -f to deactivate it completely.
P a g e 2 | 28
Warning! Avoid using -r and -f unless necessary. Instead, add -i option
to prevent accidental deletion.
7. cp command
Use the cp command to copy files from your current directory to another
folder. The syntax looks like this:
cp file1 file2 [target_path]
You can also use cp to duplicate the content of one file to another using
this syntax. If the target is in another location, specify the full path like
so:
cp source_file /path/to/target_file
8. mv command
The main usage of the mv command is to move a file or folder to another
location. Here’s the syntax:
mv file_or_directory [target_directory]
You can also use the mv command to rename files in your Linux
system. Here’s an example:
mv old_name.txt new_name.txt
If you specify the full path, you can simultaneously rename files and
move them to a new location like this example:
mv old/location/of/old_name.txt new/path/for/new_name.txt
9. touch command
P a g e 3 | 28
Run the touch command to create a new empty file in a specific
directory. The syntax is as follows:
touch [options] [path_and_file_name]
If you omit the path, the touch command will create a new file in your
current working directory. Here’s an example:
touch file.txt
If you use this command on a symbolic link , it will output the actual file
connected to the shortcut. You can add the -k option to print more
detailed information about the item.
To create a new TAR file, you must add the -c option. Then, use the -
f flag to specify the archive’s name.
P a g e 4 | 28
If you want to enable compression, add a specific option based on your
preferred method. For example, the following will
bundle file1.txt and file2.txt with the gzip compression:
tar -cfz archive.tar.gz fle1.txt file2.txt
Remember that the archive’s file format will differ depending on the
compression method. Regardless of the extension, you can unpack
a TAR file using this syntax:
tar [options] tar_file_name
If the target file doesn’t exist, these commands will create a new one.
Since your system might not have these text processing utilities pre-
installed, configure them using your package manager.
We will explain the command in the apt and dnf command section.
To print the content in reverse order, use tac instead. If you add the
standard output operator symbol (>), the cat command will create a new
file. For example, the following will make file.txt:
cat > file.txt
You can also use cat with the operator to combine the content of multiple
files into a new item. In this command, file1.txt and file2.txt will merge
into target.txt:
cat file1.txt file2.txt > target.txt
You can also print the first few lines of another command’s output by
piping it like so:
command | head [options]
By default, head will show the first ten lines. However, you can change
this setting using the -n option followed by your desired number.
P a g e 6 | 28
Meanwhile, use -c to print the first few entries based on the byte size
instead of the line.
The tail utility also has the same option as head. For example, we will
extract the last five lines from the ping command’s output:
ping -c 10 8.8.8.8 | tail -n 5
P a g e 7 | 28
Need help with a command?
Ask Kodee, Hostinger’s AI assistant, to break down and explain complex
commands.
Note that this utility doesn’t modify the actual file and only prints the
rearranged content as an output.
By default, the sort command uses the alphabetical order
from A to Z, but you can add the -r option to reverse the order. You can
also sort files numerically using the -n flag.
Unlike other Linux utilities, the cut command’s options are mandatory for
file sectioning. Here are some of the flags:
P a g e 8 | 28
-d – separates lines based on delimiters.
You can combine multiple options for a more specific output. For
example, this command extracts the third to fifth field from a comma-
separated list:
cut -d',' -f3-5 list.txt
By default, the diff command only shows the differences between the
two files. To print all the content and highlight the discrepancies, enable
the context format using the -c option. You can also ignore case
sensitivity by adding -i.
For example, run the following to show only the differences
between 1.txt and 2.txt:
diff -c 1.txt 2.txt
P a g e 9 | 28
23. tee command
The tee command outputs another command’s results to both the
Terminal and a file. It’s helpful if you want to use the data for further
processing or backups. Here’s the syntax:
command | tee [options] file_name
If the specified file doesn’t exist, tee will create it. Be careful when using
this command since it will overwrite the existing content. To preserve
and append existing data, add the -a option.
For example, we will save the ping command’s output as new entries in
the test_network.txt file:
ping 8.8.8.8 | tee -a test_network.txt
P a g e 10 | 28
24. locate command
The locate command searches for a file and prints its location path.
Here’s the syntax:
locate [options] [keyword]
If you use the -r option to search files using regular expressions, omit
the [keyword] argument. The locate command is case-sensitive by
default, but you can turn off this behavior using the -i flag.
Note that locate will look for files from its database. While this behavior
speeds up the search process, you must wait for the list to refresh before
finding newly created items.
Alternatively, enter the following to reload the data manually:
updatedb
If you don’t specify the path, the find command will search your current
working directory. To find files using their name, add the -name option
followed by the keyword.
P a g e 11 | 28
You can specify the type of item you are looking for using the -type flag.
The –type f option will search files only, while -type d will find
directories. For example, we will check file.txt in path/to/folder:
find path/to/folder -type f -name "file"
Unlike locate, the find command searches through folders in real time.
While it slows down the process, you can look for new items immediately
without waiting for the system database to refresh.
The Terminal will prompt you to enter the user’s password before
executing the command. By default, you must reenter it after five minutes
of inactivity.
Typically, you don’t add any option to sudo, but you can check them by
entering:
sudo --help
Warning! Since users with sudo privileges can change various settings
of your system, use this command with caution.
P a g e 12 | 28
If you don’t specify any option or username, this command will switch
you to the root user. In this case, you must enter the password before
changing the account.
You can check the currently logged-in user from the Linux command-line
shell. Alternatively, use the whoami command:
whoami
In Linux, there are three folder and file permissions – read (r), write (w),
and execute (x). You can assign them to three parties – the owner,
a group, or other accounts belonging to neither category. Consider this
example:
chmod -rwx---r-– file1.txt
The spot after the first hyphen (–) specifies the permission for the owner
of file1.txt. In the previous example, we grant them the rwx privilege.
The next spot is for groups. Since we won’t grant them any privilege, we
put three hyphens to indicate emptiness. The last slot is for other users
who only have read or r permission.
If you want to assign a user as the new owner of an item, leave the
group name empty. For example, we will make admin-vps the owner
of file1.txt:
chown admin-vps file1.txt
P a g e 13 | 28
Conversely, omit the username to make all group members the owner.
Remember to write the colons (:) like so:
chown :newgroup file1.txt
By default, the useradd command doesn’t prompt you to give the new
user a password. You can add or change it manually later with
the passwd command:
passwd new_username
31. df command
The df command checks your Linux system’s disk usage , displaying
the used space in percentage and kilobyte (KB). The syntax looks like
this:
df [options] [file system]
Note that the df command operates at the file system level. If you don’t
specify one, the utility will display all the active file systems.
P a g e 14 | 28
32. du command
To check the size of a directory and its content, use the du command.
Here’s the syntax:
du [directory]
The command will check your working directory if you don’t specify a
path or folder. By default, it breaks down each subfolder’s disk usage,
but you can add the -s option to summarize the total usage in one
output.
You can also use the -M option to change the information
from KB to MB.
The top command has various options. For example, -p lets you check a
specific process by specifying its ID. Meanwhile, add the -d flag to
change the delay between screen updates.
P a g e 15 | 28
Like top, the htop command lets you display and manage processes in
your Linux server . It also shares the same syntax:
htop [options]
htop has options similar to top, but you can add additional ones. For
example, -C enables the monochrome mode, while –-tree shows
processes in a hierarchical view.
35. ps command
The ps command summarizes the status of all running processes in your
Linux system at a specific time. Unlike top and htop, it doesn’t update
the information automatically. Here’s the syntax:
ps [options]
P a g e 16 | 28
You can print a more detailed report by adding other options. For
example, use -A to list all processes in your system, -r to check only the
running ones, or -u username to query those associated with a particular
account.
Without any option, the command will print your system’s kernel name.
To check all information about your machine, add the -a option.
If you leave the option empty, the command will print your hostname.
Add -i to check your server’s IP address, -a to print the hostname alias,
and -A to output the system’s fully qualified domain name (FQDN).
Note that this command might not work with older distributions since they
use another service manager.
P a g e 18 | 28
By default, watch will run your command every two seconds, but you
can change the interval using the -n option followed by the delay. If you
want to highlight changes in the output, add the -d flag.
Running this command without any argument will show all jobs running
in the Terminal’s foreground and background. If you don’t have any
ongoing tasks, it will return an empty output.
You can display more detailed information about each job by adding the -
l option. Meanwhile, use -n to show only tasks whose status has
changed since the last notification.
If you run the command without any arguments, your system will shut
down immediately. You can specify the schedule using a 24-hour format
P a g e 19 | 28
or a relative one. For example, enter +5 to shut down the system
after five minutes. To restart the machine, add the -r option.
The message argument specifies the notification other users in your
system will receive before the server shuts down.
By default, ping sends infinite packets until the user manually stops it by
pressing Ctrl + C.
However, you can specify a custom number using the -c option. You can
also change the interval between transfers by adding -i.
For instance, let’s send 15 packets every two seconds to Google’s
server:
ping -c 15 -i 2 google.com
P a g e 20 | 28
By default, the wget command will download an item to your current
working directory. For example, run this command to retrieve the latest
WordPress installer:
wget https://wordpress.org/latest.zip
Running cURL without an option will print the website’s HTML content in
your Terminal. If you add the -O or -o option, the command will download
files from the specified link.
The cURL command is also helpful for testing API or server endpoints.
You can do so by adding the –X option followed by an HTTP method,
depending on whether you want to fetch or upload data.
For example, the following command will retrieve data from a specific
API endpoint:
curl -X GET https://api.example.com/endpoint
If you are copying items to or from your local machine, omit the IP and
path. When transferring a file or folder from a local machine, specify its
name after options.
For example, we will run the following to copy file1.txt to our
VPS’ path/to/folder directory as root:
scp file1.txt root@185.185.185.185:path/to/folder
P a g e 21 | 28
You can change the default SCP port by specifying its number after the -
P option. Meanwhile, use the -l flag to limit the transfer bandwidth and
add –C to enable compression.
The source and destination can be a folder within the same system, a
local machine, or a remote server. If you are syncing content with a VPS,
specify the username and IP address like so:
rsync /path/to/local/folder/ vps-user@185.185.185.185:/path/to/remote/folder/
You can add the -a option to sync the file or folder’s attributes as well,
including their symbolic links. Meanwhile, use the -z flag to enable
compression during the transfer.
49. ip command
The ip utility lets you list and manage your system’s network parameters,
similar to the ifconfig command in older Linux distros. Here’s the
syntax:
ip [options] object command
Running this command without any argument will print the manual,
including an explanation about acceptable options and objects.
To manage a network parameter, specify the action in
the command argument. For example, run this to show your system’s IP
address:
ip address show
P a g e 22 | 28
50. netstat command
The netstat command displays information about your system’s network
configuration. The syntax is simple:
netstat [options]
P a g e 23 | 28
51. traceroute command
The traceroute command tracks a packet’s path when traveling between
hosts, providing information like the transfer time and involved routers.
Here’s the syntax:
traceroute [options] destination
If you don’t specify a DNS server, nslookup will use your internet
service provider’s default resolver. You can add other options to change
how this command queries an IP address or a domain.
For example, use the -type= option to specify the information you want
to check, such as the DNS records.
You can also set up automatic retry with the -retry= flag and add -
port= to use a specific port.
P a g e 24 | 28
Since some Linux distros don’t have this utility pre-installed, you might
encounter the “command not found” error. You can configure it by
downloading bind-utils or dnsutils via your package manager.
To run a reverse DNS lookup, add the –x option and use an IP address
as the target.
Add the -r option if you want to clear the Terminal history. To rerun a
specific utility from the list, enter an exclamation mark followed by its ID.
For example, use the following to run the 145th command:
!145
P a g e 25 | 28
55. man command
The man or manual command displays a comprehensive guide of
another utility. The syntax looks like the following:
man [options] [section_number] command_name
If you specify only the command name, man will display the entire
manual. Alternatively, you can select one of the nine sections using
their IDs to print more specific information.
For example, run the following to check the library call section of
the ls command’s manual:
man 3 ls
You can also add the redirection symbol (>) to print the text in a file
instead of Terminal. If you use two symbols (>>), it will append the
existing content. The command syntax looks like this:
echo [options] [text] > [file_name]
If your text contains an environment or shell variable like $var, echo will
display the actual value. This command is commonly used for testing
and bash scripting .
57. ln command
The ln command links files or directories with a shortcut. The syntax
looks as follows:
ln [options] source target
P a g e 26 | 28
This command will automatically create the shortcut, meaning you don’t
need to make one manually. For example, the following will enable you
to open file.txt using shortcut.txt:
ln target.txt shortcut.txt
If you don’t add any argument, the command will show the current date.
Alternatively, you can enter a specific month and year in a numerical
format.
You can also add the -3 option to show the current, previous, and next
month.
P a g e 27 | 28
60. apt and dnf command
The apt command lets you manage advanced package tool (APT)
libraries in Debian-based operating systems such as Ubuntu and Kali
Linux. The syntax looks like this:
apt [options] subcommand
The subcommands define the action, like updating the library, upgrading
software, installing an application, or removing a package. For example,
we will install the Vim text editor:
apt install vim
P a g e 28 | 28