This article is based on notes taken during a freshman Linux class, aimed at helping students review after class. The content is derived from the book “Linux Network Operating System Configuration and Management” edited by Teacher Xia Liqin. I hope everyone can learn together and make progress.
1. Usage of Directory Operation Commands
1. View the current working directory (Print Working Directory) – pwdCommand
Command format:pwd
pwd
2. Change the working directory (Change Directory) – cdCommand
Command format:cd [path]
cd /usr/local/lib // Change directory to /usr/local/lib
dcd ../src // Enter the src subdirectory of the parent directory
cd - // Return to the last directory
cd ~student // Enter the home directory of the user named student
cd // Enter the current user's home directory
cd ~ // Enter the working directory (home directory) of the root user when logged in
cd .. // Enter the parent directory of the current directory
3. List (list) – Display directory contents – lsCommand
Command format:ls [options] filename or directory name
Common options:
ls // List files and subdirectories in the current directory
ls -a // List all files including hidden files starting with '.'
ls -hl // Display file and directory sizes in K
ls -l // List file permissions, owners, sizes, modification times, and names in the current directory
ls -R // List filenames in the current directory and all its subdirectories
4. Create a directory (make directory) – mkdirCommand
Command format:mkdir [-p] [/path/]directory name
-p – Quickly create each directory specified in the directory structure; existing directories will not be overwritten.
mkdir dir1
mkdir -p dir2/bak
2. File Commands
1. Create a file – touch command
Command format: touch [options] filename or directory name list
Function: If the specified file or directory exists, it modifies the date and time to the current system date and time.
If the specified file does not exist, it creates an empty file with the specified filename.
If the specified file exists, it changes the file’s date.
2. Copy (copy) files or directories – cp command
Command format: cp [options] source file or directory target file or directory
Function: Copying directories or files.
When copying files, you can rename them.
-i option, prompts the user for confirmation when overwriting files or directories with the same name
-r option If the source file is a directory, cp will recursively copy all subdirectories and files under that directory; the target must be a directory name
3. Move (move) files or directories – mv command
Command format: mv [options] source directory or filename target directory or filename
Function: Move files or directories. If the source and target paths are different, it moves the directory or file.
Rename files or directories; if the paths are the same, only the filenames or directory names differ, it renames the file or directory.
Options:
-b option If there are duplicate names after moving, it will add a ~ to the filename.
-i Before overwriting the target file, it will prompt the user for confirmation. Answering y will overwrite the target file, making it an interactive copy.
4. Delete (remove) files or directories – rm command
Format: rm [options] filename and path
Function: Delete files or directories, can include one or more filenames (separated by spaces) or use wildcards to delete multiple files or directories.
Options:
-r Recursively delete the entire directory tree
-i Interactive deletion, requires user confirmation for each file or directory deletion
High-risk operation warning! rm -rf / Delete the entire system! Absolutely prohibited!
rm -rf /* Same as above, catastrophic consequences
rm -rf ./ * Delete all contents of the current directory (may cause accidental deletion)
rm -f important.conf Force delete without prompt, cannot be undone
5. Find files or directories – find command
Command format: find [search directory] [search condition expression]
“Search condition expressions” mainly include the following types:
-name filename Find files with the specified name. Wildcards '*' and '?' can be used in the filename.
-user username Find files belonging to the specified user.
-group groupname Find files belonging to the specified group.
-type filetype Find files of the specified type. File type symbols include: f (regular file), d (directory), b (block device file), c (character device file), l (symbolic link file), p (pipe file), etc.
-size [+|-]n[k] Find files based on size. Here, "n" is the file size; the symbol "+n" indicates finding files larger than n; "-n" indicates finding files smaller than n; k, M, G represent kilobytes, megabytes, and gigabytes respectively.
Example:
Find files ending with ".conf" in the /etc/ directory: find /etc -name ".conf"
Find files owned by the root user and mail group on the computer: find / -user root -group mail
Find files in the /boot directory larger than 2MB and starting with "vm": find /boot -size +2M -a -name "vm*"
3. File Content Browsing Commands
1. View the content of text files – cat command
Command format: cat [options] filename list
Note: Filenames can use wildcards.
Options:
-n // Number all lines in the output.
-b // Number only non-empty lines in the output.
Note: View readable files – text files; viewing binary files will display garbled text, and you will need to reset to recover, e.g., cat /bin/ls
2. View file content page by page – more command
Command format: more [options] filename
Options:
-number - Only applicable to the more command, used to specify the number of lines per page when displaying.
Interactive operation method: Press Enter to scroll down line by line, press the space bar to scroll down a page, when reaching the end of the file, more will exit automatically, press q to exit.
3. View file content page by page – less command
Command format: less [options] filename
Options:
+num - Specify to start displaying from line num of the file.
-c - Clear the screen from the top and then display the file content.
-N - Only applicable to the less command, which adds output line numbers before each line.
Interactive operation method: Press Enter to scroll down line by line, press the space bar to scroll down a page, press b to scroll up a page, when reaching the end of the file, less will exit automatically, press q to exit.
4. View the beginning of a file – head command
Command format: head [options] filename
Common options include:
-num - Specify how many lines of the file to display; if not specified, it defaults to displaying ten lines.
head /etc/idmapd.conf // Display the first 10 lines of the file
5. View the end of a file – tail command
Command format: tail [options] filename
Common options include:
-num - Specify how many lines of the file to display; if not specified, it defaults to displaying ten lines.
-f - Makes tail continuously read and display the latest content of the file, monitoring changes in file content. This provides real-time monitoring effects.
The tail command is more commonly used to view system log files to observe important system messages, especially when used with the -f option, tail will automatically display new messages from the opened file to the screen, thus tracking changes in the end of the log file content until the [Ctrl+C] key is pressed to terminate the display and tracking.
6. Retrieve and filter file content – grep command
Function: Search for and display lines containing the specified string in the specified file.
Command format: grep [options] string or condition expression filename
Options:
-i - Ignore case when searching.
-v - Invert search, output lines that do not match the search condition.
In the grep command, you can directly specify the keyword string as the search condition, or use complex condition expressions, for example: the character “^” indicates the start of a line.
4. File Packaging and Unpacking Commands – tar
The tar command can package a group of files and directories to be backed up into a single file for easy storage and network transmission. The tar command has built-in multiple options to achieve compression or decompression of tar files.
When needed, it can be restored from the .tar file.
Command format:
tar [options] package filename source file or directory list
tar [options] package filename [-C target folder]
Common options:
c Create a .tar format package file
x Extract a .tar format package file
t View the file list in the package
v Indicates detailed prompt information during command execution
f Package filename Used to specify the package filename. When used with the -c option, the created tar package file uses the filename specified by this option; when used with the -x option, it releases the tar package file specified by this option.
p When packaging, retain the permissions of files and directories
z Call the gzip program to compress or decompress files in gzip format
i Call the bzip2 program to compress or decompress files in bzip2 format
j Use xz compression (.tar.xz). The compression ratio of xz is usually higher than that of bzip2.
C Directory path name Specify the target location for extraction when releasing the package.
1. Create a (non-compressed) package file
Command format: tar cvf package filename list of files or directories to be packaged
Function: Back up one or more specified files or directories into a specified package file.
2. Create a compressed package file
To save storage space, it is often necessary to generate compressed format tar package files. The tar command supports three different compression methods:
Command format: tar c [z | j | J] f compressed package filename list of files or directories to be backed up
3. Extract package files to a specified directory
Command format: tar x [z | j | J] [v] f package filename [ -C target location]
5. Other System Commands
1. Switch User – su
Format: su [ – ] [ user ]
// The ” – ” switches the current user’s environment variables as well.
Function: Change to another user’s identity (privilege escalation).
sudo Execute commands as another user (usually root).
2. echo
Format: echo [string / $variable]
Function: Used to output strings or the values extracted from variables in the terminal.
3. Redirection Operators – “>”, “>>”, “<“, “<<“
In the Linux system
The default input device (standard input) is the keyboard.
The default output device (standard output) is the screen.
Using redirection operators, you can redefine the default input and output devices involved in commands, i.e., redirection operators can redirect the command input and output data streams from the default devices to other locations.
Redirection operators themselves are not commands, but special symbols attached to commands that can change the input and output objects of commands.
” > “, ” >> ” are called output redirection operators,
” < “, ” << ” are called input redirection operators.
(1) >, >> Input redirection symbols
The output result of the command is compared between using redirection operators and not using redirection operators as follows:
Display the string ‘good good study’ on the screen this is web
echo ‘good good study’
Write ‘good good study’ to index.html file
echo ‘good good study’ > index.html
Write the content of index.html file to f2 file
cat index.html > f2
Merge the contents of index.html and f2 into f3:
cat index.html f2 >> f3
Write ‘day day up’ to f3:
echo ‘day day up’ >> f3
4. Pipe Operator – “|”
A pipe is a sequence of commands separated by the symbol “|”.
The role of the pipe symbol “|”: It takes the output of the previous command and uses it as the input for the next command through an invisible “pipe”, i.e., it allows the output data result of the previous command to serve as the data source parameters needed by the subsequent command.
Usage scenarios for the pipe symbol:
When the output content is large, for easier browsing, you can pass the output content through the pipe operator to the more command for paginated viewing, or pass it to the grep command to view specified objects.
ls -al | more
ls -al /etc | grep ssh
Operation notes can be viewed by scanning the QR code
