Linux File and Directory Management
We know that Linux's directory structure is a tree structure, with the top-level directory being the root directory./。
Other directories can be added to the tree through mounting, and removed by unmounting.
Before starting this tutorial, we need to know what absolute paths and relative paths are.
Absolute path:
The path is written starting from the root directory/For example: the directory /usr/share/doc.-
Relative path:
The path is not written starting from/For example, to go from /usr/share/doc to /usr/share/man, you can write:cd ../manThis is how you write a relative path.
Common commands for handling directories
Next, let's look at a few common commands for handling directories:
- ls (full English: list files): List directories and file names
- cd (full English: change directory): Switch directory
- pwd (full English: print work directory): Display the current directory
- mkdir (full English: make directory): Create a new directory
- rmdir (full English: remove directory): Remove an empty directory
- cp (full English: copy file): Copy files or directories
- rm (full English: remove): Remove files or directories
- mv (full English: move file): Move files and directories, or rename files and directories
You can useman [command]to view the usage documentation for each command, e.g., man cp.
ls (List directories)
In Linux systems, the ls command is probably the most frequently run.
Syntax:
[root@www ~]# ls [-aAdfFhilnrRSt] 目录名称
[root@www ~]# ls [--color={never,auto,always}] 目录名称
[root@www ~]# ls [--full-time] 目录名称
Options and parameters:
- -a: List all files, including hidden files (files starting with .) (common)
- -d: List only the directory itself, not the file data within the directory (common)
- -l: List in a long data string, including file attributes and permissions, etc. (common)
List all files under the directory (including attributes and hidden files)
[root@www ~]# ls -al ~
cd (Change directory)
cd is an abbreviation for Change Directory, which is a command used to change the working directory.
Syntax:
cd [相对路径或绝对路径]
#使用 mkdir 命令创建 example 目录 [root@www ~]# mkdir example #使用绝对路径切换到 example 目录 [root@www ~]# cd /root/example/ #使用相对路径切换到 example 目录 [root@www ~]# cd ./example/ # 表示回到自己的家目录,亦即是 /root 这个目录 [root@www example]# cd ~ # 表示去到目前的上一级目录,亦即是 /root 的上一级目录的意思; [root@www ~]# cd ..
Next, after practicing a few times, you should be able to understand the cd command well.
pwd (Display the current directory)
pwd isPrint Working Directorythe abbreviation for, which is the command to display the current directory.
[root@www ~]# pwd [-P]
Options and parameters:
- -P: Display the actual path, rather than using a link path.
Example: Simply display the current working directory:
[root@www ~]# pwd /root <== 显示出目录啦~
Example: Display the actual working directory, not just the directory name of the link file itself.
[root@www ~]# cd /var/mail <==注意,/var/mail是一个链接档 [root@www mail]# pwd /var/mail <==列出目前的工作目录 [root@www mail]# pwd -P /var/spool/mail <==怎么回事?有没有加 -P 差很多~ [root@www mail]# ls -ld /var/mail lrwxrwxrwx 1 root root 10 Sep 4 17:54 /var/mail -> spool/mail # 看到这里应该知道为啥了吧?因为 /var/mail 是链接档,链接到 /var/spool/mail # 所以,加上 pwd -P 的选项后,会不以链接档的数据显示,而是显示正确的完整路径啊!
mkdir (Create a new directory)
If you want to create a new directory, then use mkdir (make directory).
Syntax:
mkdir [-mp] 目录名称
Options and parameters:
- -m: Configure the file permissions! Set them directly, no need to worry about the default permissions (umask) ~
- -p: Helps you directly create the required directory (including parent directories) recursively!
Example: Please go to /tmp and try creating several new directories:
[root@www ~]# cd /tmp [root@www tmp]# mkdir test <==创建一名为 test 的新目录 [root@www tmp]# mkdir test1/test2/test3/test4 mkdir: cannot create directory `test1/test2/test3/test4': No such file or directory <== 没办法直接创建此目录啊! [root@www tmp]# mkdir -p test1/test2/test3/test4
Adding the -p option can help you create multiple levels of directories automatically!
Example: Create a directory with permissionsrwx--x--xdirectory.
[root@www tmp]# mkdir -m 711 test2 [root@www tmp]# ls -l drwxr-xr-x 3 root root 4096 Jul 18 12:50 test drwxr-xr-x 3 root root 4096 Jul 18 12:53 test1 drwx--x--x 2 root root 4096 Jul 18 12:54 test2
For the permissions above, if you don't add -m to force configuration, the system will use default attributes.
If we use -m, as in the above example we gave -m 711 to give the new directory drwx--x--x permissions.
rmdir (Remove an empty directory)
Syntax:
rmdir [-p] 目录名称
Options and parameters:
- -p :Starting from this directory, delete multiple levels of empty directories at once.
Delete the example directory
[root@www tmp]# rmdir example/
Delete the directories created in the mkdir example (under /tmp)!
[root@www tmp]# ls -l <==看看有多少目录存在? drwxr-xr-x 3 root root 4096 Jul 18 12:50 test drwxr-xr-x 3 root root 4096 Jul 18 12:53 test1 drwx--x--x 2 root root 4096 Jul 18 12:54 test2 [root@www tmp]# rmdir test <==可直接删除掉,没问题 [root@www tmp]# rmdir test1 <==因为尚有内容,所以无法删除! rmdir: `test1': Directory not empty [root@www tmp]# rmdir -p test1/test2/test3/test4 [root@www tmp]# ls -l <==您看看,底下的输出中test与test1不见了! drwx--x--x 2 root root 4096 Jul 18 12:54 test2
Using the -p option, you can immediately delete test1/test2/test3/test4 all at once.
However, note that rmdir can only delete empty directories; you can use the rm command to delete non-empty directories.
cp (Copy files or directories)
cp is for copying files and directories.
Syntax:
[root@www ~]# cp [-adfilprsu] 来源档(source) 目标档(destination) [root@www ~]# cp [options] source1 source2 source3 .... directory
Options and parameters:
-a:Equivalent to -pdr; for pdr, please refer to the following explanations; (common)
-d:If the source file is a link file attribute, then copy the link file attribute rather than the file itself;
-f:Means force; if the destination file already exists and cannot be opened, remove it and try again;
-i:If the destination file already exists, it will ask before overwriting (common)
-l:Create a hard link to the link file, rather than copying the file itself;
-p:Copy the file attributes together with the file, rather than using default attributes (commonly used for backups);
-r:Recursively copy continuously, used for copying directories; (common)
-s:Copy as a symbolic link, that is, a "shortcut" file;
-u:Only upgrade destination if destination is older than source!
As root, copy .bashrc from the root directory to /tmp and name it bashrc
[root@www ~]# cp ~/.bashrc /tmp/bashrc [root@www ~]# cp -i ~/.bashrc /tmp/bashrc cp: overwrite `/tmp/bashrc'? n <==n不覆盖,y为覆盖
rm (Remove files or directories)
Syntax:
rm [-fir] 文件或目录
Options and parameters:
- -f: Means force, ignores non-existent files, no warning messages will appear;
- -i: Interactive mode, asks the user whether to proceed before deletion
- -r: Recursive deletion! Most commonly used for deleting directories! This is a very dangerous option!!!
Delete the bashrc created in the cp example just now!
[root@www tmp]# rm -i bashrc rm: remove regular file `bashrc'? y
If you add the -i option, it will actively ask, preventing you from deleting the wrong file name!
mv (Move files and directories, or rename)
Syntax:
[root@www ~]# mv [-fiu] source destination [root@www ~]# mv [options] source1 source2 source3 .... directory
Options and parameters:
- -f: Means force; if the destination file already exists, it will overwrite directly without asking;
- -i: If the destination file already exists, it will ask whether to overwrite!
- -u: Only update if the destination file already exists and source is newer
Copy a file, create a directory, move the file into the directory
[root@www ~]# cd /tmp [root@www tmp]# cp ~/.bashrc bashrc [root@www tmp]# mkdir mvtest [root@www tmp]# mv bashrc mvtest
Moving a file to a directory is done like this!
Rename the directory just now to mvtest2
[root@www tmp]# mv mvtest mvtest2
Linux File Content Viewing
In Linux systems, use the following commands to view file contents:
- cat: Display file content starting from the first line
- tac: Display starting from the last line; you can see that tac is cat written backwards!
- nl: When displaying, also output line numbers!
- more: Display file content page by page
- less: Similar to more, but better than more because it can page backward!
- head: Only look at the first few lines
- tail: Only look at the last few lines
You can useman [command]to view the usage documentation for each command, e.g., man cp.
cat
Display file content starting from the first line
Syntax:
cat [-AbEnTv]
Options and parameters:
- -A : Equivalent to the combined options of -vET, can list some special characters instead of just whitespace;
- -b : Display line numbers, only for non-blank lines, blank lines are not numbered!
- -E : Display the end-of-line byte $ at the end;
- -n : Print line numbers, blank lines also get line numbers, unlike the -b option;
- -T : Display the [tab] key as ^I;
- -v : List some special characters that are not visible
View the contents of the /etc/issue file:
[root@www ~]# cat /etc/issue CentOS release 6.4 (Final) Kernel \r on an \m
tac
The tac command is the exact opposite of cat; it displays the file content starting from the last line. You can see that tac is cat written backwards! For example:
[root@www ~]# tac /etc/issue Kernel \r on an \m CentOS release 6.4 (Final)
nl
Display line numbers
Syntax:
nl [-bnw] 文件
Options and parameters:
- -b : Specifies the method of numbering lines, mainly two types:
-b a : Indicates that regardless of whether it is a blank line, line numbers are listed as well (similar to cat -n);
-b t : If there are blank lines, do not list line numbers for the blank lines (default); - -n : The method of displaying line numbers, mainly three types:
-n ln : Line numbers are displayed at the far left of the screen;
-n rn : Line numbers are displayed at the far right of their own field, without adding 0;
-n rz : Line numbers are displayed at the far right of their own field, with 0 added; - -w : The number of digits occupied by the line number field.
Example 1: Use nl to list the contents of /etc/issue
[root@www ~]# nl /etc/issue
1 CentOS release 6.4 (Final)
2 Kernel \r on an \m
more
Turn pages one by one
[root@www ~]# more /etc/man_db.config # # Generated automatically from man.conf.in by the # configure script. # # man.conf from man-1.6d ....(中间省略).... --More--(28%) <== 重点在这一行喔!你的光标也会在这里等待你的命令
During the operation of the more program, there are several keys you can press:
- Space key (space): scroll down one page;
- Enter : scroll down 'one line';
- /string : search downward for the keyword 'string' in the displayed content;
- :f : immediately display the filename and the currently displayed line number;
- q : immediately quit more and stop displaying the file content.
- b or [ctrl]-b : scroll back one page, but this action only works for files, not for pipes.
less
Turn pages one by one. The following example outputs the contents of the /etc/man.config file:
[root@www ~]# less /etc/man.config # # Generated automatically from man.conf.in by the # configure script. # # man.conf from man-1.6d ....(中间省略).... : <== 这里可以等待你输入命令!
Commands that can be entered while less is running:
- Space key : scroll down one page;
- [pagedown]: scroll down one page;
- [pageup] : scroll up one page;
- /string : function to search downward for 'string';
- ?string : function to search upward for 'string';
- n : repeat the previous search (related to / or ?!)
- N : repeat the previous search in reverse (related to / or ?!)
- q : quit the less program;
head
Take out the first few lines of a file
Syntax:
head [-n number] 文件
Options and parameters:
- -n : followed by a number, meaning how many lines to display
[root@www ~]# head /etc/man.config
By default, it displays the first 10 lines! To display the first 20 lines, do this:
[root@www ~]# head -n 20 /etc/man.config
tail
Take out the last few lines of a file
Syntax:
tail [-n number] 文件
Options and parameters:
- -n : followed by a number, meaning how many lines to display
- -f : indicates continuous monitoring of the filename given, tail will not stop monitoring until [ctrl]-c is pressed
[root@www ~]# tail /etc/man.config # 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样: [root@www ~]# tail -n 20 /etc/man.configOther extensions