Linux touch command

Linux touch command

Linux touch Command – Create and Modify Files

The touch command in Linux is used to create new empty files or update the timestamp of an existing file without modifying its content. It's a simple and efficient way to manage files, making it an essential tool for system administrators and users alike.

Syntax of touch

touch [OPTIONS] FILE(s)
  • OPTIONS → Flags that modify the behavior of touch.
  • FILE(s) → The name(s) of the file(s) you want to create or modify.

Common Usage of touch

1. Create a New Empty File

To create a new, empty file:

touch newfile.txt

Example:

touch myfile.txt

This will create a new file named myfile.txt in the current directory if it doesn't already exist. If the file exists, its timestamp is updated.

2. Update the Timestamp of an Existing File

If the file already exists, the touch command updates its access and modification timestamps to the current date and time without altering its content:

touch existingfile.txt

Example:

touch report.txt

This updates the report.txt file's timestamp.

3. Create Multiple Files

You can create multiple empty files in a single command:

touch file1.txt file2.txt file3.txt

Example:

touch file1.txt file2.txt

This will create file1.txt and file2.txt.

4. Set a Specific Timestamp

To set a specific timestamp for a file, use the -t option followed by the timestamp in the format [[CC]YY]MMDDhhmm[.ss] (e.g., 202502091200.00 for February 9, 2025, at 12:00:00):

touch -t 202502091200.00 file.txt

Example:

touch -t 202412310800.00 event.txt

This sets the timestamp of event.txt to December 31, 2024, at 8:00 AM.

5. Change Only the Access Time (-a)

If you want to change only the access time (the last time the file was read) without modifying the modification time, use the -a option:

touch -a file.txt

Example:

touch -a document.txt

This updates the access time of document.txt without modifying its content or modification timestamp.

6. Change Only the Modification Time (-m)

If you want to update only the modification time (the last time the file was written to) without modifying the access time, use the -m option:

touch -m file.txt

Example:

touch -m notes.txt

This updates the modification time of notes.txt without changing its access timestamp.

7. Use -c to Avoid Creating New Files

If you want to update the timestamp of an existing file but not create new files if the file doesn't exist, use the -c option:

touch -c file.txt

Example:

touch -c report.txt

If report.txt doesn't exist, this command won't create the file.

8. Use -h to Change the Timestamp of Symbolic Links

To change the timestamp of a symbolic link instead of the target file, use the -h option:

touch -h symlink.txt

Example:

touch -h link.txt

This will modify the timestamp of link.txt (if it's a symlink), not the target file it points to.

Examples

1. Create a New File

touch newfile.txt

This creates a new empty file named newfile.txt.

2. Update the Timestamp of an Existing File

touch existingfile.txt

This updates the access and modification timestamps of existingfile.txt.

3. Create Multiple Files

touch file1.txt file2.txt file3.txt

This creates file1.txt, file2.txt, and file3.txt.

4. Set a Specific Timestamp

touch -t 202502091200.00 file.txt

This sets the timestamp of file.txt to February 9, 2025, at 12:00 PM.

5. Change Only the Access Time

touch -a file.txt

This updates only the access time of file.txt.

6. Avoid Creating a File

touch -c nonexistingfile.txt

This will not create nonexistingfile.txt if it doesn't exist.

7. Modify the Timestamp of a Symlink

touch -h symlink.txt

This will modify the timestamp of the symlink symlink.txt without changing the timestamp of the target file.

Error Handling with touch

  • File Not Found: If the file doesn't exist and you don't specify -c, touch will create the file.

    touch: cannot touch 'file_name': No such file or directory
  • Permission Denied: If you do not have sufficient permissions to create or modify the file, you will get a "Permission denied" error:

    touch: cannot touch 'file_name': Permission denied

Conclusion

The touch command is a simple but powerful tool in Linux for creating files, modifying file timestamps, and managing file metadata. Whether you're updating access times, setting specific timestamps, or avoiding file creation, touch offers flexible options for a variety of tasks.

Would you like additional details or SEO optimization? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close