Linux 文件与目录管理
Linux?文件与目录管理我们知道Linux的目录结构为树状结构,最顶级的目录为根目录 / 其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们。 我们需要先知道什么是绝对路径与相对路径。 绝对路径: 相对路径: 处理目录的常用命令接下来我们就来看几个常见的处理目录的命令吧:
你可以使用?man [命令]?来查看各个命令的使用文档,如 :man ls。 ls(列出目录)在Linux系统当中, ls 命令可能是最常被运行的。 语法: [[email?protected] ~]# ls [-aAdfFhilnrRSt] 目录名称 [[email?protected] ~]# ls [--color={never,auto,always}] 目录名称 [[email?protected] ~]# ls [--full-time] 目录名称 选项与参数:
将~目录(即root目录)下的所有文件列出来(含属性与隐藏文档) [[email?protected] ~]# ls -al ~
cd (切换目录)cd是Change Directory的缩写,这是用来变换工作目录的命令。 语法: cd [相对路径或绝对路径] 测试 #使用 mkdir 命令创建 testdir 目录 [[email?protected] ~]# mkdir testdir #使用绝对路径切换到 testdir 目录 [[email?protected] ~]# cd /root/testdir/ #使用相对路径切换到 testdir 目录 [[email?protected] ~]# cd ./testdir/ # 表示回到自己的家目录,亦即是 /root 这个目录 [[email?protected] testdir]# cd ~ # 表示去到目前的上一级目录,亦即是 /root 的上一级目录的意思; [[email?protected] ~]# cd .. pwd (显示目前所在的目录)pwd 是?Print Working Directory?的缩写,也就是显示目前所在目录的命令。 [[email?protected] ~]# pwd [-P]
选项与参数:
例: [[email?protected] ~]# pwd
/root
连结路径实例: [[email?protected] /]# cd var [[email?protected] var]# cd mail [[email?protected] mail]# pwd /var/mail [[email?protected] mail]# pwd -P /var/spool/mail 为什么使用 pwd 与 pwd -P得到的路径不同呢 我们可以使用ls -l 查看一下 [[email?protected] mail]# cd .. [[email?protected] var]# ls -l 总用量 12 drwxr-xr-x. 2 root root 19 6月 6 21:36 account drwxr-xr-x. 2 root root 6 4月 11 2018 adm drwxr-xr-x. 13 root root 159 6月 6 21:54 cache drwxr-xr-x. 2 root root 6 11月 5 2018 crash drwxr-xr-x. 3 root root 34 6月 6 15:19 db drwxr-xr-x. 3 root root 18 6月 6 21:36 empty drwxr-xr-x. 2 root root 6 4月 11 2018 games drwxr-xr-x. 2 root root 6 4月 11 2018 gopher drwxr-xr-x. 3 root root 18 6月 6 21:31 kerberos drwxr-xr-x. 61 root root 4096 6月 6 21:54 lib drwxr-xr-x. 2 root root 6 4月 11 2018 local lrwxrwxrwx. 1 root root 11 6月 6 21:30 lock -> ../run/lock drwxr-xr-x. 20 root root 4096 6月 16 12:18 log lrwxrwxrwx. 1 root root 10 6月 6 21:30 mail -> spool/mail drwxr-xr-x. 2 root root 6 4月 11 2018 nis drwxr-xr-x. 2 root root 6 4月 11 2018 opt drwxr-xr-x. 2 root root 6 4月 11 2018 preserve lrwxrwxrwx. 1 root root 6 6月 6 21:30 run -> ../run drwxr-xr-x. 12 root root 140 6月 6 21:36 spool drwxr-xr-x. 4 root root 28 6月 6 21:32 target drwxrwxrwt. 10 root root 4096 6月 16 12:11 tmp drwxr-xr-x. 2 root root 6 4月 11 2018 yp 我们可以看到其中的? ??lrwxrwxrwx. 1 root root 10 6月 6 21:30 mail -> spool/mail 因此可以知道/var/mail 是连结档,连结到 /var/spool/mail ? 加上 pwd -P 的选项后,会不以连结档的数据显示,而是显示正确的完整路径 mkdir (创建新目录)如果想要创建新的目录的话,那么就使用mkdir (make directory)吧。 语法: mkdir [-mp] 目录名称
选项与参数:
实例:请到/tmp底下尝试创建数个新目录看看: [[email?protected] var]# cd /tmp [[email?protected] tmp]# mkdir test [[email?protected] tmp]# mkdir test1/test2/test3/test4 mkdir: 无法创建目录"test1/test2/test3/test4": 没有那个文件或目录 [[email?protected] tmp]# mkdir -p test1/test2/test3/test4 //创建成功 加上-p参数就可以直接创建多层目录 实例:使用-m参数创建权限为?rwx--x--x?的目录。 ? [[email?protected] tmp]# mkdir -m 711 test2 ? [[email?protected] tmp]# ls -l ... drwxr-xr-x. 2 root root 6 6月 16 15:20 test drwxr-xr-x. 3 root root 19 6月 16 15:20 test1 drwx--x--x. 2 root root 6 6月 16 15:25 test2 如果没有加上 -m 来强制配置属性,系统会使用默认属性。 rmdir (删除空的目录)语法: rmdir [-p] 目录名称
选项与参数:
例如删除 ~中的testdir目录 [[email?protected] /]# rmdir /root/testdir //成功 [[email?protected] /]# rmdir ~/testdir 我们还可以使用-p参数将 刚刚在mkdir 实例中创建的目录(/tmp 底下)删除掉 [[email?protected] tmp]# rmdir test [[email?protected] tmp]# rmdir test2 [[email?protected] tmp]# rmdir test1 rmdir: 删除 "test1" 失败: 目录非空 [[email?protected] tmp]# rmdir -p test1/test2/test3/test4 //这时才可以完全删除test1 注意的是,这个 rmdir 仅能删除空的目录,你可以使用 rm 命令来删除非空目录。 cp (复制文件或目录)cp 即拷贝文件和目录。 语法: [[email?protected] ~]# cp [-adfilprsu] 来源档(source) 目标档(destination) [[email?protected] ~]# cp [options] source1 source2 source3 .... directory 选项与参数:
实例:用 root 身份,将 root 目录下的 .bashrc 复制到 /tmp 下,并命名为 bashrc [[email?protected] ~]# cp ~/.bashrc /tmp/bashrc [[email?protected] ~]# cp -i ~/.bashrc /tmp/bashrc cp:是否覆盖"/tmp/bashrc"? n 上面代码中我们第二次复制添加 i 参数? 此时如果目标地址中已有同名文件,则会询问是否覆盖 可是我使用的centos7中发现即使不加 i 也会询问。。 [[email?protected] tmp]# cp ~/.bashrc /tmp/bashrc cp:是否覆盖"/tmp/bashrc"? n rm (移除文件或目录)语法: rm [-fir] 文件或目录
选项与参数:
将刚刚在 cp 的实例中创建的 bashrc 删除掉: [[email?protected] tmp]# rm -i bashrc rm:是否删除普通文件 "bashrc"?y 加上 -i 的选项就会主动询问,避免你删除到错误的档名 mv (移动文件与目录,或修改名称)语法: [[email?protected] ~]# mv [-fiu] source destination [[email?protected] ~]# mv [options] source1 source2 source3 .... directory 选项与参数:
复制一文件,创建一目录,将文件移动到目录中 [[email?protected] tmp]# cp ~/.bashrc /tmp/bashrc [[email?protected] tmp]# mkdir mvtest [[email?protected] tmp]# mv bashrc mvtest [[email?protected] tmp]# cd mvtest [[email?protected] mvtest]# ls //验证是否成功 bashrc 将刚刚的目录名称更名为 mvtest2 [[email?protected] tmp]# mv mvtest mvtest2
?Linux 文件内容查看Linux系统中使用以下命令来查看文件的内容:
我们可以使用?man [命令]来查看各个命令的使用文档,如 :man cat cat由第一行开始显示文件内容 语法: cat [-AbEnTv]
选项与参数:
检看 /etc/issue 这个文件的内容: [[email?protected] ~]# cat /etc/issue S Kernel r on an m tactac与cat命令刚好相反,文件内容从最后一行开始显示,可以看出 tac 是 cat 的倒着写! [[email?protected] ~]# tac /etc/issue Kernel r on an m S nl显示行号 语法: nl [-bnw] 文件
选项与参数: -b :指定行号指定的方式,主要有两种: -n :列出行号表示的方法,主要有三种: -w :行号栏位的占用的位数。 如用 nl 列出 /etc/issue 的内容: [[email?protected] ~]# nl /etc/issue 1 S 2 Kernel r on an m more一页一页翻动? 例如 /root/anaconda-ks.cfg这个文件 [[email?protected] /]# more /root/anaconda-ks.cfg #version=DEVEL # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom # Use graphical install graphical # Run the Setup Agent on first boot firstboot --enable ignoredisk --only-use=sda # Keyboard layouts keyboard --vckeymap=cn --xlayouts=‘cn‘ # System language lang zh_CN.UTF-8 # Network information network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto -- --More--(24%) <== 重点在这一行!光标会在这里等待你的命令 在 more 这个程序的运行过程中,你有几个按键可以按的:
less与more类似 命令不同 如下 less运行时可以输入的命令有:
? head取出文件前面几行 语法: head [-n number] 文件
选项与参数:
例如命令: [[email?protected] ~]# head /root/anaconda-ks.cfg #version=DEVEL # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom # Use graphical install graphical # Run the Setup Agent on first boot firstboot --enable ignoredisk --only-use=sda 默认的情况中,显示前面 10 行!若要显示前 20 行,就得要这样: [[email?protected] ~]# head -n 20 /root/anaconda-ks.cfg tail取出文件后面几行 语法: tail [-n number] 文件
选项与参数:
例如: [[email?protected] ~]# tail /root/anaconda-ks.cfg
同样默认也是取出10行,若要显示最后的 20 行,就得要这样: [[email?protected] ~]# tail -n 20 /root/anaconda-ks.cfg (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |