文件及目录操作 - touch、tree
1、touch:创建文件或更改文件时间戳
touch命令的功能说明
touch
命令用于创建新的空文件或改变已有文件的时间戳属性。
touch命令的语法格式
touch
[OPTION]... FILE...touch
[参数选项] [文件]
touch命令的选项说明
touch
选项不常用,就不细说了:
touch命令的实践操作
范例1: 创建文件(文件事先不存在的情况)
[root@oldboyedu ~]# mkdir -p /test
[root@oldboyedu ~]# cd /test
[root@oldboyedu /test]# ls
[root@oldboyedu /test]# touch oldboy.txt
[root@oldboyedu /test]# ls
oldboy.txt
[root@oldboyedu /test]# touch a.txt b.txt
[root@oldboyedu /test]# ls
a.txt b.txt oldboy.txt
[root@oldboyedu /test]# touch stu{1..4} <-->利用{ }有序序列批量创建文件
[root@oldboyedu /test]# ls
a.txt b.txt oldboy.txt stu1 stu2 stu3 stu4
范例1: 更改文件的时间戳属性
[root@oldboyedu /test]# stat oldboy.txt
File: ‘oldboy.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 51524120 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-04-06 19:00:34.447304058 +0800
Modify: 2019-04-06 19:00:34.447304058 +0800
Change: 2019-04-06 19:00:34.447304058 +0800
Birth: -
[root@oldboyedu /test]# touch -a oldboy.txt <--> -a 参数只更改文件的最后访问时间
[root@oldboyedu /test]# stat oldboy.txt
File: ‘oldboy.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 51524120 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-04-06 19:04:20.967284353 +0800
Modify: 2019-04-06 19:00:34.447304058 +0800 <-->时间变了
Change: 2019-04-06 19:04:20.967284353 +0800
Birth: -
[root@oldboyedu /test]# touch -m oldboy.txt <--> -m 参数只更改文件的最后修改时间
[root@oldboyedu /test]# stat oldboy.txt
File: ‘oldboy.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 51524120 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-04-06 19:04:20.967284353 +0800 <-->时间变了
Modify: 2019-04-06 19:05:13.179279812 +0800
Change: 2019-04-06 19:05:13.179279812 +0800
Birth: -
范例2: 指定时间属性创建/修改文件(不常用,了解即可)
-rw-r--r-- 1 root root 0 Apr 6 19:05 oldboy.txt
[root@oldboyedu /test]# ls -lh oldboy.txt
[root@oldboyedu /test]# touch -d 20201001 oldboy.txt <--> -d 参数的用法
[root@oldboyedu /test]# ls -lh oldboy.txt
-rw-r--r-- 1 root root 0 Oct 1 2020 oldboy.txt
[root@oldboyedu /test]# ls -lh a.txt
-rw-r--r-- 1 root root 0 Apr 6 19:00 a.txt
[root@oldboyedu /test]# touch -r a.txt oldboy.txt <--> -r 参数的用法
[root@oldboyedu /test]# ls -lh oldboy.txt
-rw-r--r-- 1 root root 0 Apr 6 19:00 oldboy.txt
[root@oldboyedu /test]# touch -t 201904161914.50 oldboy.txt <--> -t 参数的用法
[root@oldboyedu /test]# ls -lh oldboy.txt
-rw-r--r-- 1 root root 0 Apr 16 2019 oldboy.txt
[root@oldboyedu /test]# ls -lh --full-time oldboy.txt <-->查看结果
-rw-r--r-- 1 root root 0 2019-04-16 19:14:50.000000000 +0800 oldboy.txt
2、tree:以树形结构显示目录下内容
tree命令的功能说明
tree
命令用于以树形结构列出指定目录下的所有内容,包括所有文件、子目录等。
tree命令的语法格式
tree
[OPTION]... [directory ...]tree
[参数选项] [目录]
tree命令的选项说明
tree
选项很多,表1为tree
命令的常用参数及说明:
表1:
tree
命令的常用参数及说明
参数选项 | 解释说明(带*的为重点) |
---|---|
-a | 显示所有文件,包括隐藏文件 |
-d | 只显示目录 * |
-f | 显示每个文件的全路径 |
-i | 不显示树枝,常用参数-f配合使用 |
-L level | 遍历目录的最大层数,level为大于0的正整数 * |
-F | 在不同类型文件结尾加各种表示 |
tree命令的实践操作
环境准备
[root@oldboyedu ~]# rpm -qa tree <-->查询tree命令是否安装
tree-1.6.0-10.el7.x86_64 <-->显示已经安装,若未安装,执行以下操作
[root@oldboyedu ~]# yum install tree -y <-->使用yum,进行安装tree命令
[root@oldboyedu ~]# LANG=en_US.UTF-8 <-->临时调整系统字符集,防止树形显示乱码
范例1: 不加任何参数执行
tree
命令
[root@oldboyedu ~]# tree <-->以实验环境为准
.
└── anaconda-ks.cfg
0 directories, 1 file
范例2: 以树形结构显示目录下的所有内容(
-a
的功能)
[root@oldboyedu ~]# tree -a <-->以 . 开头的文件都显示出来
.
├── anaconda-ks.cfg
├── .bash_history
├── .bash_logout
├── .bash_profile
├── .bashrc
├── .cshrc
├── .gem
│ ├── ruby
│ │ └── cache
│ │ └── paint-2.0.3.gem
│ └── specs
│ └── rubygems.org%443
│ ├── latest_specs.4.8
│ ├── prerelease_specs.4.8
│ ├── quick
│ │ └── Marshal.4.8
│ │ ├── lolcat-99.9.21.gemspec
│ │ ├── manpages-0.6.1.gemspec
│ │ ├── optimist-3.0.0.gemspec
│ │ ├── paint-2.0.0.gemspec
│ │ ├── paint-2.0.1.gemspec
│ │ ├── paint-2.0.2.gemspec
│ │ └── paint-2.0.3.gemspec
│ └── specs.4.8
├── .ssh
│ └── authorized_keys
├── .tcshrc
└── .viminfo
8 directories, 20 files
范例3: 只列出根目录下第一层目录结构(
-L
的功能)
[root@oldboyedu /test]# mkdir -p dir{1..2}/text{1..2} <-->模拟环境,创建目录
[root@oldboyedu /test]# touch dir{1..2}/text{1..2}/{1..2}.txt <-->模拟环境,文件
[root@oldboyedu /test]# tree <-->不加参数的结果
.
├── dir1
│ ├── text1
│ │ ├── 1.txt
│ │ └── 2.txt
│ └── text2
│ ├── 1.txt
│ └── 2.txt
└── dir2
├── text1
│ ├── 1.txt
│ └── 2.txt
└── text2
├── 1.txt
└── 2.txt
6 directories, 8 files
[root@oldboyedu /test]# tree -L 1 <-->加参数的结果
.
├── dir1
└── dir2
2 directories, 0 files
范例4: 只显示所有目录(但不显示文化)
[root@oldboyedu /test]# tree -d dir1 <--> -d 只显示目录
dir1
├── text1
└── text2
2 directories
[root@oldboyedu /test]# ls -l dir1
total 0
drwxr-xr-x 2 root root 32 Apr 6 19:52 text1 <-->text1和text2是目录文件
drwxr-xr-x 2 root root 32 Apr 6 19:52 text2
[root@oldboyedu /test]# cd dir1
[root@oldboyedu /test/dir1]# ls
text1 text2
[root@oldboyedu /test/dir1]# ls text1/
1.txt 2.txt
[root@oldboyedu /test/dir1]# ll text1/
total 0
-rw-r--r-- 1 root root 0 Apr 6 19:52 1.txt <-->1.txt和2.txt是文件
-rw-r--r-- 1 root root 0 Apr 6 19:52 2.txt
范例4: 使用
tree
命令区分目录和文件的方法(常用)
[root@oldboyedu /test]# tree -L 3 -F <-->是目录的话后面加\
.
├── dir1/
│ ├── text1/
│ │ ├── 1.txt
│ │ └── 2.txt
│ └── text2/
│ ├── 1.txt
│ └── 2.txt
└── dir2/
├── text1/
│ ├── 1.txt
│ └── 2.txt
└── text2/
├── 1.txt
└── 2.txt
6 directories, 8 files
[root@oldboyedu /test]# tree -L 1 -F /boot/ |grep /$ <-->过滤以斜线结尾的所有内容
/boot/
├── efi/
├── grub/
├── grub2/
[root@oldboyedu /test]# tree -L 1 -d /boot/ <-->使用 -d参数只显示目录树
/boot/
├── efi
├── grub
└── grub2
3 directories
今天就写到这里,有什么疑问或出现什么错误,随时欢迎大神们发表评论指点迷津