Touch command is used in Linux, Ubuntu operating system to create empty files, changing or modifying the timestamp of existing files and directories.
In Linux OS every file has last access and modification time. You can check this through stat command or ll command.
1 |
stat test.txt |
1 2 3 4 5 6 7 |
File: `test.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 805h/2053d Inode: 2360339 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/rajkumar) Gid: ( 1000/rajkumar) Access: 2013-01-06 16:56:58.006804786 +0530 Modify: 2013-01-06 16:56:50.118805195 +0530 Change: 2013-01-06 16:56:50.118805195 +0530 |
stat command shows the complete timestamp of a file. It shows the last access,modify and change timestamp.
1 |
ll test.txt |
1 |
-rw-rw-r-- 1 rajkumar rajkumar 0 Jan 6 16:56 test.txt |
So whenever we create or modify an existing file it’s timestamp automatically updated.
Touch Command with Examples in Linux
Touch command is used to create empty files, modify access time of files and directories.
Before we begin let’s check some options with touch command.
-a – Change the access time.
-c – If the file does not exist, do not create it
-d – Update the access and modification times
-m – Change the modification time only
-r – Use the access and modification times of file
-t – Create a file using a specified time
Creating an Empty File through touch Command
1 |
# touch test.txt |
Use -c with touch command if you don’t want to create new file if it doesn’t exist.
1 |
# touch -c test1.txt |
Create multiple files using touch command.
1 |
# touch test1.txt text2.txt test3.txt |
Change Access and Modification Timestamp of a File using touch command
You can use -a switch with touch command to change the timestamp of a file.
1 |
# touch -a test.txt |
This file is already created by using touch -a with filename access time of this file is updated to the current time.
Change Last Modification time with Touch Command
1 |
# touch -m test.txt |
Copy the Timestamp of Another File
You can also update the timestamp of another file using -r switch with touch command.
1 |
# touch test.txt -r test1.txt |