Linux Touch command in detail


Linux Touch command in detail
Hiiee... everyone!!
Today i am going to explain in detail about the most used command in unix/linux history "touch command".New guys to linux want to know about the touch command in linux.
touch command in unix and touch command syntax in Linux/unix machine
Here we have explained touch command in linux with examples
As we all know that in Unix/Linux every single file is associated with timestamps, and we also have seen that every file stores the information of last access time, last modification time and last change time.
That's why whenever a user create a new file in linux, access or modify an existing file, the timestamps of that file automatically changed or you can say it got updated.
Everyone who use linux/unix have used this command.
Most people used it to create an empty file as it do. But It's not only used to create an empty file, it is something more than that. Altough this topic is related with file let me introduce a little bit about the concept of File in unix/linux..
As u all guys have heared about the famous statement that
"On a UNIX/LINUX system, everything is a file; if something is not a file, it is a process."
Lets have a Brief Explanation about this Statement to make File concept clear.
I found this statement is true bcz as we all know that there are special files that are more than just files (named pipes and sockets) right?
But to keep these things simple, saying that everything in unix and Linux is a file is an acceptable generalization.
"Linux is just like UNIX, and it makes no difference between a file and a directory."
And i hope you guys don't have a shock after hearing above statement, altough i love to shock people with some simple concepts.
Let me explain my views about the above statements:
since a directory is just a file containing names of other files.
Programs, services, texts, images, and so forth, are all files.
Input and output devices, and generally all devices, are considered to be files, according to the system.
Now Let me allow to introduce one of my favourite command in linux "touch command".
Almost every linux/unix users have used this command but still only few have the idea of the strength of this command.
Linux is so strong in itself but some of its command show the demostration of real linux strength.
Touch command Explanation with Examples:
1.Create an empty File
#touch file1
this command create an empty file named file1.
2.Create multiple Empty Files in Linux
#touch a b c d e f g h
#touch 1 2 3 4 5 6 7 8 9
3.Create filess from A to Z
#touch {A..Z}
4.Create 20 Files at a time from 1 to 20.
#touch {1..20}
5.Create 1000 Text Files from 1 to 1000.
[root@localhost satish]# touch {1..1000}.txt
[root@localhost satish]# ls
1000.txt 182.txt 264.txt 346.txt 428.txt 50.txt 592.txt 674.txt 756.txt 838.txt 91.txt
100.txt 183.txt 265.txt 347.txt 429.txt 510.txt 593.txt 675.txt 757.txt 839.txt 920.txt
101.txt 184.txt 266.txt 348.txt 42.txt 511.txt 594.txt 676.txt 758.txt 83.txt 921.txt
102.txt 185.txt 267.txt 349.txt 430.txt 512.txt 595.txt 677.txt 759.txt 840.txt 922.txt
103.txt 186.txt 268.txt 34.txt 431.txt 513.txt 596.txt 678.txt 75.txt 841.txt 923.txt
104.txt 187.txt 269.txt 350.txt 432.txt 514.txt 597.txt 679.txt 760.txt 842.txt 924.txt
105.txt 188.txt 26.txt 351.txt 433.txt 515.txt 598.txt 67.txt 761.txt 843.txt 925.txt
106.txt 189.txt 270.txt 352.txt 434.txt 516.txt 599.txt 680.txt 762.txt 844.txt 926.txt
107.txt 18.txt 271.txt 353.txt 435.txt 517.txt 59.txt 681.txt 763.txt 845.txt 927.txt
108.txt 190.txt 272.txt 354.txt 436.txt 518.txt 5.txt 682.txt 764.txt 846.txt 928.txt
109.txt 191.txt 273.txt 355.txt 437.txt 519.txt 600.txt 683.txt 765.txt 847.txt 929.txt
10.txt 192.txt 274.txt 356.txt 438.txt 51.txt 601.txt 684.txt 766.txt 848.txt 92.txt
110.txt 193.txt 275.txt 357.txt 439.txt 520.txt 602.txt 685.txt 767.txt 849.txt 930.txt
6. Create 1000 Files in a manner like a1,a2,.......a1000
[root@localhost satish]# touch a{1..1000}
7.Create Files with echo command
[root@localhost satish]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
8.You can Create millions of Files at a time by touch command
#touch {1..1000}{1..1000}
Now I am going to Explain you how you can have fun with Files timestamps.
For that here i will create two files "satish.txt" and "sarath.txt"
Then we will notice the information of last access time, last modification time and last change time.
Then we will use touch command to create, change and modify timestamps of a file.
Lets Create satish.txt and sarath.txt Files here First.
[root@localhost satish]# touch satish.txt sarath.txt
[root@localhost satish]# ls
sarath.txt satish.txt
[root@localhost satish]# stat satish.txt
File: `satish.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 11272254 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-01-16 16:02:37.000000000 +0530
Modify: 2013-01-16 16:02:37.000000000 +0530
Change: 2013-01-16 16:02:37.000000000 +0530
Hence you can clearly see stat command response.Hence you can see the Access time, Modification Time and Change time of any file.
All these three comes under files timestamps.
And touch command is able to change that timestamps and misguide.So I want to say that If an intruder can once get access to your system he/she can have fun with your files timestamp.
Lets Start explaning the changing of these timestamps with the help of few Examples.
Example1:How to Change File Access and Modification Time of file satish.txt
[root@localhost satish]# stat satish.txt
File: `satish.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 11272254 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-01-16 16:02:37.000000000 +0530
Modify: 2013-01-16 16:02:37.000000000 +0530
Change: 2013-01-16 16:02:37.000000000 +0530
[root@localhost satish]# touch -a satish.txt
Above command will change the access time os file satish.txt here.
Now To see the statics for changes made we need to take help of stat command here...
[root@localhost satish]# stat satish.txt
File: `satish.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 11272254 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-01-16 16:12:09.000000000 +0530
Modify: 2013-01-16 16:02:37.000000000 +0530
Change: 2013-01-16 16:12:09.000000000 +0530
[root@localhost satish]#
You can see the Access time has been changed.
Note:If file is not already exist it will create a new file.
But If you don't want to create a new file if satish.txt file does not exists then use the following command.
#touch -c satish.txt
It will avoid creating a new File.
Example2: How to change file modification time?
[root@localhost satish]# touch -m satish.txt
This will change the modification time of file satish.txt
[root@localhost satish]# touch -m *.txt
this will change the modification time of all .txt files
[root@localhost satish]# stat satish.txt
File: `satish.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 11272254 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-01-16 16:12:09.000000000 +0530
Modify: 2013-01-16 16:18:03.000000000 +0530
Change: 2013-01-16 16:18:03.000000000 +0530
Example3:How to set the Access and Modification time?
#touch -c -t YYDDHHMM filename
for eg:
#touch -c -t 12101820 satish.txt
This command sets the Access and Modification date and time to a file satish.txt as 18:20 pm December10 of 2013.
You can See the Access and Modification date and time by
#ls -l command
[root@localhost satish]# touch -c -t 12101820 satish.txt
[root@localhost satish]# ls -l
total 0
-rw-r--r-- 1 root root 0 Jan 16 16:02 sarath.txt
-rw-r--r-- 1 root root 0 Dec 10 2013 satish.txt
[root@localhost satish]#
Example4: How to use timestamp of other File
Here I am going to use the time stamp of file sarath.txt
[root@localhost satish]# ls -l
total 0
-rw-r--r-- 1 root root 0 Jan 16 16:02 sarath.txt
-rw-r--r-- 1 root root 0 Dec 10 2013 satish.txt
[root@localhost satish]# touch -r satish.txt sarath.txt
Above command will update the time stamp of satish.txt file with the time-stamp of sarath.txt file.
[root@localhost satish]# ls -l
total 0
-rw-r--r-- 1 root root 0 Dec 10 2013 sarath.txt
-rw-r--r-- 1 root root 0 Dec 10 2013 satish.txt
Here i have updated the timestamp of file satish.txt by timestamp of file sarath.txt
Example5:create a file with specified time other than the current time
# touch -t YYMMDDHHMM.SS linux.txt
The following explains the above format:
- CC – first two digits of the year
- YY – last two digits of the year. If the value of the YY is between 70 and 99, the value of the CC digits is assumed to be 19. If the value of the YY is between 00 and 37, the value of the CC digits is assumed to be 20.
- It is not possible to set the date beyond January 18, 2038.
- MM – month
- DD – date
- hh – hour
- mm – minute
- SS – seconds
Example6:Allow the user to add a specific last access time using option "-d "and "-t".
First See the List of File available for testing purpose.
[root@satish test]# ls
decryptedfile.zip linux.txt satish.txt
level6zip.des3 sarath.txt slashroot.txt
now we want to experiment on satish.txt file so let's check it's complete timestamp first.
[root@satish test]# stat satish.txt
File: `satish.txt'
Size: 115467 Blocks: 240 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311845 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-04 23:12:31.000000000 -0400
Modify: 2013-04-19 05:26:32.000000000 -0400
Change: 2013-04-23 02:09:14.000000000 -0400
Now using option -d we will add specific last access time.
[root@satish test]# touch -d "24 may" satish.txt
Now after making changes see the timestamp again to compare the result from previous timestamp output.
[root@satish test]# stat satish.txt
File: `satish.txt'
Size: 115467 Blocks: 240 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311845 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-24 00:00:00.000000000 -0400
Modify: 2013-05-24 00:00:00.000000000 -0400
Change: 2013-05-23 03:53:19.000000000 -0400
[root@satish test]#
when you compare the output of previous value of satish.txt file access and modification time with the current access and modification time value you will easily see the changes.
Note:If we provide only time using -d option then it will automatically change the date to current date.
Example:
[root@satish test]# touch -d "15:50" satish.txt
You have new mail in /var/spool/mail/root
[root@satish test]# stat satish.txt
File: `satish.txt'
Size: 115467 Blocks: 240 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311845 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-23 15:50:00.000000000 -0400
Modify: 2013-05-23 15:50:00.000000000 -0400
Change: 2013-05-23 04:08:46.000000000 -0400
How to change both the access and modification time together at a time to current time?
[root@satish test]# stat satish.txt
File: `satish.txt'
Size: 115467 Blocks: 240 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311845 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-23 15:50:00.000000000 -0400
Modify: 2013-05-23 15:50:00.000000000 -0400
Change: 2013-05-23 04:08:46.000000000 -0400
Now run the touch command with option "a" and "m"
here option "a" will change the access time.
and option "m" will change the modification time of file.
[root@satish test]# touch -am satish.txt
Now after making the change you can view the statics to check whether your command is working properly or not using stat command.
[root@satish test]# stat satish.txt
File: `satish.txt'
Size: 115467 Blocks: 240 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311845 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:23:03.000000000 -0400
How to use time stamp of a file instead of current time?
here we are going to use the times of file satish.txt for sarath.txt file.
[root@satish test]# touch -r satish.txt sarath.txt
[root@satish test]# stat sarath.txt satish.txt
File: `sarath.txt'
Size: 11470 Blocks: 24 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311844 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:40:55.000000000 -0400
File: `satish.txt'
Size: 115467 Blocks: 240 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311845 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:23:03.000000000 -0400
How to modify the time stamp of a file by going Forward with specific number of seconds.
[root@satish test]# touch -r satish.txt F 50 sarath.txt
The above command will make file satish.txt 50 seconds older than sarath.txt.You can confirm it by running stat command.
[root@satish test]# stat sarath.txt satish.txt
File: `sarath.txt'
Size: 11470 Blocks: 24 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311844 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:46:07.000000000 -0400
File: `satish.txt'
Size: 115467 Blocks: 240 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311845 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:23:03.000000000 -0400
[root@satish test]#
SIMILLARLY:The below command with option "B" will make file sarath.txt older 50 seconds than satish.txt.
[root@satish test]# touch -r satish.txt B 50 sarath.txt
[root@satish test]# stat sarath.txt satish.txt
File: `sarath.txt'
Size: 11470 Blocks: 24 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311844 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:52:27.000000000 -0400
File: `satish.txt'
Size: 115467 Blocks: 240 IO Block: 4096 regular file
Device: 805h/2053d Inode: 10311845 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2013-05-23 04:23:03.000000000 -0400
Modify: 2013-05-23 04:23:03.000000000 -0400
Change: 2013-05-23 04:23:03.000000000 -0400
Add new comment