Introduction:

In Linux, the “touch” command is a versatile tool for file manipulation and modification. While its primary function is to update the timestamps of files, the “touch” command can also be used to create new files, change access permissions, and set specific timestamps. In this article, we will explore eight practical examples of the “touch” command in action, providing step-by-step instructions to help you become proficient in file management and customization in Linux.

Creating a New File:

The simplest use of the “touch” command is to create a new file. Open the terminal and enter the following command:

touch newfile.txt

This will create a new file named “newfile.txt” in the current directory.

Updating Timestamps:

The primary purpose of the “touch” command is to update the timestamps of files. To update the access and modification timestamps of a file, use the “touch” command followed by the file name. For example:

touch file.txt

This will update the timestamps of “file.txt” to the current date and time.

Creating Multiple Files:

The “touch” command supports creating multiple files simultaneously. To create multiple files, provide the file names separated by spaces. For example:

touch file1.txt file2.txt file3.txt

This will create three files: “file1.txt,” “file2.txt,” and “file3.txt.”

Changing Access Timestamp Only:

By default, the “touch” command updates both access and modification timestamps. However, if you want to update only the access timestamp without modifying the file’s content, use the “-a” option:

touch -a file.txt

This will update the access timestamp of “file.txt” without changing the modification timestamp.

Setting Custom Timestamps:

The “touch” command allows you to set custom timestamps for a file using the “-t” option. The timestamp should be in the format “CCYYMMDDhhmm.ss,” representing the year, month, day, hour, minute, and second. For example:

touch -t 202201011200.00 file.txt

This will set the timestamp of “file.txt” to January 1, 2022, 12:00 PM.

Copying Timestamps from Another File:

If you want to copy the timestamps from an existing file to another file, use the “–reference” option with the “touch” command. For example:

touch --reference=source.txt destination.txt

This will set the timestamps of “destination.txt” to match those of “source.txt.”

Updating Timestamps Recursively:

To update the timestamps of multiple files and directories recursively, use the “-r” option with the “touch” command. For example, to update the timestamps of all files and directories within a directory called “folder,” use the following command:

touch -r folder/

This will update the timestamps of all files and directories within the “folder” directory.

Changing File Access Permissions:

In addition to timestamp manipulation, the “touch” command can also be used to change file access permissions. Use the “-m” option followed by the desired permission mode to modify the access permissions of a file. For example:

touch -m 755 file.txt

This will set the access permissions of “file.txt” to read, write, and execute for the owner, and read and execute for others.

Conclusion:
With these eight practical examples of the “touch” command in Linux, you have gained valuable knowledge and skills for file manipulation,

Comments to: 8 Practical Examples of Linux “Touch” Command

    Your email address will not be published. Required fields are marked *