Learn about touch command in Linux

Learn about touch command in Linux

The touch command in Linux is used to create empty files or update the access and modification timestamps of existing files. It’s a versatile command that is often used to indicate the presence of a file or update its timestamp without changing its content.

Usage:

touch [option] file_name

Example:

  1. Creating a New File:
    To create a new empty file named “newfile.txt”:
   touch newfile.txt
  1. Updating Timestamps:
    To update the access and modification timestamps of an existing file named “existing.txt”:
   touch existing.txt
  1. Creating Multiple Files:
    You can create multiple files in a single command:
   touch file1.txt file2.txt file3.txt
  1. Creating Files in Specific Directory:
    To create a new file named “report.txt” in the “documents” directory:
   touch documents/report.txt
  1. Using with Variables:
    In a shell script, you can use the touch command to create files dynamically:
   file_name="dynamic_file.txt"
   touch "$file_name"
  1. Combining with Other Commands:
    You can use touch along with other commands to create and update files:
   touch "$(date +%Y-%m-%d)_log.txt"

This creates a file with a timestamp in its name, such as “2023-08-29_log.txt”.

The touch command is a handy tool for file management and timestamp manipulation. It’s commonly used in shell scripts, automation tasks, and scenarios where updating timestamps is necessary without altering file content.

Leave a Comment

Scroll to Top