The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows:
- cat command: It is used to create the file with content.
- touch command: It is used to create a file without any content. The file created using the touch command is empty. This command can be used when the user doesn’t have data to store at the time of file creation.
Using touch Command Initially, we are in the home directory and this can be checked using the pwd command. Checking the existing files using the command ls and then long listing command(ll) is used to gather more details about existing files. As you can see in the below figure there are no existing files.
Syntax of `touch` command in Linux:
Touch command Syntax to create a new file:
You can create a single file at a time using touch command.
The file which is created can be viewed by ls command and to get more details about the file you can use long listing command ll or ls -l command . Here file with name ‘File1‘ is created using touch command.
Touch command to create multiple files:
Touch command can be used to create multiple numbers of files at the same time. These files would be empty while creation.
touch File1_name File2_name File3_name
Multiple files with name Doc1, Doc2, Doc3 are created at the same time using touch command here.
Options Available in `touch` command in Linux
Like all other command Touch command have various options. These options are very useful for various purpose.
This command is used to change access time only. To change or update the last access or modification times of a file touch -a command is used.
touch -a fileName
Here touch -a command changes access time of the file named Doc1.
touch `-c` option
This command is used to check whether a file is created or not. If not created then don’t create it. This command avoids creating files.
touch -c fileName
touch `-c` `-d` option
This is used to update access and modification time.
touch -c-d fileName
touch `-m` option
This is used to change the modification time only. It only updates last modification time.
touch -m fileName
touch `-d` ” ” option
This command is used to change only modification date.
touch -d "17 Mar 2023" Geek.txt
touch `-r` command
This command is used to use the timestamp of another file. Here Doc2 file is updated with the time stamp of File 1.
touch -r second_file_name first_file_name
touch `-t` command
This is used to create a file using a specified time.
touch -t YYMMDDHHMM fileName