> 文档中心 > Linux的文件操作命令详解

Linux的文件操作命令详解

在开始之前先将Linux系统的基础文件夹关系图列出来

前提知识

1、./是当前目录
2、../是父级目录
3、/是根目录

1、cd(改变当前工作目录)

[root@CentOS-7-64 ~]# cd /etc[root@CentOS-7-64 etc]#
[root@CentOS-7-64 etc]# cd ../opt[root@CentOS-7-64 opt]#

2、ll(显示出指定目录下的所有文件和文件夹和快捷方式)

[root@CentOS-7-64 etc]# lltotal 1028-rw-r--r--.  1 root root     16 May 11 13:09 adjtime-rw-r--r--.  1 root root   1518 Jun  7  2013 aliases-rw-r--r--.  1 root root  12288 May 11 13:15 aliases.dbdrwxr-xr-x.  2 root root    236 May 11 13:06 alternatives-rw-------.  1 root root    541 Aug  9  2019 anacrontab-rw-r--r--.  1 root root     55 Aug  8  2019 asound.confdrwxr-x---.  3 root root     43 May 11 13:06 audispdrwxr-x---.  3 root root     83 May 11 13:15 auditdrwxr-xr-x.  2 root root     22 May 11 13:06 bash_completion.d-rw-r--r--.  1 root root   2853 Oct 31  2018 bashrcdrwxr-xr-x.  2 root root      6 Aug  8  2019 binfmt.d-rw-r--r--.  1 root root     37 Sep  5  2019 centos-release-rw-r--r--.  1 root root     51 Sep  5  2019 centos-release-upstreamdrwxr-xr-x.  2 root root      6 Aug  4  2017 chkconfig.d-rw-r--r--   1 root root      0 May 11 20:53 configdrwxr-xr-x.  2 root root     21 May 11 13:06 cron.ddrwxr-xr-x.  2 root root     42 May 11 13:06 cron.daily-rw-------.  1 root root      0 Aug  9  2019 cron.denydrwxr-xr-x.  2 root root     22 Jun 10  2014 cron.hourlydrwxr-xr-x.  2 root root      6 Jun 10  2014 cron.monthly-rw-r--r--.  1 root root    451 Jun 10  2014 crontabdrwxr-xr-x.  2 root root      6 Jun 10  2014 cron.weekly-rw-------.  1 root root      0 May 11 13:05 crypttab-rw-r--r--.  1 root root   1620 Oct 31  2018 csh.cshrc-rw-r--r--.  1 root root    866 Oct 31  2018 csh.logindrwxr-xr-x.  4 root root     78 May 11 13:06 dbus-1drwxr-xr-x.  2 root root     44 May 11 13:09 defaultdrwxr-xr-x.  2 root root     23 May 11 13:06 depmod.ddrwxr-x---.  4 root root     53 May 11 13:06 dhcp-rw-r--r--.  1 root root   5090 Aug  6  2019 DIR_COLORS-rw-r--r--.  1 root root   5725 Aug  6  2019 DIR_COLORS.256color-rw-r--r--.  1 root root   4669 Aug  6  2019 DIR_COLORS.lightbgcolor-rw-r--r--.  1 root root   1285 Aug  9  2019 dracut.confdrwxr-xr-x.  2 root root      6 Aug  9  2019 dracut.conf.d-rw-r--r--.  1 root root    112 Aug  6  2019 e2fsck.conf-rw-r--r--.  1 root root      0 Oct 31  2018 environment-rw-r--r--.  1 root root   1317 Apr 11  2018 ethertypes-rw-r--r--.  1 root root      0 Jun  7  2013 exportslrwxrwxrwx.  1 root root     56 May 11 13:06 favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png-rw-r--r--.  1 root root     70 Oct 31  2018 filesystemsdrwxr-x---.  7 root root    133 May 11 13:06 firewalld-rw-r--r--.  1 root root    465 May 11 13:05 fstabdrwxr-xr-x.  2 root root      6 Aug  2  2017 gcryptdrwxr-xr-x.  2 root root      6 Jul 13  2018 gnupg-rw-r--r--.  1 root root     94 Mar 25  2017 GREP_COLORSdrwxr-xr-x.  4 root root     40 May 11 13:06 groff-rw-r--r--.  1 root root    435 May 11 13:06 group-rw-r--r--.  1 root root    428 May 11 13:06 group-lrwxrwxrwx.  1 root root     22 May 11 13:06 grub2.cfg -> ../boot/grub2/grub.cfgdrwx------.  2 root root    182 May 11 13:06 grub.d----------.  1 root root    345 May 11 13:06 gshadow----------.  1 root root    338 May 11 13:06 gshadow-drwxr-xr-x.  3 root root     20 May 11 13:06 gss

注:

  • "-rw-------":     -开头代表是一个文件, rw代表对该文件的操作权限
  • "drwxr-xr-x":  d开头指的是一个文件夹
  • "lrwxrwxrwx": l开头代表的是一个链接, 在windows系统中代表快捷方式

3、ls(显示出指定目录下的所有文件)跟ll是一样的作用, 只不过显示效果不太一样.

[root@CentOS-7-64 etc]# lsadjtime    dbus-1     gshadow localtime   passwd-  resolv.confsudoersaliases    default    gshadow-login.defs  pkcs11   resolv.conf.save  sudoers.daliases.db depmod.d   gss     logrotate.conf     pki      rpc sudo-ldap.confalternatives      dhcphost.conf      logrotate.d plymouth rpm sysconfiganacrontab DIR_COLORS hostnamelvm  pmrsyslog.conf      sysctl.confasound.confDIR_COLORS.256color      hosts   machine-id  polkit-1 rsyslog.d  sysctl.daudisp     DIR_COLORS.lightbgcolor  hosts.allow    magicpopt.d   rwtab      systemdaudit      dracut.confhosts.deny     makedumpfile.conf.sample  postfix  rwtab.d    system-releasebash_completion.d dracut.conf.d     init.d  man_db.conf ppp      sasl2      system-release-cpebashrc     e2fsck.confinittab mke2fs.conf prelink.conf.d  securetty  terminfobinfmt.d   environmentinputrc modprobe.d  printcap security   tmpfiles.dcentos-release    ethertypes iproute2modules-load.d     profile  selinux    tunedcentos-release-upstream  exports    issue   motd profile.dservices   udevchkconfig.dfavicon.pngissue.net      mtab protocolssestatus.conf     vconsole.confconfig     filesystemskdump.conf     my.cnf      python   shadow     virccron.d     firewalld  kernel  my.cnf.d    rc0.d    shadow-    wpa_supplicantcron.daily fstab      krb5.conf      NetworkManager     rc1.d    shells     X11cron.deny  gcrypt     krb5.conf.d    networks    rc2.d    skelxdgcron.hourlygnupg      ld.so.cache    nsswitch.conf      rc3.d    ssh xinetd.dcron.monthly      GREP_COLORSld.so.conf     nsswitch.conf.bak  rc4.d    ssl yumcrontab    groff      ld.so.conf.d   openldap    rc5.d    statetab   yum.confcron.weeklygroup      libaudit.conf  opt  rc6.d    statetab.d yum.repos.dcrypttab   group-     libnl   os-release  rc.d     subgidcsh.cshrc  grub2.cfg  libuser.conf   pam.drc.local subuidcsh.login  grub.d     locale.conf    passwd      redhat-release  sudo.conf

4、mkdir(创建文件目录), rmdir(删除空文件夹)

[root@CentOS-7-64 ~]# mkdir aaa[root@CentOS-7-64 ~]# lltotal 4drwxr-xr-x  2 root root    6 May 13 18:51 aaa-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg

一下子创建多个文件夹加 "-p" 

[root@CentOS-7-64 ~]# mkdir -p a/b/c[root@CentOS-7-64 ~]# lltotal 4drwxr-xr-x  3 root root   15 May 13 18:54 adrwxr-xr-x  2 root root    6 May 13 18:51 aaa-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg[root@CentOS-7-64 ~]# cd a[root@CentOS-7-64 a]# cd b[root@CentOS-7-64 b]# cd c

在同一个目录下创建多个文件夹

[root@CentOS-7-64 c]# mkdir -p shiren{libai,baijuyi,dufu}[root@CentOS-7-64 c]# lltotal 0drwxr-xr-x 2 root root 6 May 13 19:00 shirenbaijuyidrwxr-xr-x 2 root root 6 May 13 19:00 shirendufudrwxr-xr-x 2 root root 6 May 13 19:00 shirenlibai

在shiren目录下创建多个文件夹

[root@CentOS-7-64 ~]# lltotal 4drwxr-xr-x  3 root root   15 May 13 18:54 a-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg[root@CentOS-7-64 ~]# mkdir -p shiren/{libai,baijuyi,dufu}[root@CentOS-7-64 ~]# lltotal 4drwxr-xr-x  3 root root   15 May 13 18:54 a-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  5 root root   46 May 13 20:27 shiren[root@CentOS-7-64 ~]# cd shiren[root@CentOS-7-64 shiren]# lltotal 0drwxr-xr-x 2 root root 6 May 13 20:27 baijuyidrwxr-xr-x 2 root root 6 May 13 20:27 dufudrwxr-xr-x 2 root root 6 May 13 20:27 libai

5、rmdir(删除空文件夹)

[root@CentOS-7-64 ~]# lltotal 4-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg[root@CentOS-7-64 ~]# mkdir aaa[root@CentOS-7-64 ~]# lltotal 4drwxr-xr-x  2 root root    6 May 13 20:44 aaa-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg[root@CentOS-7-64 ~]# rmdir aaa[root@CentOS-7-64 ~]# lltotal 4-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
[root@CentOS-7-64 ~]# lltotal 4drwxr-xr-x  2 root root    6 May 13 20:45 aaa-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg[root@CentOS-7-64 ~]# cd aaa[root@CentOS-7-64 aaa]# mkdir bbb[root@CentOS-7-64 aaa]# cd[root@CentOS-7-64 ~]# rmdir aaarmdir: failed to remove ‘aaa’: Directory not empty[root@CentOS-7-64 ~]# rm -rf aaa[root@CentOS-7-64 ~]# lltotal 4-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg

6、cp(拷贝文件或者文件目录) 

将aaa文件夹中的test文件夹拷贝到bbb文件夹中

[root@CentOS-7-64 ~]# lltotal 4drwxr-xr-x  3 root root   18 May 13 20:51 aaa-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  2 root root    6 May 13 20:56 bbb[root@CentOS-7-64 ~]# cd aaa[root@CentOS-7-64 aaa]# lltotal 0drwxr-xr-x 2 root root 6 May 13 20:51 test[root@CentOS-7-64 aaa]# cd[root@CentOS-7-64 ~]# cd bbb[root@CentOS-7-64 bbb]# lltotal 0[root@CentOS-7-64 bbb]# cp -r aaa/test bbbcp: cannot stat ‘aaa/test’: No such file or directory[root@CentOS-7-64 bbb]# cd[root@CentOS-7-64 ~]# cp -r aaa/test bbb[root@CentOS-7-64 ~]# cd bbb[root@CentOS-7-64 bbb]# lltotal 0drwxr-xr-x 2 root root 6 May 13 20:57 test[root@CentOS-7-64 bbb]#

将文件拷贝到aaa文件夹下

[root@CentOS-7-64 ~]# lltotal 4drwxr-xr-x  3 root root   18 May 13 20:51 aaa-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  3 root root   18 May 13 20:57 bbb[root@CentOS-7-64 ~]# cp anaconda-ks.cfg aaa[root@CentOS-7-64 ~]# cd aaa[root@CentOS-7-64 aaa]# lltotal 4-rw------- 1 root root 1255 May 13 21:05 anaconda-ks.cfgdrwxr-xr-x 2 root root    6 May 13 20:51 test

7、mv(移动文件或者文件夹)

[root@CentOS-7-64 ~]# lltotal 4drwxr-xr-x  3 root root   41 May 13 21:05 aaa-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  3 root root   18 May 13 20:57 bbb[root@CentOS-7-64 ~]# mv aaa bbb[root@CentOS-7-64 ~]# lltotal 4-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:09 bbb[root@CentOS-7-64 ~]# cd bbb[root@CentOS-7-64 bbb]# lltotal 0drwxr-xr-x 3 root root 41 May 13 21:05 aaadrwxr-xr-x 2 root root  6 May 13 20:57 test

mv还有修改文件或者文件夹名字的作用

[root@CentOS-7-64 ~]# lltotal 4-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:09 bbb[root@CentOS-7-64 ~]# mv bbb bbb1[root@CentOS-7-64 ~]# lltotal 4-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:09 bbb1

8、rm(删除文件或者文件夹)

rm删除文件的时候需要输入(y, n), 如果输入y则是删除, 否则则是不删除, 如果只用rm只能删除文件.

[root@CentOS-7-64 ~]# lltotal 4-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:09 bbb1[root@CentOS-7-64 ~]# cd bbb1[root@CentOS-7-64 bbb1]# lltotal 0drwxr-xr-x 3 root root 41 May 13 21:05 aaadrwxr-xr-x 2 root root  6 May 13 20:57 test[root@CentOS-7-64 bbb1]# cd aaa[root@CentOS-7-64 aaa]# lltotal 4-rw------- 1 root root 1255 May 13 21:05 anaconda-ks.cfgdrwxr-xr-x 2 root root    6 May 13 20:51 test[root@CentOS-7-64 aaa]# rm anaconda-ks.cfgrm: remove regular file ‘anaconda-ks.cfg’? y[root@CentOS-7-64 aaa]# lltotal 0drwxr-xr-x 2 root root 6 May 13 20:51 test

rm -f: 强制删除文件, 不需要手动输入y,n

[root@CentOS-7-64 bbb1]# lltotal 4drwxr-xr-x 3 root root 18 May 13 21:21 aaa-rw-r--r-- 1 root root 16 May 13 21:23 adjtimedrwxr-xr-x 2 root root  6 May 13 20:57 test[root@CentOS-7-64 bbb1]# rm -f adjtime[root@CentOS-7-64 bbb1]# lltotal 0drwxr-xr-x 3 root root 18 May 13 21:21 aaadrwxr-xr-x 2 root root  6 May 13 20:57 test

rm -r(删除1文件夹, 只不过也要输入y,n)

rm -rf(前置删除文件夹, 不需要手动输入y,n)

9、touch, stat

touch(如果没有就创建一个文件)

[root@CentOS-7-64 ~]# lltotal 4-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:24 bbb1[root@CentOS-7-64 ~]# touch test.txt[root@CentOS-7-64 ~]# lltotal 4-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:24 bbb1-rw-r--r--  1 root root    0 May 13 21:36 test.txt

stat(查看文件/文件夹状态)

[root@CentOS-7-64 ~]# stat test.txt  File: ‘test.txt’  Size: 0 Blocks: 0   IO Block: 4096   regular empty fileDevice: fd00h/64768d    Inode: 67172211    Links: 1Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2022-05-13 21:36:33.572958157 +0800Modify: 2022-05-13 21:36:33.572958157 +0800Change: 2022-05-13 21:36:33.572958157 +0800

解析:

  • Inode 当前文件在文件系统的唯一标识,类似于ID

时间

  • access 访问时间
  • modify 修改文件内容时间
  • change 修改文件元数据信息时间 (文件大小 ,文件所有者 ,文件权限, 对于文件的描述信息)
[root@CentOS-7-64 ~]# lltotal 8-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:24 bbb1-rwxr-xr-x  1 root root    1 May 13 22:07 test.txt[root@CentOS-7-64 ~]# vi test.txt[root@CentOS-7-64 ~]# stat test.txt  File: ‘test.txt’  Size: 16Blocks: 8   IO Block: 4096   regular fileDevice: fd00h/64768d    Inode: 67172212    Links: 1Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2022-05-13 22:08:22.873880847 +0800Modify: 2022-05-13 22:08:22.873880847 +0800Change: 2022-05-13 22:08:22.873880847 +0800

修改了文件内容, 其中Accesss, Modify, Change的时间都会发生变化; 且时间都一致

[root@CentOS-7-64 ~]# lltotal 8-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:24 bbb1-rwxr-xr-x  1 root root   16 May 13 22:08 test.txt[root@CentOS-7-64 ~]# touch test.txt[root@CentOS-7-64 ~]# stat test.txt  File: ‘test.txt’  Size: 16Blocks: 8   IO Block: 4096   regular fileDevice: fd00h/64768d    Inode: 67172212    Links: 1Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2022-05-13 22:10:20.597876081 +0800Modify: 2022-05-13 22:10:20.597876081 +0800Change: 2022-05-13 22:10:20.597876081 +0800

其中touch 文件名: 如果该文件已经存在, 修改文件的三个时间, 将三个时间都改为当前时间;

10、ln

[root@CentOS-7-64 ~]# lltotal 8-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:24 bbb1-rwxr-xr-x  1 root root   16 May 13 22:10 test.txt//创建软链接(ln -s)[root@CentOS-7-64 ~]# ln -s test.txt test1.txt[root@CentOS-7-64 ~]# lltotal 8-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:24 bbb1lrwxrwxrwx  1 root root    8 May 13 22:27 test1.txt -> test.txt-rwxr-xr-x  1 root root   16 May 13 22:10 test.txt//创建硬链接(ln )[root@CentOS-7-64 ~]# ln test.txt test2.txt[root@CentOS-7-64 ~]# lltotal 12-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:24 bbb1lrwxrwxrwx  1 root root    8 May 13 22:27 test1.txt -> test.txt-rwxr-xr-x  2 root root   16 May 13 22:10 test2.txt-rwxr-xr-x  2 root root   16 May 13 22:10 test.txt[root@CentOS-7-64 ~]# vi test.txt[root@CentOS-7-64 ~]# cat test1.txt世界, 你好(hello world)![root@CentOS-7-64 ~]# vi test.txt[root@CentOS-7-64 ~]# cat test2.txt世界, 你好(hello world)!床前明月光, 疑是地上霜.[root@CentOS-7-64 ~]# cat test1.txt世界, 你好(hello world)!床前明月光, 疑是地上霜.//Links = 2 这个文件地址(Inode: 67172212)被两个所指向(关联)[root@CentOS-7-64 ~]# stat test.txt  File: ‘test.txt’  Size: 64Blocks: 8   IO Block: 4096   regular fileDevice: fd00h/64768d    Inode: 67172212    Links: 2Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2022-05-13 22:30:29.353827137 +0800Modify: 2022-05-13 22:30:21.518827454 +0800Change: 2022-05-13 22:30:21.518827454 +0800 Birth: -//Links = 1 这个文件指向(关联)的是test.txt文件[root@CentOS-7-64 ~]# stat test1.txt  File: ‘test1.txt’ -> ‘test.txt’  Size: 8 Blocks: 0   IO Block: 4096   symbolic linkDevice: fd00h/64768d    Inode: 67172211    Links: 1Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2022-05-13 22:27:57.728833276 +0800Modify: 2022-05-13 22:27:45.512833771 +0800Change: 2022-05-13 22:27:45.512833771 +0800 Birth: -//Links = 2 这个文件地址(Inode: 67172212)被两个所指向(关联)[root@CentOS-7-64 ~]# stat test2.txt  File: ‘test2.txt’  Size: 64Blocks: 8   IO Block: 4096   regular fileDevice: fd00h/64768d    Inode: 67172212    Links: 2Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2022-05-13 22:30:29.353827137 +0800Modify: 2022-05-13 22:30:21.518827454 +0800Change: 2022-05-13 22:30:21.518827454 +0800 Birth: -[root@CentOS-7-64 ~]# rm -rf test.txt//删除test.txt之后, 软地址(链接)已经没有指向的, 这个时候 test1.txt -> test.txt会报错.[root@CentOS-7-64 ~]# lltotal 8-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-xr-x  4 root root   29 May 13 21:24 bbb1lrwxrwxrwx  1 root root    8 May 13 22:27 test1.txt -> test.txt-rwxr-xr-x  1 root root   64 May 13 22:30 test2.txt[root@CentOS-7-64 ~]# stat test1.txt  File: ‘test1.txt’ -> ‘test.txt’  Size: 8 Blocks: 0   IO Block: 4096   symbolic linkDevice: fd00h/64768d    Inode: 67172211    Links: 1Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2022-05-13 22:27:57.728833276 +0800Modify: 2022-05-13 22:27:45.512833771 +0800Change: 2022-05-13 22:27:45.512833771 +0800 Birth: -//删除test.txt之后, Links:1 指向67172211硬盘地址的就只有test2.txt文件了;[root@CentOS-7-64 ~]# stat test2.txt  File: ‘test2.txt’  Size: 64Blocks: 8   IO Block: 4096   regular fileDevice: fd00h/64768d    Inode: 67172212    Links: 1Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2022-05-13 22:30:29.353827137 +0800Modify: 2022-05-13 22:30:21.518827454 +0800Change: 2022-05-13 22:32:14.842822865 +0800

分析