创建文件命令--touch

touch

作用:常用来创建空文件,如果文件存在,则修改这个文件的时间

补充:文件的三种时间

[root@xuegod63 ~]# ll /etc/passwd  #查看文件修改的时间

-rw-r--r-- 1 root root 2116 11月 16 14:57 /etc/passwd

[root@xuegod63 ~]# stat /etc/passwd  #查看文件属性(其中包括文件时间属性)

  文件:"/etc/passwd"

  大小:2116             块:8          IO 块:4096   普通文件

设备:803h/2051d Inode:9401663     硬链接:1

权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)

最近访问:2017-11-16 14:57:39.923177258 +0800

最近更改:2017-11-16 14:57:39.397177256 +0800

最近改动:2017-11-16 14:57:39.409177256 +0800

注:

访问时间:atime  查看内容   cat  a.txt

修改时间:mtime  修改内容  vim a.txt

改变时间:ctime  文件属性,比如权限  change time。  chmod +x a.sh

 

语法:touch 文件名

[root@xuegod63 ~]# cd /opt/

[root@xuegod63 opt]# touch a.txt

[root@xuegod63 opt]#touch filel file2    #同时指定多个文件名,创建 filel和fle2文件

[root@xuegod63 opt]#touch file{6..20}    #使用命令展开式创建file6到file20的文件。

[root(@xuegod63 opt]# Is
 a.txt file10 file12 file14 file16 file18 file2 file6 file8 rh filel filell file13 file15 file17 file19 file20 file7 file9

    touch命令实际上并不是用于创建文件,而是用于修改时间戳的,只是目标文件不存在时,会自动创建一个空文件,如果目标文件已存在,那么只修改目标文件时间戳。
 touch    文件名    #创建一个空文件。

 touch -c 文件名    #只修改文件时间戳,即使目标文件不存在,也不创建空文件。

 touch -a 文件名    #只修改文件的访问时间为当前时间。

 touch -m 文件名    #只修改文件的修改时间为当前时间。

 touch -m -t 201903181204.44 文件名    #指定修改文件的修改时间格式为年月日时分秒,秒可以不加,默认为0秒。
 touch -a -t 201903181204.44 文件名    #指定修改文件的访问时间格式为年月日时分秒,秒可以不加,默认为0秒。


创建文件的其他两种方法:

方法一:用vim命令创建一个新文件

[root@xuegod63 mnt]# vim xuegod-1.txt

方法二:用重定向创建一新文件

[root@xuegod63 mnt]#echo  aaa > xuegod.txt

版权声明:
作者:WaterBear
链接:https://l-t.top/1152.html
来源:雷霆运维
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>