Click on the [Full Stack Developer Community] above → Top right corner [...] → [Add to Favorites ⭐
Click to receive full stack materials: Full Stack Materials
This article will provide a detailed introduction to commonly used Linux commands, demonstrations, and some explanations of basic knowledge.

Table of Contents
-
ls command -
file command -
pwd command -
whoami command -
cd command -
Relative and Absolute Paths -
which command -
touch command -
mkdir command -
Adding User Trust Relationships -
rmdir command -
rm command -
man command -
cp command -
mv command -
Editing -
cat command -
echo command -
Redirection -
more command -
less command -
head command -
tail command -
Pipelining -
date command -
cal command -
find command -
grep command -
zip/unzip commands -
tar command -
bc command -
uname command -
shutdown command -
stat command -
The Three Times in Linux -
Supplement to the touch command -
Hotkeys in Linux -
Command Overview
ls command
Syntax: ls [options] [directory]
Function: The ls command has multiple options. For directories, this command lists all subdirectories and files under the directory. For files, it will list the file names and other information.
By default, ls displays the files in the current directory without options.

Options:
-l
: Lists file details in a list format.

In Linux, file types are identified by the first character instead of file extensions. Regardless of whether the file extension is .c
, .txt
, or another extension, only files that start with a dash (-) are regular files.
-
d: Directory file, simply put, it is a folder where we can create new files. -
-: Regular files, which can be text files, dynamic/static libraries, executable programs, etc. -
l: Soft link (similar to Windows shortcuts). -
b: Block device files (such as hard disks, CD drives, etc.). -
p: Pipe files. -
c: Character device files (such as screens and other serial devices). -
s: Socket files.
Our focus today will be on the first two types of files.
A single regular file can be classified into many types. To maintain compatibility with Windows and to help most Linux users distinguish between different file types, we still prefer to indicate different file types through extensions. However, some files must have extensions, such as
.c
files written in C language, which need special handling and are distinguished by extensions.
-
-a: Displays all files in the directory, including hidden files, where files starting with a dot are hidden files. One dot represents the current path, and two dots represent the parent path. We will explain this in the cd command section below.

-
-d: Displays the directory like a file, without showing the files in the directory. The dot here represents the current path.

-
-R: Lists all files in all subdirectories (recursively). All files under the directory will be listed.

-
-n: Uses numeric UID and GID instead of names.

There are some other options that will not be demonstrated here; you can try them out yourself.
-
-1: Outputs one file per line. -
-r: Reverses the sorting of directories. -
-k: Displays the file size in kilobytes. ls –alk
specifies files. -
-i: Outputs the inode index information of the file. For example, ls –ai
specifies files. -
-t: Sorts by time.
file command
Function: View file types.
Syntax: file [file]
[king@VM-12-11-centos ~]$ file cat.txt
cat.txt: UTF-8 Unicode text #Type is text file
[king@VM-12-11-centos ~]$ file ret #Type is directory
ret: directory
pwd command
Syntax: pwd
Function: Displays the current path.

whoami command
Function: Displays the current user.
Two ways to write it.

cd command
Function: Switch paths.
Usage: cd [path to switch to]
Linux manages files in a multi-branch tree structure, where files and directories on the disk form a directory tree, with each node being a directory or file. The root directory is: /

Relative and Absolute Paths
When we need to find a file, we must know the file’s path and name. For example, to find the text.c
file, since each node has only one parent node, the path from the root directory to the text.c
file is unique. The complete path that uniquely identifies a file is called an absolute path; the above pwd
command displays the absolute path.

On the other hand, a relative path is the path relative to the target location and does not have uniqueness; we can find the file from different locations.

-
Absolute Path: The path from the root directory to the specified file, which uniquely identifies a file.
-
Relative Path: The path from a certain file to the specified file, which cannot uniquely identify a file.
The cd command can switch paths using either relative or absolute paths.
You can switch to a specified directory using the absolute path.

You can also switch using a relative path. The double dot represents the parent path, so cd ..
switches to the parent directory, and cd ../../
switches to the grandparent path.

[king@VM-12-11-centos Linuxclass]$ pwd
/home/king/Linuxclass
[king@VM-12-11-centos Linuxclass]$ cd classcode
[king@VM-12-11-centos classcode]$ ls
a.out ret.txt test test.c
[king@VM-12-11-centos classcode]$ cd test
[king@VM-12-11-centos test]$ cd ../../p1 #Switch to the grandparent directory p1 path
[king@VM-12-11-centos p1]$ pwd
/home/king/Linuxclass/p1
Some friends may have questions: cd ..
can switch to the parent path, so what is the use of cd .
? We are already in the current directory, so we don’t need to switch.
Here we use vim to write a
hello Linux
code, compile it with gcc to generate an executable program called a.out, and to run the executable program, we need the path and file name. Using a dot can represent its path, which is much more convenient than using an absolute path.

The cd command also has two options.
[king@VM-12-11-centos Linuxclass]$ pwd
/home/king/Linuxclass
[king@VM-12-11-centos Linuxclass]$ cd ~ #cd ~ enters the working directory, which is the user's path
/home/king
[king@VM-12-11-centos ~]$ cd - #cd - returns to the most recently accessed path
/home/king/Linuxclass
which command
Syntax: which [command]
Function: View the system path and related information of the command.
[king@VM-12-11-centos Linuxclass]$ which ls
alias ls='ls --color=auto' #alias: giving the command an alias
/usr/bin/ls #Our commands are simplified by the system
[king@VM-12-11-centos Linuxclass]$ which ll
alias ll='ls -l --color=auto' #ll can replace ls -l
/usr/bin/ls
[king@VM-12-11-centos Linuxclass]$ which cd #We also find that commands are under the bin directory
/usr/bin/cd

Commands are essentially programs. In Linux, commands, instructions, and tools are executable programs stored in regular files. Everything in Linux is a file, and our commands are all in the bin directory.

touch command
Function: Create files, change the date of documents or directories, including access time and modification time. We will first understand file creation; we will introduce changing directory dates at the end of the article.
Note that touch creates regular files, not directory files, so you cannot
cd
into them.
[king@VM-12-11-centos test]$ touch file1 file2 file3 #touch can create multiple files at once
[king@VM-12-11-centos test]$ touch file4 #Create a single file
[king@VM-12-11-centos test]$ ls
file1 file2 file3 file4
mkdir command
Function: Create directory files.
mkdir can also create multiple directory files at the same time; you can cd
into them, but you cannot create multiple subdirectories under one directory. Here it shows that it cannot be created.

Option -p: Recursively create multiple subdirectories.

Adding User Trust Relationships
The tree command displays files in a tree structure, which needs to be downloaded using yum.
yum install -y tree
If we are a regular user, we need to add sudo in front, as sudo can temporarily elevate privileges to execute commands as root.
Typing su
alone switches to the root user, requiring the root user’s password.

[ret@VM-12-11-centos ~]$ su
Password: #Enter the root user's password
[root@VM-12-11-centos ret]# whoami #Switch to root user
root
[root@VM-12-11-centos ret]# su ret #The root user can switch to the specified user directly without entering the user password
[ret@VM-12-11-centos ~]$ whoami #Now we have switched to user ret
ret
If sudo yum install -y tree
shows a command similar to the following, it indicates that the current user has not added a trust relationship.

To help the user add a trust relationship as root, we will switch to the root user and execute the following command to add the trust relationship, after which we will be able to use sudo.
echo 'xxx ALL=(ALL) ALL' >> /etc/sudoers (where xxx represents the username)
rmdir command
Function: Delete empty directories, which are directories that do not contain other files.
Here, p1 is not empty, so it cannot be deleted; to do so, use the rm command.

rm command
We generally use the rm command to delete files, as rmdir is a bit redundant.
Function: Delete directories or files.
Options:
-
-f: Force delete. -
-r: Delete directories and all files under them recursively.
To delete a directory, you need to include the -r option; at this point, our p1/p2/p3 will be deleted.

If we want to delete all files in a directory without deleting the directory itself, we can use the wildcard *
.

Be careful not to delete the root directory.
rm -rf / #Be careful not to use this, as it recursively and forcefully deletes the root directory, which may crash the system.
man command
There are many commands in Linux. If we do not remember the usage of a certain command, we can use the man command to inquire about it.
Options:
1 is for ordinary commands.
2 is for system calls, such as open, write, etc. (through this, we can conveniently check what headers are needed to call this function).
3 is for library functions, such as printf, fread.
4 is for special files, which are various device files under /dev.
5 is for the format of files, such as passwd, which explains the meanings of various fields in this file.
6 is reserved for games, defined by various games themselves.
7 is for attachments and some variables, such as global variables like environ.
8 is for system management commands, which can only be used by root, such as ifconfig.
9 is for kernel routines.
We currently only need to understand 1 and 3.
Similarly, man requires us to download it; regular users need to add sudo.
yum install -y man-pages
By default, man without options explains commands.
[king@VM-12-11-centos file11]$ man ls
Press q to exit the current interface.

[king@VM-12-11-centos file11]$ man 3 printf #We can view the printf library function

cp command
Syntax: cp [options] [source file] [destination file] [specified directory]
Function: Copy files or directories.
Common options:
-
-f: Forcefully copy files or directories, regardless of whether the destination file or directory already exists.
-
-r: Recursively process files and subdirectories under the specified directory.
We will copy the file file11 and name it file.

By default, it copies to the current directory, but you can also specify the directory at the end.
mv command
Function: Move files or rename files.
Usage: mv [options] [source file] [destination file]
Options:
-
-f: force, meaning if the destination file already exists, it will not ask and will directly overwrite it.
-
-i: If the destination file already exists, it will ask whether to overwrite!
Here we will move the file file11 to the file directory.

If what follows mv is not a file but a file name, it will rename the file.
For example, rename the file file11 to change.

cat command
Function: View file contents.
Syntax: cat [options] [file]
Options:
-
-b: Number non-empty output lines.
-
-n: Number all output lines.
-
-s: Do not output multiple empty lines.

echo command
Function: Display strings to standard output, which is the screen. Echo defaults to a new line.

Redirection
Can we write strings into files?
The answer is yes. Through redirection, adding the > symbol and the file name after the string will write the string that should have been displayed to standard output into the file. If the file does not exist, it will automatically create the file. This is output redirection.

Writing the string that should have been displayed on the screen into the target file will clear the original content of the file and rewrite it.
At this time, the previous hello Linux has been cleared.

If you want to keep the previous content and write it in, you need to change the output redirection symbol from > to >>.

If the cat command is used without adding a file, it will default to read content from the keyboard and echo it to the display, which is the screen.

The previous cat displaying file content was achieved by input redirection.
[king@VM-12-11-centos ~]$ cat < file #This < symbol can be omitted
AAAAAAA
BBBBBBB
CCCCCCC
more command
Function: Similar to cat, generally used to view logs or long content files.
Options:
-
-n: Number the lines.
-
q: Exit more.
For example, here I write a file with content from 0-1000 lines.
count=0; while [ $count -le 1000 ]; do echo "hello ${count}"; let count++; done > cat.txt
more will first display part of the content. Pressing enter will allow you to continue reading, and pressing q will exit. However, more has a drawback; it can only scroll down, not up, so we generally do not use the more command.
less command
Function: View file contents. The usage of less is more flexible than more, allowing the use of [pageup][pagedown]
(previous and next keys) to scroll back and forth through the file. Additionally, less does not load the entire file before viewing.
Options:
-
-i: Ignore case.
-
-N: Show line numbers.
-
/string: Search for the content of the string upward.
-
?: Search for the content of the string downward.
-
n: Repeat the previous search (related to / or ?).
-
N: Repeat the previous search in reverse (related to / or ?).
-
q: Exit less.
[king@VM-12-11-centos ~]$ less cat.txt

We input /3 to search for character 3, and less will help us mark the content of character 3.

There are other options you can try directly; we won’t demonstrate them here. Press q to exit the less interface.
head command
Function: Display the content of the beginning of a file to standard output, which is the screen. By default, head displays the first 10 lines without parameters.
Options:
-n<number of lines>
[king@VM-12-11-centos ~]$ head cat.txt #Default outputs the first 10 lines
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
[king@VM-12-11-centos ~]$ head -n5 cat.txt #Specifies to output the first 5 lines
hello 0
hello 1
hello 2
hello 3
hello 4
[king@VM-12-11-centos ~]$ head -5 cat.txt #n can be omitted
hello 0
hello 1
hello 2
hello 3
hello 4
tail command
Function: Used to display the end content of a specified file. When we check logs, we definitely want to see the latest content, which is from the tail end, and tail can help us view the most recent content.
Options:
-
-f: Loop read.
-
-n<line number>
: Display line numbers.
tail -f filename
will display the latest content at the end of the filename on the screen and refresh so you can see the latest file content. It is very suitable for viewing logs.
[king@VM-12-11-centos ~]$ tail cat.txt #tail without line number defaults to display the last 10 lines
hello 991
hello 992
hello 993
hello 994
hello 995
hello 996
hello 997
hello 998
hello 999
hello 1000
[king@VM-12-11-centos ~]$ tail -n5 cat.txt #Specifies to display the last 5 lines
hello 996
hello 997
hello 998
hello 999
hello 1000
[king@VM-12-11-centos ~]$ tail -5 cat.txt #n can be omitted
hello 996
hello 997
hello 998
hello 999
hello 1000
So can we display the head and tail of the file? What if we want to display the content in the middle of the file?
For example, if we want to display lines 100 to 110, there are two methods:
One method is to redirect the first 110 lines into a temporary file and then read the last 10 lines. However, this method requires recreating the file, which wastes space and is inefficient.

The answer is yes, the second method uses pipelining.
Pipelining

When using pipes, redirection occurs implicitly by default.
# The # symbol | represents a pipe, passing the result of the execution to the next command.
[king@VM-12-11-centos ~]$ head -110 cat.txt | tail -10
hello 100
hello 101
hello 102
hello 103
hello 104
hello 105
hello 106
hello 107
hello 108
hello 109
date command
Format:
-
%H: Hour (00..23) -
%M: Minute (00..59) -
%S: Second (00..61) -
%X: Equivalent to %H:%M:%S -
%d: Day (01..31) -
%m: Month (01..12) -
%Y: Full year (0000..9999) -
%F: Equivalent to %Y-%m-%d
[king@VM-12-11-centos ~]$ date #date defaults to display
Fri Jan 28 16:55:54 CST 2022
[king@VM-12-11-centos ~]$ date +%s #date +%s displays the timestamp
1643360162
[king@VM-12-11-centos ~]$ date +%F%X #Display current time in YYYY-MM-DD HH:MM:SS format
2022-01-2804:56:08 PM
[king@VM-12-11-centos ~]$ date +%F%X@1643360162 #@timestamp, converts timestamp to standard time
2022-01-2804:56:27 PM@1643360162
We can use _ to distinguish, but it cannot be a space; this writing is incorrect.

cal command
Function: View the calendar.
Options:
-
-3: Displays the calendar for the previous month, current month, and next month.
-
-y: Displays the calendar for the current year.
-
cal defaults to display the calendar for the current month.

[king@VM-12-11-centos ~]$ cal -y 2021 #You can also specify the year to display the calendar for 2021.
find command
Function: Search for files in the file system and perform corresponding actions (which may access the disk). The find command will help us search for corresponding files in the current directory and all subdirectories under that directory. When we traverse a large file system, it may take some time before displaying the corresponding file information, but the next time we use find to search, it will be much faster.
The find command has many options; here is one. To learn more, you can use man find
.
Option: -name: Search by file name.
We search for a file named text in the root directory, but here many are permission denied
because the regular user’s permissions are insufficient, and many files cannot be read.

We search for a file named file in our own directory; it will display all files named file in that directory.

Specify directory.
Syntax: find [starting directory] [-name][filename]

grep command
Function: Text line filtering, search for strings.
Common options:
-
-i: Ignore case, treating upper and lower case as the same.
-
-n: Output line numbers.
-
-v: Inverse selection.
Let’s use the file content for 0-1000 lines as an example with the file cat.txt
.
When displaying the file content, we will display all characters that contain ’90’.

We will use output redirection to write two lines into the file; using -i to ignore case will allow us to search for both lines.

Meanwhile, the grep command also supports regular expressions, allowing you to search for any characters you want. You can try this yourself later.
We will first append two lines to cat.txt
, and using regular expressions will allow us to search for these two lines.
[king@VM-12-11-centos ~]$ echo "he9999" >> cat.txt
[king@VM-12-11-centos ~]$ echo "he9290" >> cat.txt
[king@VM-12-11-centos ~]$ cat cat.txt | grep 'he[0-9]*$'
he9290
he9999
zip/unzip commands
Zip syntax: zip [compressed filename.zip] [target file]
Function: Compress files into .zip format.
Common options:
-
-r: Process all files and subdirectories under the specified directory recursively.
-
Unzip syntax:
unzip [compressed file.zip]
-
-d: Unzip to the specified path.
If we do not add the -r option and directly compress the file, we will take the test file as an example, which has three subdirectories: file, file22, and file33. The size of the compressed file.zip is only 160.

After unzipping, we find that the test file is an empty directory and did not help us compress all files.

So we need to add the -r option. After unzipping, we find that cur.zip’s size is 774, which is significantly larger than the previous 160.

Adding the -d option, we unzip cur.zip to the specified path in the ret file.

tar command
Similar to zip/unzip
, but the unzipped file suffix is .tgz
.
Function: Complete packaging, compression, and decompression.
Options:
-
-c: Establish a parameter command for creating a compressed file (create). -
-x: Unzip a compressed file. -
-t: View the files inside a tarfile. -
-z: Whether it has gzip properties, i.e., whether it needs to be compressed with gzip. -
-j: Whether it has bzip2 properties, i.e., whether it needs to be compressed with bzip2. -
-v: Show files during the compression process! This is commonly used but not recommended for background execution processes! -
-f: Use the filename; please note that after -f, the filename should be immediately followed without adding parameters! -
-C: Unzip to the specified directory.
Usually, we use -czf
and -xzf
together for compression and decompression. Here we will also move the compressed file cur.tgz
to the test directory for decompression.

Here we will decompress the compressed file cur.tgz
to the ret directory using the -C
option.

The previous operations were decompression and compression, while packaging is to organize all the files that need to be compressed together. You can understand it as sorting all the files; the file size does not change.
bc command
Function: Can be understood as a calculator.
It directly displays the calculation results on the screen. Use Ctrl+c
to exit.

uname command
Function: Obtain computer and operating system-related information.
Options:
-
-a: Output detailed information, including kernel name, hostname, kernel version number, kernel version, hardware name, processor type, hardware platform type, and operating system name. -
-r: Output the kernel version number.
By default, uname outputs the kernel name, and hostname outputs the hostname. In fact, we only need the -a option.
The kernel version number: 3 represents the main version number, and 10 represents the sub-version number. The sub-version number is even: stable kernel; odd: testing kernel. The kernel version we use on our server is generally not the latest but rather a classic version that has been used for many years. New versions need to undergo the test of time to prove that the kernel is stable, secure, and efficient before they can be used.

shutdown command
Options:
-
-h: Shut down the system’s services and immediately power off. -
-r: Shut down the system’s services and restart.
–-t second:
indicates to shut down after a few seconds.
For servers, we do not need to shut down.
Here we will supplement the touch command for changing time.
stat command
Function: View detailed information about files.
Usage: stat [file]
Stat checks the detailed information of the test file.

Relevant information about the file:
-
File
: Displays the file name. -
Size
: Displays the file size. -
Blocks
: Total number of data blocks used by the file. -
IO Block
: Size of IO blocks. -
regular file
: File type (regular file). -
Device
: Device number. -
Inode
: Inode number. -
Links
: Number of links. -
Access
: File permissions. -
Gid, Uid
: Gid and Uid of file ownership.
The three times in Linux:
-
Access Time
: Abbreviated as atime, represents the last time the file was accessed. -
Modify Time
: Abbreviated as mtime, represents the last time the file content was modified. -
Change Time
: Abbreviated as ctime, represents the last time the file attributes were modified.
atime: When we use cat to view the file after a period of time, the atime will change when we check again with stat. Of course, for kernels after 2.6, the refresh time for atime has been reset; atime will not be updated immediately but will be automatically updated by the OS after a certain time interval. This is because, compared to modifying file content and attributes, viewing files is the most frequent operation. If atime is refreshed frequently, it will reduce efficiency.

For mtime: After we write hello, all three times change. The change of mtime does not necessarily affect atime, but ctime may change as a result because modifying file content may also modify file attributes. For example, when writing data, the size attribute of the file will be modified.

ctime: The most recent time when file attributes were modified; this line represents the file attributes.


The chgmod command can change file access permissions, thus changing file attributes.
touch command supplement
Options:
-
-a: Change atime and ctime. -
-c or --no-create
: Do not create any documents. -
-d: Use the specified date and time instead of the current time to change atime and mtime. -
-f: This parameter will be ignored; it only addresses compatibility issues with the BSD version of the touch command. -
-m: Change ctime and mtime. -
-r: Set the date and time of the specified document or directory to be the same as that of the reference document or directory. -
-t: Use the specified date and time instead of the current time.
The commonly used options are -d, -a, and -m, while the touch command defaults to modifying all times. Here, touch is an operation on already created files.

Hotkeys in Linux
[Tab]
: Command completion. Pressing once or twice will display the commands starting with wh on the screen.

[c]
: Terminate the current program. For example, if characters are continuously output on the screen due to a dead loop code, you can use Ctrl +c
to terminate it.
[Ctrl d]
: Replaces exit to switch identity. Multiple Ctrl d
will directly exit.

[Ctrl r]
: Search for historical commands based on keywords. If we input r, it will automatically convert to historical commands.

[page up page down]
: Up and down, can browse historical commands. We can directly scroll through the commands we have recently input, which is very convenient.
Command Overview
-
Installation and login commands:
login, shutdown, halt, reboot, install, mount, umount, chsh, exit, last
-
File processing commands:
file, mkdir, grep, dd, find, mv, ls, diff, cat, ln
-
System management related commands:
df, top, free, quota, at, lp, adduser, groupadd, kill, crontab
-
Network operation commands:
ifconfig, ip, ping, netstat, telnet, ftp, route, rlogin, rcp, finger, mail, nslookup
-
System security related commands:
passwd, su, umask, chgrp, chmod, chown, chattr, sudo ps, who
-
Other commands:
tar, unzip, gunzip, unarj, mtools, man, unendcode, uudecode
Do you think this article is helpful? Please share it with more people.
Follow the [Full Stack Developer Community] and add it to favorites to enhance your full stack skills.
This public account will periodically send benefits to everyone, including books, learning resources, etc., so stay tuned!
If you find the push content good, feel free to click on the lower right corner to share it on Moments or bookmark it. Thank you for your support.
Good articles, comments, likes, viewing and sharing all in one go.