In Linux, the “touch” command is used to create new empty files or update the access and modification timestamps of existing files. Here’s how to use the “touch” command:
- Open a Terminal:
Launch a terminal emulator on your Linux system.
- Type the “touch” command:
Simply type “touch” followed by the name or names of the files you want to create or update. For example, to create a new empty file named “myfile.txt”, run:
If the file already exists, the “touch” command will update its access and modification timestamps to the current time.
- Create Multiple Files:
You can create multiple files at once by specifying their names separated by spaces. For example, to create three files named “file1.txt”, “file2.txt”, and “file3.txt”, run:
touch file1.txt file2.txt file3.txt
- Specify Timestamps:
You can use the “-t” option followed by a specific timestamp to set a custom access and modification time for the file. The timestamp should be in the format “YYYYMMDDHHMM”. For example, to set the timestamps of a file named “myfile.txt” to January 1, 2023, at 12:00 PM, run:
touch -t 202301011200 myfile.txt
- Create Files with Parent Directories:
If you want to create a file within a specific directory that does not exist yet, you can use the “-p” option. For example, to create a file named “myfile.txt” within a directory named “mydir” (creating the directory if it doesn’t exist), run:
touch -p mydir/myfile.txt
- Verifying the File:
After running the “touch” command, you can verify that the file has been created or updated by using the “ls” command to list the contents of the current directory or the relevant directory. For example:
You should see the newly created or updated file listed.
- Exiting the Command:
The “touch” command doesn’t produce any output unless there is an error. Once you create or update the desired files, you can continue executing other commands or exit the terminal as needed.
The “touch” command is a handy tool for creating new empty files and updating file timestamps in Linux. It allows you to quickly create files, modify their timestamps, and organize your file system based on access and modification times.