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:
- Creating a New File:
To create a new empty file named “newfile.txt”:
touch newfile.txt
- Updating Timestamps:
To update the access and modification timestamps of an existing file named “existing.txt”:
touch existing.txt
- Creating Multiple Files:
You can create multiple files in a single command:
touch file1.txt file2.txt file3.txt
- Creating Files in Specific Directory:
To create a new file named “report.txt” in the “documents” directory:
touch documents/report.txt
- Using with Variables:
In a shell script, you can use thetouch
command to create files dynamically:
file_name="dynamic_file.txt"
touch "$file_name"
- Combining with Other Commands:
You can usetouch
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.