GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — Linux 文件与目录管理 | 菜鸟教程
Linux 教程
Linux 教程
Linux 简介
Linux 安装
Linux 云服务器
WSL 安装 Linux
Linux 系统启动过程
Linux 系统目录结构
Linux 忘记密码解决方法
Linux 远程登录
Linux 文件基本属性
Linux 文件与目录管理
Linux 用户和用户组管理
Linux 磁盘管理
Linux vi/vim
linux yum 命令
Linux apt 命令
Shell 教程Shell 教程 Shell 变量 Shell 传递参数 Shell 数组 Shell 运算符 Shell echo 命令 Shell printf 命令 Shell test 命令 Shell 流程控制 Shell 函数 Shell 输入/输出重定向 Shell 文件包含Linux 参考手册Linux 命令大全 Nginx 安装配置 MySQL 安装配置 Linux 测验Linux 文件与目录管理我们知道 Linux 的目录结构为树状结构,最顶级的目录为根目录 /。 其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们。 在开始本教程前我们需要先知道什么是绝对路径与相对路径。
处理目录的常用命令接下来我们就来看几个常见的处理目录的命令吧:
你可以使用 man [命令] 来查看各个命令的使用文档,如 :man cp。 ls (列出目录)在Linux系统当中, ls 命令可能是最常被运行的。 语法: [root@www ~]# ls [-aAdfFhilnrRSt] 目录名称
[root@www ~]# ls [--color={never,auto,always}] 目录名称
[root@www ~]# ls [--full-time] 目录名称
选项与参数:
将目录下的所有文件列出来(含属性与隐藏档) [root@www ~]# ls -al ~ cd (切换目录)cd是Change Directory的缩写,这是用来变换工作目录的命令。 语法: 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 .. 接下来大家多操作几次应该就可以很好的理解 cd 命令的。 pwd (显示目前所在的目录)pwd 是 Print Working Directory 的缩写,也就是显示目前所在目录的命令。 [root@www ~]# pwd [-P] 选项与参数:
实例:单纯显示出目前的工作目录: [root@www ~]# pwd /root <== 显示出目录啦~ 实例显示出实际的工作目录,而非链接档本身的目录名而已。 [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 (创建新目录)如果想要创建新的目录的话,那么就使用mkdir (make directory)吧。 语法: mkdir [-mp] 目录名称 选项与参数:
实例:请到/tmp底下尝试创建数个新目录看看: [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 加了这个 -p 的选项,可以自行帮你创建多层目录! 实例:创建权限为 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 上面的权限部分,如果没有加上 -m 来强制配置属性,系统会使用默认属性。 如果我们使用 -m ,如上例我们给予 -m 711 来给予新的目录 drwx--x--x 的权限。 rmdir (删除空的目录)语法: rmdir [-p] 目录名称 选项与参数:
删除 runoob 目录 [root@www tmp]# rmdir runoob/ 将 mkdir 实例中创建的目录(/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 利用 -p 这个选项,立刻就可以将 test1/test2/test3/test4 一次删除。 不过要注意的是,这个 rmdir 仅能删除空的目录,你可以使用 rm 命令来删除非空目录。 cp (复制文件或目录)cp 即拷贝文件和目录。 语法: [root@www ~]# cp [-adfilprsu] 来源档(source) 目标档(destination) [root@www ~]# cp [options] source1 source2 source3 .... directory 选项与参数:
用 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 (移除文件或目录)语法: rm [-fir] 文件或目录 选项与参数:
将刚刚在 cp 的实例中创建的 bashrc 删除掉! [root@www tmp]# rm -i bashrc rm: remove regular file `bashrc'? y 如果加上 -i 的选项就会主动询问喔,避免你删除到错误的档名! mv (移动文件与目录,或修改名称)语法: [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 文件内容查看Linux系统中使用以下命令来查看文件的内容:
你可以使用 man [命令]来查看各个命令的使用文档,如 :man cp。 cat由第一行开始显示文件内容 语法: cat [-AbEnTv] 选项与参数:
检看 /etc/issue 这个文件的内容: [root@www ~]# cat /etc/issue CentOS release 6.4 (Final) Kernel \r on an \m tactac与cat命令刚好相反,文件内容从最后一行开始显示,可以看出 tac 是 cat 的倒着写!如: [root@www ~]# tac /etc/issue Kernel \r on an \m CentOS release 6.4 (Final) nl显示行号 语法: nl [-bnw] 文件 选项与参数:
实例一:用 nl 列出 /etc/issue 的内容 [root@www ~]# nl /etc/issue
1 CentOS release 6.4 (Final)
2 Kernel \r on an \m
more一页一页翻动 [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 这个程序的运行过程中,你有几个按键可以按的:
less一页一页翻动,以下实例输出/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取出文件前面几行 语法: head [-n number] 文件 选项与参数:
[root@www ~]# head /etc/man.config 默认的情况中,显示前面 10 行!若要显示前 20 行,就得要这样: [root@www ~]# head -n 20 /etc/man.config tail取出文件后面几行 语法: tail [-n number] 文件 选项与参数:
[root@www ~]# tail /etc/man.config # 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样: [root@www ~]# tail -n 20 /etc/man.config
AI 思考中...
|
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |