文件管理相关命令

先介绍下linux文件系统上的文件类型如下:

 -:表示普通文件
 d:表示目录文件
 b:表示块设备文件
 c:表示字符设备文件
 l:表示软链接文件
 p:表示管道文件
 s:表示套接字文件

例1
查看文件类型

[root@centos7 ~]# ll 
total 40
-rw-------.  1 root root 1897 Sep 20  2019 anaconda-ks.cfg
-rw-r--r--   1 root root  181 Oct 15  2019 base.repo
drwxr-xr-x.  2 root root    6 Sep 20  2019 Desktop
drwxr-xr-x.  2 root root    6 Sep 20  2019 Documents
drwxr-xr-x.  2 root root    6 Sep 20  2019 Downloads
drwx------. 38 root root 4096 Oct 10  2019 etc
-rw-r--r--   1 root root    6 Nov  7  2019 filel
-rw-r--r--   1 root root   33 Nov  7  2019 filel.ciper

显示结果中,第一个位置的符号“-”就代表了文件类型为普通文件。

例2
显示当前工作目录

[root@centos7 ~]# pwd
/root

例3
获取/etc/sysconfig/的基名

[root@centos7 ~]#basename /etc/sysconfig/
sysconfig

例4
获取/etc/sysconfig/的目录名

[root@centos7 ~]#dirname /etc/sysconfig/
/etc

例5
切换到用户家目录

[root@centos7 /etc/sysconfig]#cd
[root@centos7 ~]#
或
[root@centos7 /etc/sysconfig]#cd ~
[root@centos7 ~]#

例6
切换到父目录

[root@centos7 /etc/sysconfig]#cd ~
[root@centos7 ~]#

例7
切换到/etc/sysconfig目录下

[root@centos7 ~]#cd /etc/sysconfig/
[root@centos7 /etc/sysconfig]#

例8
切换到上次所在目录

[root@centos7 /etc/sysconfig]#cd -
/root

例9
显示当前目录下所有文件

[root@centos7 ~]#ls -a
.                Documents                  .pki
..               Downloads                  private2.key
anaconda-ks.cfg  .esd_auth                  private2.keyu
base.repo        etc                        private.key
.bash_history    filel                      Public
.bash_logout     filel.ciper                public.key
.bash_profile    .ICEauthority              .rnd
.bashrc          initial-setup-ks.cfg       .tcshrc
.cache           .initial-setup-ks.cfg.swp  Templates
.config          .lesshst                   Videos
.cshrc           .local                     .viminfo
.dbus            Music                      .vimrc
Desktop          Pictures                   .Xauthority

例10
显示目录内容的额外信息

[root@centos7 ~]#ls -l
total 40
-rw-------.  1 root root 1897 Sep 20  2019 anaconda-ks.cfg
-rw-r--r--   1 root root  181 Oct 15  2019 base.repo
drwxr-xr-x.  2 root root    6 Sep 20  2019 Desktop
drwxr-xr-x.  2 root root    6 Sep 20  2019 Documents
或
[root@centos7 ~]#ll
total 40
-rw-------.  1 root root 1897 Sep 20  2019 anaconda-ks.cfg
-rw-r--r--   1 root root  181 Oct 15  2019 base.repo
drwxr-xr-x.  2 root root    6 Sep 20  2019 Desktop
drwxr-xr-x.  2 root root    6 Sep 20  2019 Documents

例11
递归显示目录内容

[root@centos7 ~]#ls -r
Videos       private2.keyu         filel.ciper  Desktop
Templates    private2.key          filel        base.repo
public.key   Pictures              etc          anaconda-ks.cfg
Public       Music                 Downloads
private.key  initial-setup-ks.cfg  Documents

例12
组合应用

[root@centos7 ~]#ls -ar
.Xauthority    Pictures                   Desktop
.vimrc         Music                      .dbus
.viminfo       .local                     .cshrc
Videos         .lesshst                   .config
Templates      .initial-setup-ks.cfg.swp  .cache

例13
查看filel文件的状态,注意三个时间戳

[root@centos7 ~]#stat filel
    File: ‘filel’
    Size: 6             Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 201551289   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-09-12 18:36:47.799983521 +0800
Modify: 2019-11-07 10:22:48.764939245 +0800
Change: 2019-11-07 10:22:48.764939245 +0800
 Birth: -

例14
创建空文件tist.sh

[root@centos7 /data]#touch tist.sh
[root@centos7 /data]#ll tist.sh 
-rw-r--r-- 1 root root 0 Oct 20 14:48 tist.sh

例15
把/etc/virc文件拷贝到当前目录

[root@centos7 /data]#cp /etc/virc ./
[root@centos7 /data]#ll
total 16
-rw-r--r--  1 root  root  1026 Sep 16 10:50 anaconda-ks.cfg
-rw-r--r--  1 root  root  1761 Sep 10 08:29 awktest.txt
drwxr-xr-x  2 root  root     6 Sep 14 11:08 cdrom
drwxr-xr-x  2 root  root     6 Oct 31  2019 dir
drwxr-xr-x  5 mysql mysql  110 May 17 15:32 mysql
drwxr-xr-x. 2 root  root  4096 Oct 11 17:09 script39
-rw-r--r--  1 root  root     0 Oct 20 14:48 tist.sh
-rw-r--r--  1 root  root  1982 Oct 20 14:52 virc

例16
把/data/script39目录及其下面所有文件和子目录拷贝到当前目录

[root@centos7 ~]#cp -R /data/script39/ ./
[root@centos7 ~]#ls script39/
argsnum.sh     disk.sh     mkcdrom.sh  sumfile.sh     yesorno.sh
backup.sh      hello.sh    pass.sh     sumid.sh
chkscore.sh    initial.sh  REG         sysinfo.sh
createuser.sh  links.sh    rename.sh   systeminfo.sh

猜你喜欢

转载自blog.51cto.com/14233656/2542617