文件及目录操作 - 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

今天就写到这里,有什么疑问或出现什么错误,随时欢迎大神们发表评论指点迷津

Shell命令-文件及目录操作之touch、tree的更多相关文章

  1. Shell命令-文件及目录操作之file、md5sum

    文件及目录操作 - file.md5sum 1. file:显示文件的类型 file命令的功能说明 用于辨识文件类型.通过 file 指令,我们得以辨识该文件的类型. file命令的语法格式 file ...

  2. Shell命令-文件及目录操作之ls、cd

    文件及目录操作 - ls.cd 1.ls:列出目录的内容及其内容属性信息 ls命令的功能说明 ls命令用于列出目录的内容及其内容属性信息. ls命令的语法格式 ls [OPTION]... [FILE ...

  3. Shell命令-文件及目录操作之mkdir、mv

    文件及目录操作 - mkdir.mv 1.mkdir:创建目录 mkdir命令的功能说明 mkdir命令用于创建目录,默认情况下,要创建的目录已存在,会提示文件存在,不会继续创建目录. mkdir命令 ...

  4. Shell命令-文件及目录操作之pwd、rm

    文件及目录操作 - pwd.rm 1.pwd:显示当前所在位置信息 pwd命令的功能说明 pwd命令用于显示当前工作目录的绝对路径,以便在各个目录间来回切换. pwd命令的语法格式 pwd [OPTI ...

  5. Shell命令-文件及目录操作之cp、find

    文件及目录操作 - cp.find 1.cp:复制文件或目录 cp命令的功能说明 cp命令用于复制文件或目录. cp命令的语法格式 cp [OPTION]... SOURCE... DIRECTORY ...

  6. Shell命令-文件及目录操作之chattr、lsattr

    文件及目录操作 - chattr.lsattr 1. chattr:改变文件属性 chattr命令的功能说明 chattr命令用于改变文件属性.这项指令可改变存放在ext2文件系统上的文件或目录属性, ...

  7. Linux操作系统常用命令合集——第一篇-文件和目录操作(40个命令)

    一.选项和参数的区别        在经过上一次的系统安装后我们已经成功登陆,登陆LInux后,我们就可以在#或$符后面去输入命令,有的时候命令后面还会跟着“选项”(英文名:options)或“参数” ...

  8. Linux基础命令-文件与目录

    Linux基础命令-文件与目录 参考:<鸟哥linux私房菜>五-七章,17/12/5复习,18/01/15复习 文件权限 rwx421:用户,用户组,其他 umask查看默认权限:000 ...

  9. Python中的文件和目录操作实现

    Python中的文件和目录操作实现 对于文件和目录的处理,虽然可以通过操作系统命令来完成,但是Python语言为了便于开发人员以编程的方式处理相关工作,提供了许多处理文件和目录的内置函数.重要的是,这 ...

随机推荐

  1. 【代码笔记】Web-CSS-CSS 教程

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  2. Azure WebJob-Custom Schedule for Azure Web Job Timer Triggers

    如果想实现Azure Schedule WebJob,有两种方法: 1. 配置CRON Expression,网上有在线CRON配置工具,根据业务需要配置即可 注意:Azure的CRON Expres ...

  3. 优秀代码摘录片段一:LinkedList中定位index时使用折半思想

    在LinkedList有一段小代码,实现的功能是,在链表中间进行插如,所以在插如的过程中会需要找到对应的index位置的node元素: 如果放在平时只为了实现功能而进行遍历查找,很多人会直接使用一个w ...

  4. python 生成图形验证码

    文章链接:https://mp.weixin.qq.com/s/LYUBRNallHcjnhJb1R3ZBg 日常在网站使用过程中经常遇到图形验证,今天准备自己做个图形验证码,这算是个简单的功能,也适 ...

  5. Git 最佳实践:分支管理

    5月份,为统一团队git分支管理规范,刚开始准备自己写,在网上搜了下,发现不少不错的git分支管理实践.最后我为团队选择了这个git分支管理实践 A successful Git branching ...

  6. 使用Connector/C++(VS2015)连接MySQL的完整例子

    完整示例代码1 /* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved. This program is ...

  7. selenium-确定找到的element唯一(三)

    在python + selenium 中经常会遇到找到的元素不唯一,导致定位到的元素不是预期的或者定位不到元素 解决方法:只要在页面进行确认找到的元素唯一后,再进行操作 页面确认方法: 1.通过htm ...

  8. mssql sqlserver 表增加列后,视图不会自动更新相关列的两种解决方法分享

    摘要: 今天对物理数据表,进行增加列操作后,程序一直显示无法找到相应列,通过仔细比对发现,视图中无相应列更新,下文将具体的解决方法分享如下: 例: create view vw_test as sel ...

  9. SQL 使用临时表和临时变量完成update表字段---实际案例

    -- 使用临时表 -- 创建临时表 --ALTER TABLE TS_ExpenseApplication_Reim_Detail ADD BgCode NVARCHAR() NULL, BgItem ...

  10. 20181225-Linux Shell Bash环境下自动化创建ssh互信脚本

    20181225-Linux Shell Bash环境下自动化创建ssh互信脚本 1. 我的Blog 博客园 https://www.cnblogs.com/piggybaba/ 个人网站 http: ...