Linux File and Directory Management
We know that the Linux directory structure is tree-like, with the top-level directory being the root directory /.
Other directories can be added to the tree through mounting and removed through unmounting.
Before starting this tutorial, we need to know what absolute and relative paths are.
-
Absolute path:
The way to write a path, starting from the root directory / , for example, the directory /usr/share/doc. - ** **
Relative path:
The way to write a path that does not start from / , for example, when going from /usr/share/doc to /usr/share/man, you can write: cd ../man This is how a relative path is written.
Common commands for handling directories
Next, let's look at a few common commands for handling directories:
- ls (short for list files): list directories and file names
- cd (short for change directory): change directories
- pwd (short for print work directory): display the current directory
- mkdir (short for make directory): create a new directory
- rmdir (short for remove directory): delete an empty directory
- cp (short for copy file): copy files or directories
- rm (short for remove): delete files or directories
- mv (short for move file): move files and directories, or rename files and directories
You can use the man [command] to view the usage documentation of each command, for example: 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 .) (commonly used)
- -d: List only the directory itself, rather than the files inside it (commonly used)
- -l: List in long format, including file attributes, permissions, and other data (commonly used)
List all files in a directory (including attributes and hidden files)
[root@www ~]# ls -al ~
cd (change directory)
cd is short for Change Directory; it is the command used to change the working directory.
Syntax:
cd [相对路径或绝对路径]
#Use mkdir command to create runoob directory
[root@www ~]# mkdir runoob
#Switch to the runoob directory using absolute path
[root@www ~]# cd /root/runoob/
#Change to the runoob directory using a relative path
[root@www ~]# cd ./runoob/
# Represents returning to the home directory, which is /root
[root@www runoob]# cd ~
# Represents going to the current parent directory, which means the directory above /root;
[root@www ~]# cd ..
After a few more operations, you should be able to understand the cd command well.
pwd (display the current directory)
pwd is short for Print Working Directory , that is, the command that displays the current directory.
[root@www ~]# pwd [-P]
Options and parameters:
- -P: Display the actual path rather than a linked (link) path.
Example: Simply display the current working directory:
[root@www ~]# pwd
/root <== 显示出目录啦~
The example displays 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
# Seeing this, you should know why, right? Because /var/mail is a symlink, linking to /var/spool/mail
# So, after adding the pwd -P option, it won't display the data of the link file, but the correct full path!
mkdir (create a new directory)
If you want to create a new directory, use mkdir (make directory).
Syntax:
mkdir [-mp] 目录名称
Options and parameters:
- -m: Configure the file's permissions! Set them directly, without having to rely on the default permissions (umask).
- -p: Helps you recursively create the required directories (including parent directories) directly!
Example: 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
With the -p option added, it will automatically create multiple levels of directories for you!
Example: Create a directory with permissions of rwx--x--x .
[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 part above, if -m is not added to force the attribute configuration, the system will use the default attributes.
If we use -m, as in the example above, we give -m 711 to grant the new directory the permissions drwx--x--x.
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 runoob directory
[root@www tmp]# rmdir runoob/
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
With the -p option, you can delete test1/test2/test3/test4 all at once.
However, note that this rmdir can only delete empty directories; you can use the rm command to delete non-empty directories.
cp (copy files or directories)
cp means 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 descriptions (commonly used)
-
-d:If the source file is a link file, copy the link file's attributes rather than the file itself;
-
-f:means force; if the target file already exists and cannot be opened, remove it and try again;
-
-i:If the destination file (destination) already exists, you will be asked before overwriting (commonly used)
-
-l:Create a hard link (hard link) file rather than copying the file itself;
-
-p:Copy the file along with its attributes rather than using default attributes (commonly used for backups);
-
-r:Recursively copy continuously, used for copying directories (commonly used)
-
-s:Copy as a symbolic link (symbolic link), that is, a "shortcut" file;
-
-u:Only update destination if it is older than source!
As root, copy the .bashrc file in 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; ignore files that do not exist, and no warning messages will appear;
- -i: interactive mode, asks the user whether to proceed before deleting
- -r: recursive deletion! Most commonly used for deleting directories! This is a very dangerous option!!!
Delete the bashrc file just created in the cp example!
[root@www tmp]# rm -i bashrc
rm: remove regular file `bashrc'? y
If you add the -i option, it will actively ask you, to avoid 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 target file already exists, it will be overwritten without asking;
- -i: if the target file (destination) already exists, you will be asked whether to overwrite!
- -u: only update (update) if the target file already exists and source is newer
Copy a file, create a directory, and 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
This is how you move a file into a directory!
Rename the directory just now to mvtest2
[root@www tmp]# mv mvtest mvtest2
Linux File Content Viewing
Linux systems use the following commands to view file contents:
- cat display file contents starting from the first line
- tac display starting from the last line; you can see that tac is cat written backwards!
- nl display with line numbers output along the way!
- more display file contents page by page
- less similar to more, but better than more in that it can page backwards!
- head view only the first few lines
- tail view only the last few lines
You can use *man [command]* to view the usage documentation of each command, for example: man cp.
cat
Display file contents starting from the first line
Syntax:
cat [-AbEnTv]
Options and parameters:
- -A: equivalent to the combined -vET options, which can list some special characters instead of just whitespace;
- -b: list line numbers, showing line numbers only for non-blank lines; blank lines are not numbered!
- -E: display the ending line-break byte $;
- -n: print out line numbers, including blank lines, which are also numbered, unlike the -b option;
- -T: display the [tab] key as ^I;
- -v: list some special characters that are not normally 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; file contents are displayed 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: there are two main ways to specify line numbering:
-b a: number all lines, whether blank or not (similar to cat -n);
-b t: if there are blank lines, do not number them (default); - -n: there are three main ways to display line numbers:
-n ln: display line numbers at the far left of the screen;
-n rn: display line numbers at the far right of their column, without adding 0;
-n rz: display line numbers at the far right of their column, adding 0; - -w: the number of characters occupied by the line number column.
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
Page through one page at a time
[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%) <== 重点在这一行喔!你的光标也会在这里等待你的命令
While the more program is running, there are several keys you can press:
- Space key (space): page down one page;
- Enter : page down one line;
- /string : search downward for the keyword "string" within the displayed content;
- :f : immediately display the file name and the current line number;
- q : immediately leave more and stop displaying the file contents.
- b or [ctrl]-b: page backward, but this action only works for files, not for pipes.
less
Page through one page at a time; 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 : page down one page;
- [pagedown]: page down one page;
- [pageup] : page up one page;
- /string : search downward for "string";
- ?string : search upward for "string";
- n : repeat the previous search (related to / or ?!)
- N : repeat the previous search in reverse (related to / or ?!)
- q : leave the less program;
head
Extract the first few lines of a file
Syntax:
head [-n number] 文件
Options and parameters:
- -n: followed by a number, indicating how many lines to display
[root@www ~]# head /etc/man.config
By default, the first 10 lines are displayed! To display the first 20 lines, do this:
[root@www ~]# head -n 20 /etc/man.config
tail
Extract the last few lines of a file
Syntax:
tail [-n number] 文件
Options and parameters:
- -n: followed by a number, indicating how many lines to display
- -f: continuously monitor the file name that follows; tail's monitoring will not end until [ctrl]-c is pressed
[root@www ~]# tail /etc/man.config
# By default, it shows the last ten lines! To show the last 20 lines, you have to do this:
[root@www ~]# tail -n 20 /etc/man.config