Linux: 8 practical examples of the touch command
What's in the box?
In this article, we'll look at a few practical examples of how to use the touch command under Linux.. Although basic, it is an essential command that is integrated into the various distributions. Unix/Linux. This command is used to create files, but also to modify a file's timestamps.
Under Linux, each file is associated with timestamps, and each file stores information on the last time it was accessed, the last time it was modified and the last time it was changed. So every time we create a new file, access an existing one or modify it, the file's timestamps are automatically updated with the current date and time.
Here are 8 examples to discover without further ado....
1. How do I create an empty file?
The following command will create a file named "tutobox" which will be empty, but present on the server's storage space. We could also add the extension to the file.
touch tutobox
2. How do I create multiple files?
The touch command is capable of creating several files at once, simply by chaining the names together, separating them with a space, like this:
touch tutobox1 tutobox2 tutobox3
3. How can I avoid creating a new folder?
The use ofoption -c with the touch control avoids creating new files. For example, the following command will not create a file called "tutobox" if it does not exist. A way of testing whether or not a file exists.
touch -c tutobox
4. Modify date and time of access and last modification
To change or update the latest access and modification times for a file called "tutobox", use the -a option. Here, no date or time is specified, so the command will set the current time and date on the targeted file.
Note: if the "tutobox" file doesn't exist, the command will create this empty file.
touch -a tutobox
5. How do I change a file's modification date and time?
If you wish to modify only the modification time of a file called "tutobox", use theoption -m with the touch control. In this case, unlike the previous example, the command will only update the date and time of the last modificationand not the date and time of the last access to this file.
touch -m tutobox
6. Explicitly define access and modification date and time
You can explicitly set the date and time using the -c and -t options with the touch control. This means that custom values are used instead of the current date and time. Here's the format to follow :
touch -c -t YYYYMMDDhhmm <file name
To set the July 21, 2022 à 08:00 on the " tutobox" we use :
touch -c -t 202207210800 tutobox
The "stat" command is used to obtain information about the file, such as the creation date, last access date, etc... This will allow you to make a before/after analysis to ensure that the modification has worked.
stat tutobox
This gives :
7. How do I use another file's timestamp?
The following touch command with theoption -r will update the timestamp of the tutobox1 file with the timestamp of the tutobox2 file, as if copying the information. In this way, both files have the same timestamp.
touch -r tutobox2 tutobox1
8. Create a file using a specific date and time?
To create a file with a specific date and time of last access and modification, use the "-t" option and specify a date and time, as described above. Note that this does not change the file creation date.
To create a file named "tutobox" with the date and time "July 20, 2022 at 1:00 PM", use :
touch -t 202207201300 tutobox
Thanks to this article from the Computer Tutorials box, you'll be able to use the touch command to create files, but also act on the different dates and times of the files.
Resources :