Linux下必须掌握的6条Touch命令技巧

417

前言

linux中创建文件可以使用touch命令,但好奇的是为什么叫touch,貌似和文件没有一点关系,除了他还可以使用'>'、'>>',但是touch命令远不止创建文件这样简单,下面列举了6条常用的技巧。

1.创建单个文件

hxl@hxl-PC:/home/HouXinLin/touch$ touch test.txt
hxl@hxl-PC:/home/HouXinLin/touch$ ls
test.txt
hxl@hxl-PC:/home/HouXinLin/touch$ 

2.创建多个文件

hxl@hxl-PC:/home/HouXinLin/touch$ touch 1.txt 2.txt
hxl@hxl-PC:/home/HouXinLin/touch$ ls
1.txt  2.txt  test.txt
hxl@hxl-PC:/home/HouXinLin/touch$ 

3.创建大量文件

如果要测试需要生成大量文件,可以使用以下方式

hxl@hxl-PC:/home/HouXinLin/touch$ touch {1..10}
hxl@hxl-PC:/home/HouXinLin/touch$ ls
1  10  2  3  4  5  6  7  8  9
hxl@hxl-PC:/home/HouXinLin/touch$ 

也可以附带后缀。

hxl@hxl-PC:/home/HouXinLin/touch$ touch {1..10}.txt
hxl@hxl-PC:/home/HouXinLin/touch$ ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
hxl@hxl-PC:/home/HouXinLin/touch$ 

4.更新时间

如果只想更新现有文件的访问时间,可以使用“-c”参数。

touch -c hello.txt

当然还有-m表示改变文件的修改时间。

5.设置特定的访问

要将访问/修改时间设置为特定日期时间,使用-t选项选项。

hxl@hxl-PC:/home/HouXinLin/touch$ ls -l
总用量 0
-rw-r--r-- 1 hxl hxl 0 114 19:51 a.txt

hxl@hxl-PC:/home/HouXinLin/touch$ touch -c -t 202012111111 a.txt 
hxl@hxl-PC:/home/HouXinLin/touch$ ls -l
总用量 0
-rw-r--r-- 1 hxl hxl 0 1211  2020 a.txt

 6.使用另一个文件的时间戳作为参考

touch -r ref.txt abc.txt

上述命令将abc.txt的访问/修改时间设置为ref.txt的访问/修改时间