0% found this document useful (0 votes)
2 views7 pages

Linux Top Commands

Linux main commands
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
2 views7 pages

Linux Top Commands

Linux main commands
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 7

1.

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]

The pwd command has only two options. The -L option prints environment variable
content, like shortcuts, instead of the actual path of your current location.
Meanwhile, -P outputs the exact location.

For example, /shortcut/folder is a shortcut for /actual/path, and you are currently
in /actual/path/dir. If you use the -L option, the output will be:

/shortcut/folder/dir

Meanwhile, the -P option will print the exact canonical path:

/actual/path/dir

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:

cd – returns to the current user’s home directory.


cd .. – moves a directory up.
cd – – goes back to the previous directory.

4. mkdir command

The mkdir command lets you create one or multiple directories. The syntax looks
like this:

mkdir [options] directory_name1 directory_name2

To create a folder in another location, specify the full path. Otherwise, this
command will make the new item in your current working directory.

For example, enter the following to create new_folder in /path/to/target_folder:

mkdir path/to/target_folder/new_folder
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

The rmdir command won’t work if the directory contains subfolders. To force the
deletion, add the –p option. Note that you must own the item you want to remove or
use sudo instead.
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.

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

Additionally, cp lets you duplicate a directory and its content to another folder
using the -R option:

cp -R /path/to/folder /target/path/to/folder_copy

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]

For example, we will move file1.txt from another location to the


/new/file/directory path using this command:

mv /original/path/file1.txt the/target/path

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

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

10. file command

The file command checks a file type, such as TXT, PDF, or other. The syntax is as
follows:

file [file_name]

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.
File command shows the actual file of a symbolic link
11. zip and unzip commands

The zip command compresses one or multiple files into a ZIP archive, reducing their
size. Here’s the syntax:

zip [options] zip_file_name file1 file2

To extract a compressed file into your current working directory, use the unzip
command like so:

unzip [options] zip_file_name

12. tar command

The tar command bundles multiple files or directories into an archive without
compression. The syntax looks as follows:

tar [options] tar_file_name file1 file2

To create a new TAR file, you must add the -c option. Then, use the -f flag to
specify the archive’s name.

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

13. nano, vi, and jed command

nano, vi, and jed commands let you edit files. They have the same syntax, except at
the beginning, where you specify the name of the tool:

nano/vi/jed 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.
14. cat command

The concatenate or cat command has various usages. The most basic one is printing
the content of a file. Here’s the syntax:

cat file_name

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

15. grep command

Global regular expression print or grep lets you search specific lines from a file
using keywords. It is useful for filtering large data like logs. The syntax looks
as follows:

grep [options] keyword [file]

You can also filter data from another utility by piping it to the grep command. For
example, the following searches file.txt from the ls command’s output:

ls | grep "file.txt"

Grep command filters ls' output


16. sed command

Use the sed command to search and replace patterns in files quickly. The basic
syntax looks like this:

sed [options] 'subcommand/new_pattern/target_pattern' input_file

You can replace a string in multiple files simultaneously by listing them. Here’s
an example of a sed command that changes red in colors.txt and hue.txt with blue:

sed 's/red/blue' colors.txt hue.txt


17. head command

Use the head command to print the first few entries of a file. The basic syntax is
as follows:

head [options] file_name

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.

Meanwhile, use -c to print the first few entries based on the byte size instead of
the line.
18. tail command

The tail command is the opposite of head, allowing you to print the last few lines
from files or another utility’s output. Here are the syntaxes:

tail [options] file_name

command | tail [options]

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

Tail command prints last five lines from ping


19. awk command

The awk command searches and manipulates regular expression patterns in a file.
Here’s the basic syntax:

awk '/regex pattern/{action}' input_file.txt

Although similar to sed, awk offers more operations beyond substitution, including
printing, mathematical calculation, and deletion. It also lets you run a complex
task with an if statement.

You can run multiple actions by listing them according to their execution order,
separated by a semicolon (;). For example, this awk command calculates the average
student score and print names that are above that threshold:

awk -F':' '{ total += $2; students[$1] = $2 } END { average = total /


length(students); print "Average:", average; print "Above average:"; for (student
in students) if (students[student] > average) print student }' score.txt

awk prints average score and students with the higher-than-average score
Need help with a command?

Ask Kodee, Hostinger’s AI assistant, to break down and explain complex commands.
20. sort command

Use the sort command to rearrange a file’s content in a specific order. Its syntax
looks as follows:

sort [options] [file_name]

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.
21. cut command

The cut command selects specific sections from a file and prints them as a Terminal
output. The syntax looks like this:

cut options file

Unlike other Linux utilities, the cut command’s options are mandatory for file
sectioning. Here are some of the flags:

-f – selects a specific row field.


-b – cuts the line by a specified byte size.
-c – sections the line using a specified character.
-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

cut command extracts sections from a comma-separated list


22. diff command

The diff command compares two files and prints their differences. Here’s the
syntax:

diff file_name1 file_name2

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

diff command shows differences between files in context format


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

tee-command-prints-ping-output-in-terminal-and-a-file
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

25. find command

The find command searches for a file within a specific directory. Here’s the
syntax:

find [path] [options] expression

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.

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.

You might also like