Creating files is one of the most common things we do. Every now and then, you may also need to modify file timestamps. The touch
command in Linux is a two-in-one solution for creating new files and changing file timestamps of older files.
In this article, we will discuss the touch
command in Linux and how it can create, modify, or change the timestamps of a file.
Table of Contents
- File Timestamps in Linux
- What is touch Command used for?
- Touch Command: Syntax
- Touch Command Options
- Create an Empty File using the Touch Command
- Create Multiple Files using the Touch Command
- Create and Populate a File using the Touch Command
- Change Only File Access Time or Modification Time
- Set File Access and Modification Time
- Use Timestamp of Another File
- Avoid Creating New File
- How Does the Touch Command Works in the case of a Symbolic Link?
- Conclusion
File Timestamps in Linux
Before diving deeper into touch
command usage, you first need to understand file timestamps.
In Linux, each file has three timestamps: access time (atime
), modified time (mtime
), and the changed time (ctime
).
- Access Time (
atime
) refers to the time that the file was last viewed, but not edited. - The modify Time (
mtime
) signifies the time that the file was modified. - Change Time (
ctime
) shows the metadata changes to a file. For example, file permission changes.
To check file status including file timestamps, you can use the stat command followed by the file name:
stat filename
What is touch Command used for?
The touch
command in Linux is used to create, modify, or change timestamps of one or more files. It creates an empty file, unlike the cat
command that creates a non-empty file.
Touch Command: Syntax
The primary use of the touch
command is to modify file timestamps, however, if the file does not exist, then it will create an empty file. The syntax for the touch command is as follows:
touch [OPTIONS]... [FILENAME]...
Touch Command Options
Touch Command Options | Description |
---|---|
-a | Change (only) the file access time of a file |
-c | Do not create a file if it doesn’t already exist |
-d | Update the access and modification time of a file |
-m | Change (only) the modification time of a file |
-r | Use the access and modification times of a file |
-t | Create a file using a specified time |
Create an Empty File using the Touch Command
The very basic functionality of touch
is to create an empty file. To create an empty file in the current directory, type touch
followed the file name:
touch emptyFile
To create an empty file in another directory, pass the path to the directory before the filename:
touch path/to/emptyFile
If a file already exists by the same name, then touch
will change the last access and modification time to the current time.
Next, you should verify the command with ls. It will list all files in the current directory (except the hidden files) so you can verify the newly-created file.
ls
Create Multiple Files using the Touch Command
To create multiple files using the touch
command, you simply pass multiple file names followed by a space consecutively:
touch file01 file02 file03 file04
You can also go one step further and make the process cleaner by creating multiple files using regular expressions:
touch file{1, 2, 3, 4, 5, 6}
Create and Populate a File using the Touch Command
In Linux, the cat
command is the conventional way of creating non-empty files. However, we can also create non-empty files in Linux using the touch
command:
touch filename
Once you’ve created the empty file, the next step is to populate it using another command. We will show you some common commands such as echo, printf, or seq to add content.
Echo Command to Add Content
echo "Adding content using the echo command" > filename
Printf Command to Add Content
touch "Adding content using the printf command" > filename
seq Command to Add Content
seq 10 > filename
You can use the cat command to verify the content inside the files. It’s a multi-purpose command that can perform multiple operations ranging from creating the file to viewing file content and more.
To view the content of your newly-created file, type cat followed by the file name:
cat filename
Change Only File Access Time or Modification Time
As we discussed earlier, the touch
command changes the access and modification times to the current time if we don’t pass any arguments. However, if you want to change only the access time or modification time using the -a
or -m
options.
Change File Modification Time
To change the file modification time of a given file, use the -m
option:
touch -a filename
Change File Access Time
To change the access time of a file, use the -a
option:
touch -m filename
[powerkit_alert type=”info” dismissible=”false” multiline=”false”]
Important: making any changes to the modification time (mtime) will automatically update the change time (ctime).
[/powerkit_alert]
Set File Access and Modification Time
The touch
command can be used to create or update a file with a specific timestamp.
Update Specific Timestamp for an Old File
To update the access and modification time of a file to first February 2022, you can use the -d
option. Remember to enclose the date string and time into single quotes:
touch -d '1 February 2022 5:02' file01
Important: if you enter a partial date-string, then the rest will be automatically switched to the current one.
Create New File with a Specific Timestamp for an Old File
To create a new file with a specific time, use the -t
option to specify a timestamp. The timestamp should be in the following format YYMMDDHHMM
touch -t 2112010502 file02
Use Timestamp of Another File
The touch command allows users to use the timestamps of another file instead of the current time.
To use the timestamp of file01 in file02, use the -r
option between the names of both files::
touch file1 -r file2
Avoid Creating New File
If you want to update the timestamp of a re-existing file without creating a new one, then use the -c
option:
touch -c file.txt
How Does the Touch Command Works in the case of a Symbolic Link?
In Linux, passing a symbolic link file name to the touch
command changes the access and modification timestamps of the original file. However, the -h
(--no-dereference
) option allows users to override this behavior and modify timestamps of the symbolic link.
To change the access and modification timestamps of a symbolic link:
touch -h symlink01
Conclusion
We have covered the common uses of the touch
. To learn more about it you can check the manpage,and if you encounter any issues or have any questions feel free to leave a comment below and we’ll get back to you as soon as possible.