Skip to main content

Practical examples of Touch command on Linux

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".

Comments

Popular posts from this blog

Files creation

In LINUX files can be created by using any of the three methods given below:        1.  Cat command (cat - CONCATENATION)        2. Touch command        3. Vi editor 1. cat command: cat stands for CONCATENATE. This is the basic command when we start learning LINUX/UNIX, as the name suggest it is used to create new file, concatenate files and display the content of the files on the standard output.     Different examples of cat command which will be useful for the beginners. 1. Creating new file 2. View the contents of the file 3. View the contents of  multiple files 4. Display the output of a file using page wise 5. cat command without filename arguments 6. Display the content of a file with line numbers 7. Copy the contents of a one file to another file 8. Appending the contents of one file to another file 9. Redirecting the output of multiple files into a single fi...

grep Word Count Command

How do I count words using grep command under Linux / Unix like operating systems? You can pass the -c option to grep command to suppress normal output and display a count of matching lines for each input file. The syntax is follows: grep -c "string" file In this example, search for a word called ‘var’ and display a count of matching lines: grep -c 'var' /etc/passwd You can pass the -v option to count non-matching lines: grep -v 'var' /etc/passwd However, this will not count words. To count exact matched words, enter: grep -o -w 'word' /path/to/file/ | wc -w The grep -o command will only display matched words and the wc -c command will display the word counts.