Showing posts with label Linux touch. Show all posts
Showing posts with label Linux touch. Show all posts

Tuesday, 6 August 2019

Practical examples of Touch command on Linux

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

Friday, 2 August 2019

Linux - Touch Command

Touch command is one of the basic commands in Linux/UNIX flavor machines for working with files. 

Touch command practical usages:

To create empty files in a directory.
To update the timestamp of the file without changing the content.

Creation of empty file:
Syntax:
touch file-name1

Command example output:
[admin@linbox1 ~]$ touch file1

[admin@linbox1 ~]$ ls -l
total 0
-rw-rw-r--. 1 admin admin  0 Jul 25 15:23 file1

If you observe that created file size is “0”. i.e, nothing is there in the file.
[admin@linbox1 ~]$ cat file1

Updating the modified time of the file:

If the file is already created then with touch command, its modified time is updated.

Example:
[root@linbox1 admin]# ls -l file1
-rw-rw-r--. 1 admin admin 12 Jul 25 15:28 file1

[root@linbox1 admin]# touch file1

[root@linbox1 admin]# ls -l file1
-rw-rw-r--. 1 admin admin 12 Jul 25 16:55 file1

Practical usage,
Sometimes, an automated script missed a file to consider, then we want to modify the file timestamp for getting picked up by script.

Creating multiple empty files with touch command:
With touch command, we can create multiple files at a time in directory.
Why we need multiple empty files? Any business case here?
Practical usage example…

Syntax:
touch file-name1 file-name2

 Command  Example Output:
[admin@linbox1 ~]$ touch abcfile1 abcfile2

Listing the files:
[admin@linbox1 ~]$ ls -l abc*
-rw-r--r--. 1 root root 0 Jul 25 17:13 abcfile1
-rw-r--r--. 1 root root 0 Jul 25 17:13 abcfile2