How to use the “touch” command in Linux?

The “touch” command in Linux is used to create a new empty file or update the modification time of an existing file. Using this command, users to quickly create new files without opening a text editor, and you can update the timestamps on existing files. This can be useful in certain scripting & file management cases.

What are the touch syntaxes in Linux?

As we mentioned in the introduction, you can use the touch command to create or modify files. Here is the basic syntax of the touch command:

touch [OPTION]... FILE...

Where:

  • FILE… is the name of the file(s) to be created or modified (for existing files).
  • OPTION is an optional flag that modifies the use case of the touch command. Some common options include:
    • -a or –time=atime to change only the access time of a file.
    • -m or –time=mtime to change only the modification time of a file.
    • -c or –no-create to avoid creating new files if they do not exist.
    • -d or –date=STRING to specify the date and time using the specified string format.
    • -t or –reference=FILE to use the specified file’s timestamps as the reference for the new timestamps.

For a complete list of options, type man touch or touch --help in your terminal.

touch command in Linux

Examples of touch commands in Linux & how to use them?

Here are some examples of the touch command in Linux along with an explanation:

Create a new empty file:

touch newfile.txt

Explanation: This command will create a new empty file named “newfile.txt”. If the file already exists, its modification time will be updated to the current time.

Update the modification time of an existing file:

touch existingfile.txt

Explanation: This command updates the modification time of an existing file named “existingfile.txt”. If the file does not exist, a new empty file will be created.

Create multiple new files at once:

touch file1.txt file2.txt file3.txt

Explanation: This command creates multiple new empty files with the specified names. If any of the files already exist, their modification times will be updated to the current time.

Change only the access time of a file:

touch -a file.txt

Explanation: This command updates the access time of the file “file.txt” to the current time. The modification time will not be affected.

Change only the modification time of a file:

touch -m file.txt

Explanation: This command updates the modification time of the file “file.txt” to the current time. The access time will not be affected.

Specify the date and time for the timestamps:

touch -d '2022-12-25 12:00:00' file.txt

Explanation: This command updates both the access and modification times of the file “file.txt” to the specified date and time.

Use a reference file for the new timestamps:

touch -r referencefile.txt file.txt

Explanation: This command updates both the access and modification times of the file “file.txt” to the timestamps of the reference file “referencefile.txt”.

These are just a few examples of the touch command in Linux. By using different options and combinations of options, you can customize the behavior of the command to fit your specific needs.

Must Read: Practical Uses of Rsync Command in Linux

How to change the modification time using the touch command?

To change the modification time of a file using the touch command in Linux, simply run the following command:

touch filename

Where filename is the name of the file whose modification time you want to change. If the file does not exist, a new empty file with the specified name will be created.

Alternatively, you can use the -m or --time=mtime option to change only the modification time of a file:

touch -m filename

This will update the modification time of the file filename to the current time while leaving the access time unchanged.

How to set specific timestamps using the touch command?

To set specific timestamps for the access and modification times of a file using the touch command in Linux, you can use the -d or --date option followed by the desired date and time. The format of the date and time string should be the format “YYYY-MM-DD hh:mm:ss”. Here’s an example:

touch -d "2022-12-25 12:00:00" filename

Explanation:

  • The touch command is used to change the timestamps of a file.
  • The -d or --date option is used to specify the desired date and time for the timestamps.
  • The date and time string “2022-12-25 12:00:00” specifies the desired date and time for the timestamps.
  • 'filename' is the name of the file whose timestamps you want to change.
  • This will set the access and modification times of the file filename to the specified date and time, “2022-12-25 12:00:00”.

Note that you can use the -a or --time=atime option to set only the access time, and the -m or --time=mtime option to set only the modification time.

How to set timestamps with a reference file using the touch command?

To set the timestamps of a file using the timestamps of a reference file, you can use the touch command in Linux with the -r or --reference option. The syntax is as follows:

touch -r referencefile filename

Explanation:

  • The touch command is used to change the timestamps of a file.
  • The -r or --reference option is used to specify a reference file whose timestamps will be used for the target file.
  • 'referencefile' is the name of the reference file whose timestamps will be used.
  • 'filename' is the name of the target file whose timestamps you want to change.

This will set the access and modification times of the file filename to the timestamps of the file 'referencefile'. If the file filename does not exist, it will be created with the timestamps of the reference file.

Also Read: Move Files and Directories using Linux mv Command

How to set specific access and modification time?

To set specific access and modification times for a file using the touch command in Linux, you can use the -a or --time=atime and -m or --time=mtime options, respectively. The syntax is as follows:

touch -a -t YYYYMMDDhhmm.ss filename
touch -m -t YYYYMMDDhhmm.ss filename

Explanation:

  • The touch command is used to change the timestamps of a file.
  • The -a or --time=atime option is used to set the access time of the file.
  • The -m or --time=mtime option is used to set the modification time of the file.
  • The -t YYYYMMDDhhmm.ss option is used to specify the desired access or modification time in the format “YYYYMMDDhhmm.ss”.
  • 'filename' is the name of the file whose timestamps you want to change.

For example, to set the access time of a file to December 25th, 2022 at 12 PM, and the modification time to December 26th, 2022 at 1 PM, you would run the following commands:

touch -a -t 202212251200.00 filename
touch -m -t 202212261300.00 filename

This will set the access and modification times of the file filename to the specified values. If the file filename does not exist, it will be created with the specified timestamps.

FAQ

What does the ‘touch’ command do?

The ‘touch’ command in Linux is used to update the timestamps of files or create new empty files if they do not already exist.

Can I set the timestamps of a directory using the ‘touch’ command?

No, the ‘touch’ command can only be used to update the timestamps of regular files. It cannot be used to update the timestamps of directories.

What happens if I use the ‘touch’ command on a file that doesn’t exist?

If you use the ‘touch’ command on a file that doesn’t exist, it will create a new empty file with the specified name and timestamps.

Can I set timestamps in the past using the ‘touch’ command?

Yes, you can set timestamps in the past using the ‘touch’ command.

How do I set the timestamps of a file to the current time using the ‘touch’ command?

To set the timestamps of a file to the current time, simply use the touch command without any options or arguments: ‘touch filename’.

Can I set both access and modification timestamps using the same ‘touch’ command?

Yes, you can set both access and modification timestamps using the same touch command by using the ‘-a’ and ‘-m’ options, or the ‘-d’ option with a specific date and time string.

Conclusion

In conclusion, the touch command in Linux is a useful tool for changing the timestamps of files. It can be used to create new files, update the timestamps of existing files, set specific timestamps, set timestamps using a reference file, and set specific access and modification times. By using the various options available, you can fine-tune the timestamps of your files to meet your needs.


Pronay Sarkar

Hello!
I am Pronay Sarkar, I love to write about tech and stuff.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.