Thursday, July 7, 2016

10 practical examples of Touch command on Linux

http://www.binarytides.com/linux-touch-command

Linux Touch command

The touch command can be used to modify the access/modification timestamps of files. It is more often used to actually just create an empty file quickly.
This post shows some very simple and quick examples of using the touch command to modify timestamps or create files.

1. Create a blank file

To simply create a blank file with touch command, use the syntax below.
$ touch abc.txt
If the file already exists, its access time will be updated.

2. Create multiple files with touch

To create multiple files, specify their names together separated by a space.
$ touch abc.txt cde.txt xyz.txt

3. Create lots and lots of files

If for some reason you wish to create lots of files, then commands like these would be very helpful
# Create files with names A to Z
$ touch {A..Z}

# Create files with names 1 to 20
$ touch {1..20}

# Create files with extension
$ touch {1..1000}.txt

# Create 10K files
$ touch {1..10}{1..1000}
And then use the ls command to see what all has been created.

4. Avoid creating new files

If you want to just update the access time of existing file, without creating it, use the '-c' option. If the file exists, touch will update the access time, else will do nothing
$ touch -c hello.txt

5. Change file access time - 'a'

To change only access time of a file use the '-a' option with the file name.
$ touch -a abc.txt
To check the access time use the stat command
$ stat a.txt 
  File: ‘a.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d      Inode: 5904730     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/enlightened)   Gid: ( 1000/enlightened)
Access: 2016-03-10 15:04:24.281533071 +0530
Modify: 2016-03-10 15:00:16.117864128 +0530
Change: 2016-03-10 15:04:24.281533071 +0530

6. Change the modified time '-m'

Use the '-m' option to change the modified time of the file
$ touch -m a.txt 
[term]

Then check the file statistics with the stat command -

[term]
$ stat a.txt 
  File: ‘a.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d      Inode: 5904730     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/enlightened)   Gid: ( 1000/enlightened)
Access: 2016-03-10 15:04:24.281533071 +0530
Modify: 2016-03-10 15:05:03.409475551 +0530
Change: 2016-03-10 15:05:03.409475551 +0530



To change the modify time of multiple files using wildcard
$ touch -m *.txt

7. Change access and modification time together

Use the a and m option together to modify both access and modification time
$ touch -am a.txt
$ stat a.txt 
  File: ‘a.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d      Inode: 5904730     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/enlightened)   Gid: ( 1000/enlightened)
Access: 2016-03-10 15:07:39.633235119 +0530
Modify: 2016-03-10 15:07:39.633235119 +0530
Change: 2016-03-10 15:07:39.633235119 +0530

8. Set a specific access/modify time instead of current time

To set the access/modify time to a specific datetime use the t option and specify the datetime in format
[[CC]YY]MMDDhhmm[.ss]
$ touch -c -t 1603051015 a.txt 

or 

$ touch -c -t 201603051015 a.txt
Note - If you omit the c option, a new file will be created with the given datetime if it does not exist.

9. Use the timestamp of another file as reference

$ touch -r ref.txt abc.txt
The above command will set the access/modify time of abc.txt to that of ref.txt

10. Specify datetime as a string

Apart from the t option, there is another option '-d' which accepts datetime in general human readable formats.
The following example provides the date only. The time is automatically set to 00:00
$ touch -c -d '14 Mar' abc.txt
Or just provide the time, and the current date will be selected -
$ touch -d '14:24' abc.txt
To learn more, check the man page with "man touch".

No comments:

Post a Comment