Insight: Examples of touch command in Linux
What Is A Touch Command ?
In a simple definition, we can call touch command as a file processing command. The touch command is the easiest way to create new files. Touch command is used to modify the timestamps of files.
What Is A Touch Command: Examples of Touch Command in Linux
Touch command syntax is
touch [option] file_name(s)
When touch command is used without any options, it creates new files for any file names ( provided as arguments ).
touch itsubuntu.txt
1. How to create a blank file or an empty file
touch <filename>
2. How to create multiple files
touch <file1> <file2> <file3>
3. How to avoid creating new files
Touch command create an empty file if the input file doesn’t exist otherwise it will update the timestamps of the input file.
Use the option “-c”.
touch -c itsubuntu.txt
4. How to change access time of a file
touch -a itsubuntu.txt
5. How to use the timestamp of another file as reference
Set the access/modify time of fedora.txt to that of itsubuntu.txt
touch -r itsubuntu.txt fedora.txt
6. How to change access time of a file
Use the command below to change the access time of a file using the”-a”option with the file name.
touch -a itsubuntu.txt
Now, check it if it is changed or not?
stat itsubuntu.txt
7. How to update only modify time of a file
touch -m itsubuntu.txt
Now check it.
stat itsubuntu.txt
8. Change timestamp of a symbolic link
You can use the -h option.
touch -h <symbolic_link>
9. Create a File using a specified time
Use the following command to create a file with specified time other than the current time
touch -t YYMMDDHHMM.SS itsubuntu
10. Create a multiple files
# Create files with names 1 to 20
touch {1..20}
# Create files with names A to Z
touch {A..Z}
# Create files with extension
touch {1..1000}.txt
# Create 10K files
touch {1..10}{1..1000}
List of options with Touch Command
- -a – Access time of a file.
- -c – Do not create a specified file if it does not exist.
- -m – Change the modification time of file.
- -r -use this file’s times instead of current time.
- -t – use [[CC]YY]MMDDhhmm[.ss] instead of current time.
- -d, –date=STRINGparse STRING and use it instead of current time.
Let us know if there is error in the commands mentioned above.