Linux change modification time file

5 Linux Touch Command Examples (How to Change File Timestamp)

Every file in Linux is associated with timestamps, which specifies the last access time, last modification time and last change time. Whenever we create a new file, or modify an existing file or its attributes, these timestamps will be updated automatically. Touch command is used to change these timestamps (access time, modification time, and change time of a file).

1. Create an Empty File using touch

You can create an empty file using touch command. The following example will create a zero byte new file named tgs.txt.

You can also use -c option to avoid creating new files. If you use -c option, and if a file doesn’t exists, touch will not create the file.

Commands like ls command and find command uses these timestamp information for listing and finding files. You can also create more than 1 files from a single touch command. The following example will create 4 files named a, b, c, and d.

2. Change File’s Access Time using -a

We can change the access time of a file using -a option. By default it will take the current system time and update the atime field. Before touch command is executed:

$ stat tgs.txt File: `tgs.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 394283 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan) Access: 2012-10-18 23:58:21.663514407 +0530 Modify: 2012-10-18 23:58:21.663514407 +0530 Change: 2012-10-18 23:58:21.663514407 +0530
$ stat tgs.txt File: `tgs.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 394283 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan) Access: 2012-10-19 00:08:23.559514525 +0530 Modify: 2012-10-18 23:58:21.663514407 +0530 Change: 2012-10-19 00:08:23.559514525 +0530

3. Change File’s Modification Time using -m

The above method can be used to change the mtime of all obj files, when using make utility. NOTE: It is not possible to change the ctime using touch command

4. Explicitly Setting Access and Modification time using -t and -d

Instead of taking the current time-stamp, you can explicitly specify the time using -t and -d options. The format for specifying -t is [[CC]YY]MMDDhhmm[.SS]

  • CC – Specifies the first two digits of the year
  • YY – Specifies the 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 – Specifies the month
  • DD – Specifies the date
  • hh – Specifies the hour
  • mm – Specifies the minute
  • SS – Specifies the seconds
$ touch -a -m -t 203801181205.09 tgs.txt

Verify the above change using stat command:

$ stat tgs.txt File: `tgs.txt' Size: 3 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 394283 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan) Access: 2038-01-18 12:05:09.000000000 +0530 Modify: 2038-01-18 12:05:09.000000000 +0530 Change: 2012-10-19 00:40:58.763514502 +0530

You can also use a string to change the time

$ touch -d "2012-10-19 12:12:12.000000000 +0530" tgs.txt

For developers, touch command will be really helpful when you are working with Makefiles

Читайте также:  How to resize linux partitions gparted

5. Copy the Time-stamp from Another File using -r

You can also take a file as a reference, and update the time for other files, so that both file will hold the same time.

The following touch command example will update the time-stamp of file a.txt with the time-stamp of tgs.txt file.

Источник

Linux Touch Command – Tutorial & Examples !

touch command - full tutorial how to modify access time, modified time and created time on a file in linux

The touch command is a Linux command-line tool that can be used to create an empty file and modify the timestamps of files.

Timestamps stores the information of each file and folder including, access time, modify time and change time.

Whenever you create or change an existing file, the timestamps value will be updated automatically.

There are three types of timestamps :

  • Access time
    The last time the file was accessed by some application.
  • Modification time
    The last time the file contents were modified.
  • Changed time
    The last time the file’s metadata, attribute or content was changed.

In this tutorial, we will learn how to use Touch command with practical examples.

Basic Syntax for Linux Touch Command

The basic syntax of Touch command is shown below:

A brief explanation of each option is specified below:

  • -a
    Used to change only the access time.
  • -m
    Used to change only the modification time.
  • -d
    Used to update the access and modification times.
  • -c
    Do not create a new file, if the does not exist.
  • -r
    Used to use the access and modification times of another file.
  • -t
    Used to create a file with a specified time.

Create a Single File with Touch

You can create a single empty file by running the following command:

You can now verify your file with the following command:

You should see the following output:

-rw-rw-r— 1 vyom vyom 0 Mar 29 10:44 file1

you can avoid creating new files if a file doesn’t exists, use the touch command with -c option as shown below:
touch -c file1

Once you are finished, you can proceed to the next example.

Create a Multiple Files with Touch

To create multiple files named file2, file3, file4 and file5, run the following command:

touch file2 file3 file4 file5

You can now verify all the files with the following command:

You should see the following output:

-rw-rw-r— 1 vyom vyom 0 Mar 29 10:44 file1
-rw-rw-r— 1 vyom vyom 0 Mar 29 10:49 file2
-rw-rw-r— 1 vyom vyom 0 Mar 29 10:49 file3
-rw-rw-r— 1 vyom vyom 0 Mar 29 10:49 file4
-rw-rw-r— 1 vyom vyom 0 Mar 29 10:49 file5

Change Access Time of File with Touch

If you want to change/update the access time of an existing file, you can use -a option file touch command.

Читайте также:  Linux stop all jobs

For example, to change the access time of a file named file1 to the current time, run the following command:

Next, verify whether the access time of a file has been changed or not using the following command:

You should see the following output:

File: ‘file1’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467544 Links: 1
Access: (0664/-rw-rw-r—) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-03-29 10:50:14.503381528 +0530
Modify: 2020-03-29 10:44:37.155374736 +0530
Change: 2020-03-29 10:50:14.503381528 +0530
Birth: —

Change Modification Time of File with Touch

You can change the modification time of a file using -m option.

For example, to change the modification time of a file named file2, run the following command:

Next, verify the modification time of a file with the following command:

You should see the following output:

File: ‘file2’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467546 Links: 1
Access: (0664/-rw-rw-r—) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-03-29 10:49:18.007380390 +0530
Modify: 2020-03-29 10:54:23.735386545 +0530
Change: 2020-03-29 10:54:23.735386545 +0530
Birth: —

Change Access and Modification Time of File with Touch

You can change both access and modification time of a file using the option -a and -m together.

For example, change the access and modification time of a file named file3 to the current time using the following command:

You can now verify the access and modification time of a file with the following command:

You should see the following output:

File: ‘file3’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467615 Links: 1
Access: (0664/-rw-rw-r—) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-03-29 10:58:23.347391370 +0530
Modify: 2020-03-29 10:58:23.347391370 +0530
Change: 2020-03-29 10:58:23.347391370 +0530
Birth: —

Set Specific Access and Modification Time of a File with Touch

You can also set specific access and modification time of a file using the following syntax:

touch -c -t YYYYMMDDhhmm FILENAME

  • YYYY : Specify the four digits of the year.
  • MM : Specify the month of the year.
  • DD : Specify the day of the month.
  • hh : specify the hour of the day.
  • mm : Specify the minutes of the hour.

For example, set the access and modification time of a file named file4 to 11:25 AM Jan 10 of the year 2020, run the following command:

touch -c -t 202001101125 file4

You can verify the access and modification time of a file with the following command:

You should see the following output:

File: ‘file4’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467620 Links: 1
Access: (0664/-rw-rw-r—) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-01-10 11:25:00.000000000 +0530
Modify: 2020-01-10 11:25:00.000000000 +0530
Change: 2020-03-29 11:06:33.535401239 +0530
Birth: —

You can also set specific date and time in human readable format by using the -d option.

For example, set the date of a file named file4 to 15 Feb run the following command:

This command will set the date of a file4 to 15 Feb and the time is automatically set to 00:00.

You can verify it with the following command:

You should see the following output:

File: ‘file4’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467620 Links: 1
Access: (0664/-rw-rw-r—) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-02-15 00:00:00.000000000 +0530
Modify: 2020-02-15 00:00:00.000000000 +0530
Change: 2020-03-29 11:27:00.983425951 +0530
Birth: —

Читайте также:  Restarting linux from terminal

Set the Timestamp of Another File

You can also take a timestamp from another file and use it to set the time for other files using the -r option.

For example, set the timestamp of a file named file5 using the timestamp value of file1 using the following command:

You can now verify the timestamp value of file5 with the following command:

You should see the following output:

File: ‘file5’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 15467630 Links: 1
Access: (0664/-rw-rw-r—) Uid: ( 1000/ vyom) Gid: ( 1000/ vyom)
Access: 2020-03-29 10:50:14.503381528 +0530
Modify: 2020-03-29 10:44:37.155374736 +0530
Change: 2020-03-29 11:17:05.391413960 +0530
Birth: —

Conclusion

In the above guide, we’ve learned How to Use the Touch Command with several examples, including how to change Access, Modified and Created times.

We hope you can now use the touch command to change the access and modification time of file and create files as well.

Feel free to ask any questions below and post your comments!

Recent Posts

  • Forcepoint Next-Gen Firewall Review & Alternatives
  • 7 Best JMX Monitoring Tools
  • Best PostgreSQL Backup Tools
  • Best CSPM Tools
  • Best Cloud Workload Security Platforms
  • Best Automated Browser Testing Tools
  • Event Log Forwarding Guide
  • Best LogMeIn Alternatives
  • Citrix ShareFile Alternatives
  • SQL Server Security Basics
  • Cloud Security Posture Management Guide
  • Cloud Workload Security Guide
  • The Best JBoss Monitoring Tools
  • ITL Guide And Tools
  • 10 Best Enterprise Password Management Solutions

Источник

Linux Change Modification date of files?

I extract .php files in one of my directory and there were many files in there so most of the files replaced ! but I have a problem since the modification date of new files are 23 April 2013 so I want to find all files and folders in this directory that are not 23 April 2013 ! In other way, I want to change all files in this directory that have 23 April 2013 modification date to 30/08/2013 ! How its possible to find and change the files ? Combine FIND and TOUCH function to replace all files modification date.

1 Answer 1

You could cd to the folder containing the PHP files and:

touch -d '30 August 2013' *.php 

Or if it has sub folders with php files — search through them recursively:

find /path/to/your/php/ -exec touch -d '30 August 2013' *.php <> \; 

the folder ‘php’ in the command above would be included.

If you ONLY need to find/change EXACTLY files modified on 23 April 2013, you can use the -mtime parameter in your find command.

  • -mtime +60 means you are looking for a file modified 60 days ago or more.
  • -mtime -60 means less than 60 days.
  • -mtime 60 If you skip + or — it means exactly 60 days.

So modifying the command above like this:

find /path/to/your/php/ -mtime 127 -exec touch -d '30 August 2013' *.php <> \; 

Where 127 is the exact amount of days since 23 April (if my quick head calculation is correct). Else you can change the number to the correct amount of days, or use the + or — as described above if it doesn’t need to be ‘that’ exact.

You can read more about the find commands -mtime parameter here: http://www.cyberciti.biz/faq/howto-finding-files-by-date/

(yes I borrowed 3 lines from there)

Источник

Оцените статью
Adblock
detector