touch

Description

The touch command is a standard command used in the unix/linux operating system which is used to create, change and modify the timestamps of a file.

It is used to create a file without any content.

The syntax for touch command is:

touch [flags] [file_names]

Flags

Some of the flags that are used with touch command are as follows:

  • -a: only to change access time of the file.
  • -c or --no-create: do not create any files.
  • -d or --date=string: parse string and use it instead of current time.
  • -m: change only the modification time.
  • -r or --reference=file: use this file's times instead of current time.
  • -t stamp: use [[cc]yy]mmddhhmm[.ss] instead of current time.

  • --help: get more information about this command.

-a

Description:

It changes the access time of the file and does not change the modification time unless -m is also specified.

Usage:

Consider we want to change access time of a file file1.txt, make sure you are in the correct directory then:

touch -a file1.txt

You can use the stat command to check the updated time:

$ stat file1.txt
  File: file1.txt
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: 10303h/66307d    Inode: 20452679    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/sandeshpyakurel)   Gid: ( 1000/sandeshpyakurel)
Access: 2023-11-12 21:32:00.000000000 +0545
Modify: 2023-11-12 21:32:00.000000000 +0545
Change: 2023-11-11 21:34:49.051967025 +0545
 Birth: 2023-11-11 20:46:42.690779555 +0545

-c

Description:

Using this flag, it does not create the file if it does not already exist. No diagnostic messages are written concerning this condition and it is usually used with other flags.

Usage:

If we want to update the access time of a file only if it exists then:

touch -c -a file1.txt

-d

Description:

The touch command uses the -d option to set a timestamp using a date string. Date string is a flexible time format which accepts many different human-readable textual forms. Some examples are:

  • Calendar dates 11 november 2023.
  • Time of day 9:08pm.
  • Day of the week sunday.
  • Relative time yesterday, next tuesday etc.

Usage:

Let us use this flag to change the access and modify time of the file file1.txt:

touch -d "8 march 2023" file1.txt

Also,

touch -d "yesterday" file1.txt

-m

Description:

This flag is used to change modification time of the file. the -m option changes the modification time to the current timestamp by default.

Usage:

If we want to update the the modification time of a file named file1.txt then:

touch -m file1.txt

-r

Description:

The touch command offers a useful option to change a file's timestamp based on another file's timestamp.

Usage:

Let's update timestamp of file1.txt using file2.txt as reference.

touch -r <reference file> <file>
touch -r file2.txt file1.txt

-t

Description:

Using -t we can explicitly state the timestamp. It is used in combination with other flags. The time is given in yymmddhhmm format.

Usage:

Using following command, timestamp is changed to 2023 nov 12, 9:34pm:

touch -t yymmddhhmm filename
touch -t 2311122134 file1.txt

--help

Description:

With the --help option, the touch command will show flags and options for the command.

Usage:

touch --help

Additional Information

We can use all of the above flags with multiple files.

touch file1.txt file2.txt file3.txt

This command creates these files if they do not exist and update their timestamp.

We can also use regular expression like *.c to modify timestamp of files at once.

touch -m *.cpp

Exercises:

  1. Use touch command to create a new file file.
  2. Use -d flag to update timestamp of file to 2024 dec 10.
  3. Use -r to update timestamp of file using existing file.
  4. Update access time of file using -t to any date.

results matching ""

    No results matching ""