Home>Article>Operation and Maintenance> What is the linux file directory command?
Linux file directory commands include: 1. ls, used to list directories and file names; 2. cd, used to switch directories; 3. pwd, used to display the current directory; 4. mkdir, used Used to create new directories; 5. rmdir, used to delete empty directories; 6. cp, used to copy files or directories; 7. rm, used to delete files or directories, etc.
#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
Common commands for processing files and directories
Next let’s look at a few common commands for processing directories:
You can useman [command]to view the details of each command Use documentation, such as: man cp.
In Linux systems, the ls command may be the most commonly 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 the home directory (including attributes and hidden files)
[root@www ~]# ls -al ~
cd is the abbreviation of Change Directory, which is the command used to change the working directory.
Syntax:
cd [相对路径或绝对路径]
#使用 mkdir 命令创建 runoob 目录 [root@www ~]# mkdir runoob #使用绝对路径切换到 runoob 目录 [root@www ~]# cd /root/runoob/ #使用相对路径切换到 runoob 目录 [root@www ~]# cd ./runoob/ # 表示回到自己的家目录,亦即是 /root 这个目录 [root@www runoob]# cd ~ # 表示去到目前的上一级目录,亦即是 /root 的上一级目录的意思; [root@www ~]# cd ..
You should be able to understand the cd command well after a few more operations.
pwd is the abbreviation ofPrint Working Directory, which is the command to display the current directory.
[root@www ~]# pwd [-P]
Options and parameters:
Example: Simply display the current working directory:
[root@www ~]# pwd /root <== 显示出目录啦~
Example displays the actual working directory, not 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 的选项后,会不以连结档的数据显示,而是显示正确的完整路径啊!
If you want to create a new directory, use mkdir (make directory).
Syntax:
mkdir [-mp] 目录名称
Options and parameters:
Example: Please go to /tmp and try to create several new directories to see:
[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, you can create multi-layer directories for you!
Example: Create a directory with permissionsrwx--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
In the permissions section above, if -m is not added to force configuration properties, the system will use the default properties.
If we use -m, as in the above example we give -m 711 to give the new directory drwx--x--x permissions.
Syntax:
rmdir [-p] 目录名称
Options and parameters:
Delete the runoob directory
[root@www tmp]# rmdir runoob/
Delete the directory created in the mkdir instance (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
Use the -p option to delete test1/test2/test3/test4 immediately.
However, it should be noted that this rmdir can only delete empty directories. You can use the rm command to delete non-empty 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:is equivalent to -pdr, as for pdr please Refer to the following instructions; (commonly used)
-d:If the source file is the attribute of the link file (link file), copy the link file attribute instead of the file itself ;
#-f:means force. If the target file already exists and cannot be opened, remove it and try again;
-i:If the target file (destination) already exists, the action will be asked first when overwriting (commonly used)
-l:Create a link file of a hard link instead of copying the file itself;
-p:Copy the file together with its attributes instead of using the default attributes (commonly used for backup);
-r:Recursive continuous copying, used for directory copying behavior; (Commonly used)
-s:复制成为符号连结档 (symbolic link),亦即『捷径』文件;
-u:若 destination 比 source 旧才升级 destination !
用 root 身份,将 root 目录下的 .bashrc 复制到 /tmp 下,并命名为 bashrc
[root@www ~]# cp ~/.bashrc /tmp/bashrc [root@www ~]# cp -i ~/.bashrc /tmp/bashrc cp: overwrite `/tmp/bashrc'? n <==n不覆盖,y为覆盖
语法:
rm [-fir] 文件或目录
选项与参数:
将刚刚在 cp 的实例中创建的 bashrc 删除掉!
[root@www tmp]# rm -i bashrc rm: remove regular file `bashrc'? y
如果加上 -i 的选项就会主动询问喔,避免你删除到错误的档名!
语法:
[root@www ~]# mv [-fiu] source destination [root@www ~]# mv [options] source1 source2 source3 .... directory
选项与参数:
复制一文件,创建一目录,将文件移动到目录中
[root@www ~]# cd /tmp [root@www tmp]# cp ~/.bashrc bashrc [root@www tmp]# mkdir mvtest [root@www tmp]# mv bashrc mvtest
将某个文件移动到某个目录去,就是这样做!
将刚刚的目录名称更名为 mvtest2
[root@www tmp]# mv mvtest mvtest2
Linux系统中使用以下命令来查看文件的内容:
你可以使用man [命令]来查看各个命令的使用文档,如 :man cp。
由第一行开始显示文件内容
语法:
cat [-AbEnTv]
选项与参数:
检看 /etc/issue 这个文件的内容:
[root@www ~]# cat /etc/issue CentOS release 6.4 (Final) Kernel \r on an \m
tac与cat命令刚好相反,文件内容从最后一行开始显示,可以看出 tac 是 cat 的倒着写!如:
[root@www ~]# tac /etc/issue Kernel \r on an \m CentOS release 6.4 (Final)
显示行号
语法:
nl [-bnw] 文件
选项与参数:
实例一:用 nl 列出 /etc/issue 的内容
[root@www ~]# nl /etc/issue 1 CentOS release 6.4 (Final) 2 Kernel \r on an \m
一页一页翻动
[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%) <== 重点在这一行喔!你的光标也会在这里等待你的命令
在 more 这个程序的运行过程中,你有几个按键可以按的:
一页一页翻动,以下实例输出/etc/man.config文件的内容:
[root@www ~]# less /etc/man.config # # Generated automatically from man.conf.in by the # configure script. # # man.conf from man-1.6d ....(中间省略).... : <== 这里可以等待你输入命令!
less运行时可以输入的命令有:
取出文件前面几行
语法:
head [-n number] 文件
选项与参数:
[root@www ~]# head /etc/man.config
默认的情况中,显示前面 10 行!若要显示前 20 行,就得要这样:
[root@www ~]# head -n 20 /etc/man.config
取出文件后面几行
语法:
tail [-n number] 文件
选项与参数:
[root@www ~]# tail /etc/man.config # 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样: [root@www ~]# tail -n 20 /etc/man.config
相关推荐:《Linux视频教程》
The above is the detailed content of What is the linux file directory command?. For more information, please follow other related articles on the PHP Chinese website!