preface

In Linux, you can use the touch command to create files, but it doesn’t have anything to do with files, except that it can also use ‘>’ and ‘>>’, but the touch command is much more than just creating files. Here are 6 common techniques.

1. Create a single file

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

2. Create multiple files

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$ 

Copy the code

3. Create lots of files

If you need to generate a large number of files for testing, you can use the following method

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$ 
Copy the code

Suffixes can also be attached.

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$ 

Copy the code

4. Update time

If you only want to update the access time of existing files, you can use the “-c” parameter.

touch -c hello.txt
Copy the code

There is also -m to change the modification time of the file.

5. Set specific access

To set the access/modify time to a specific date time, use the -t option option.

hxl@hxl-PC:/home/HouXinLin/touch$ls -l Total usage0
-rw-r--r-- 1 hxl hxl 0 11month4 19:51 a.txt

hxl@hxl-PC:/home/HouXinLin/touch$ touch -c -t 202012111111 a.txt 
hxl@hxl-PC:/home/HouXinLin/touch$ls -l Total usage0
-rw-r--r-- 1 hxl hxl 0 12month11  2020 a.txt
Copy the code

6. Use the timestamp of another file as a reference

touch -r ref.txt abc.txt
Copy the code

The above command sets the access/modification time of abc.txt to the access/modification time of ref.txt