🐧 Linux File and Directory Management
The directory structure of the Linux system is tree-like, with the top level being the root directory
<span>/</span>. All files and directories start from<span>/</span>and are added to the system tree through “mounting”.
📂 1. Path Basics: Absolute Path and Relative Path
Before operating the Linux file system, it is essential to understand the two representations of paths:
🧭 1. Absolute Path
A complete path that starts from the root directory
<span>/</span>. No matter where you are in the system, this path can accurately locate the target file or directory.
These paths begin with <span>/</span>, indicating a downward navigation from the top level of the file system (the root directory).
💡 Understanding Tip:
Just like writing an address in real life, from “Country → Province → City → Street → House Number” layer by layer, an absolute path always starts from the root directory, writing out the complete “address” of the file.
🧩 2. Relative Path
A path that does not start from the root directory
<span>/</span>, but describes the target file’s location relative to the current working directory.
Common Symbols:
. : represents the current directory.. : represents the parent directory~ : represents the current user's home directory (e.g., /root or /home/username)
🧭 2. Overview of Common Directory Handling Commands
The common file and directory operation commands are as follows:
-
<span>ls</span>: list directories and files (list files) -
<span>cd</span>: change directory (change directory) -
<span>pwd</span>: display the current directory (print working directory) -
<span>mkdir</span>: create directory (make directory) -
<span>rmdir</span>: remove empty directory (remove directory) -
<span>cp</span>: copy file or directory (copy file) -
<span>rm</span>: remove file or directory (remove) -
<span>mv</span>: move or rename file, directory (move file)
You can use
<span>man [command]</span>to view detailed documentation for each command. For example:<span>man cp</span>
📑 3. Detailed Explanation of Directory Operation Commands
1️⃣ <span><span>ls</span></span> — List Directory Contents
Syntax:
ls [-aAdfFhilnrRSt] [directory name]ls [--color={never,auto,always}]ls [--full-time]
Common Parameter Descriptions:
-
<span>-a</span>: display all files (including hidden files) -
<span>-d</span>: display only the directory itself, not its contents -
<span>-l</span>: output in long format, including permissions, owner, size, time, etc.
Example:
[root@www ~]# ls -al ~
2️⃣ <span><span>cd</span></span> — Change Directory
Syntax:
cd [relative path or absolute path]
Common Examples:
# Using absolute pathcd /root/runoob/# Using relative pathcd ./runoob/# Return to home directorycd ~# Return to parent directorycd ..
💡 Tip: Repeatedly practicing the switching of
<span>cd</span>with relative and absolute paths helps to understand the hierarchical structure of the Linux file system.
3️⃣ <span><span>pwd</span></span> — Display Current Directory
Syntax:
pwd [-P]
Parameter Descriptions:
-
<span>-P</span>: display the real path (if the current path is a symbolic link, it shows the target path)
Example:
[root@www ~]# cd /var/mail[root@www mail]# pwd /var/mail[root@www mail]# pwd -P /var/spool/mail
✨
<span>pwd -P</span>can avoid confusion from symbolic link paths, and it is recommended to use it in scripts.
4️⃣ <span><span>mkdir</span></span> — Create Directory
Syntax:
mkdir [-mp] directory name
Parameter Descriptions:
-
<span>-m</span>: directly specify permissions (not restricted by<span>umask</span>) -
<span>-p</span>: recursively create multi-level directories
Example:
mkdir -p test1/test2/test3/test4mkdir -m 711 secure_dir
If there is no
<span>-p</span>parameter, the system will not automatically create the parent directories.
5️⃣ <span><span>rmdir</span></span> — Remove Empty Directory
Syntax:
rmdir [-p] directory name
Parameter Descriptions:
-
<span>-p</span>: recursively remove multi-level empty directories
Example:
rmdir testdirmdir -p test1/test2/test3/test4
⚠️ Note:
<span>rmdir</span>can only remove empty directories. If there are files in the directory, please use<span>rm -r</span>.
6️⃣ <span><span>cp</span></span> — Copy File or Directory
Syntax:
cp [-adfilprsu] source file target filecp [options] source1 source2 ... target directory
Common Parameter Descriptions:
-
<span>-a</span>: equivalent to<span>-pdr</span><span>,</span>preserves attributes, recursively copies, does not dereference links -
<span>-i</span>: prompt before overwrite (interactive mode) -
<span>-r</span>: recursively copy directories -
<span>-p</span>: preserve file permissions and timestamps -
<span>-u</span>: copy only if the source file is newer
Example:
cp ~/.bashrc /tmp/bashrccp -i ~/.bashrc /tmp/bashrc
7️⃣ <span><span>rm</span></span> — Remove File or Directory
Syntax:
rm [-fir] file or directory
Parameter Descriptions:
-
<span>-f</span>: force delete, do not prompt for errors -
<span>-i</span>: interactive mode (prompt before deletion) -
<span>-r</span>: recursively delete directories and contents
Example:
rm -i bashrcrm -rf testdir
⚠️ Dangerous Operation Warning!
<span>rm -rf /</span>will directly delete the entire system, please confirm the path before operating!
8️⃣ <span><span>mv</span></span> — Move or Rename File
Syntax:
mv [-fiu] source file target filemv [options] source1 source2 ... target directory
Parameter Descriptions:
-
<span>-f</span>: overwrite directly, do not ask -
<span>-i</span>: prompt before overwrite -
<span>-u</span>: move only if the source file is newer
Example:
mv bashrc mvtest/mv mvtest mvtest2
📜 4. File Content Viewing Commands
Common file viewing commands in the Linux system include:
-
<span>cat</span>: display file content from the first line -
<span>tac</span>: display file content from the last line (reverse of cat) -
<span>nl</span>: display line numbers -
<span>more</span>: display in pages, can scroll down -
<span>less</span>: enhanced version of more, can scroll up and down -
<span>head</span>: display the first few lines of a file -
<span>tail</span>: display the last few lines of a file or monitor file changes in real-time
You can also use
<span>man [command]</span>to view complete explanations.
1️⃣ <span><span>cat</span></span> / <span><span>tac</span></span> — Display File in Order or Reverse
Syntax:
cat [options] filetac [options] file
Example:
cat /etc/issuetac /etc/issue
<span>cat</span>displays from the first line;<span>tac</span>displays from the last line. Suitable for quickly viewing short files.
2️⃣ <span><span>nl</span></span> — Display File Content with Line Numbers
Syntax:
nl [-bnw] file
Parameter Descriptions:
-
<span>-b a</span>: number all lines -
<span>-b t</span>: ignore empty lines (default) -
<span>-n ln</span>: line numbers displayed on the left -
<span>-w</span>: set line number width
Example:
nl /etc/issue
3️⃣ <span><span>more</span></span> — View File in Pages
Common Operation Keys:
-
Space: scroll down one page
-
Enter: scroll down one line
-
<span>/keyword</span>: search for keywords downwards -
<span>b</span>or<span>Ctrl + b</span>: scroll up one page -
<span>q</span>: exit
Example:
more /etc/man_db.config
4️⃣ <span><span>less</span></span> — Enhanced File Viewer
Common Operation Keys:
-
Space or PageDown: scroll down one page
-
PageUp: scroll up one page
-
<span>/keyword</span>: search downwards -
<span>?keyword</span>: search upwards -
<span>n</span>/<span>N</span>: repeat last search (in the opposite direction) -
<span>q</span>: exit
Example:
less /etc/man.config
5️⃣ <span><span>head</span></span> / <span><span>tail</span></span> — View the First or Last Few Lines of a File
Syntax:
head [-n number] filetail [-n number] file
Parameter Descriptions:
-
<span>-n</span>: specify the number of lines to display -
<span>-f</span>:<span>tail</span>continuously monitors file changes (commonly used for logs)
Example:
head -n 20 /etc/man.configtail -f /var/log/messages
🧩
<span>tail -f</span>is commonly used for real-time log output viewing, such as Nginx, system logs, etc.
🎯 Summary
File and directory management in Linux is the foundation of daily operations and development. Mastering commands like
<span>ls</span>,<span>cd</span>,<span>pwd</span>,<span>mkdir</span>,<span>rm</span>,<span>cp</span>,<span>mv</span>, etc., will make you proficient in system operations.It is recommended to practice more hands-on and combine with
<span>man</span>documentation to deepen understanding.