
The touch command is a file modification tool that enables you to create new files, update file timestamps, and empty files. It's an essential tool used in administrative and scripting tasks that require file creation or timestamp manipulation.
This article explains how to use the touch in Linux to create and manage files.
touch Command SyntaxBelow is the basic touch command syntax:
Within the above command, filename specifies the filename to create or update while [options] modifies how the touch operates.
touch Command Optionstouch CommandCreate a new empty file.
The above command creates a new empty file file1.txt if it does not already exist in your working directory.
Output:
Update the timestamps of an existing file.
The above command updates the access and modification timestamps of the existingfile.txt file using the system time.
Output:
Create multiple empty files.
The above command creates three empty files file1.txt, file2.txt, and file3.txt.
Output:
Change the access time of a file.
The above command updates the access time of the file1.txt file.
Output:
Change the file modification time.
The above command updates the modification time of the file1.txt file.
Output:
Do not create a file if it does not exist.
The above command does not create file1.txt if exists and updates the existing file's timestamp.
Output:
Set a specific timestamp on a file.
The above command sets the access and modification time of the file1.txt file to May 15, 2024, 09:30 AM.
Output:
Copy the timestamps of an existing file to a new file.
The above command sets the access and modification times of the file1.txt file to match the referencefile.txt file.
Output:
Update timestamps using a string.
The above command updates the access and modification time of the file1.txt file using the specified date and time string.
Output:
Create a file with a specific time.
The above command sets the access and modification time of file1.txt to January 1, 1990, 00:00 AM.
Output:
Create log files in a Bash script using touch.
Create a sample script log_script.sh using a text editor such as Nano
Add the following contents to the file.
Save and close the file.
Enable execute permissions on the file.s
Run the script
The above script creates five empty log files in your working directory using the format logfile_1.log to logfile_5.log.
Output:
Backup a file before modifying the existing file.
The above command creates a backup of the file1.txt file before updating its timestamp.
Output:
Create empty files in nested directories.
The above command creates nested directories and an empty file in each directory.
Output:
Combine touch with find to recursively update timestamps on multiple files.
The above command updates the timestamps of all files in a specific directory.
Output:
Create new files using the current system date and a specific extension.
The above command creates a new empty file with the current system date and the .txt extension.
Output:
Use touch with xargs for batch processing.
The above command creates multiple files in your standard input using the xargs utility.
Output:
Create a series of empty files with different extensions.
The above command creates a series of files with different extensions using the brace expansion method.
Output:
You have used the touch command to create and manipulate files in Linux. For more command options, run the man touch command to view the touch manual page.
0 Comments
Be the first to comment and share your perspective with the community.