Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux File and Directory Management

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.


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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

head

Extract the first few lines of a file

Syntax:



head [-n number] 文件 

Options and parameters:



[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:



[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