Linux modify file modify time

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)

Источник

How can I change ‘change’ date of file?

You cannot change the ctime by ordinary means. This is by design: the ctime is always updated to the current when you change any of the file’s metadata, and there is no way to impose a different ctime. To change the ctime of a file, you need to do one of the following:

  • Set the system time to the ctime you want to impose, then touch the file, then reset the system time.
  • Modify the kernel to add an interface to change the ctime.
  • Access the disk image directly (e.g. with debugfs ) and twiddle the bits on the disk (don’t do it while the filesystem is mounted).
Читайте также:  Opera on arch linux

You have the answer on related SO question pointed by jw013, for extX, on unmounted disk :

# Update ctime debugfs -w -R 'set_inode_field /tmp/foo ctime 201001010101' /dev/sda1 # Drop vm cache so ctime update is reflected echo 2 > /proc/sys/vm/drop_caches 

$ NOW=$(date) && date -s «2030-08-15 21:30:11» && touch file.txt && date -s «$NOW»

The ctime of a file is updated when any of the metadata is changed.

$ ls -l x.py -rw-rw-r--. 1 ignacio ignacio 485 Mar 26 2010 x.py $ stat -c %z x.py 2010-03-26 11:57:56.237068175 -0400 $ chown ignacio x.py $ stat -c %z x.py 2012-04-08 15:31:33.682383575 -0400 $ ls -l x.py -rw-rw-r--. 1 ignacio ignacio 485 Mar 26 2010 x.py 

evandrix’s answer excerpted in the next line,
NOW=$(date) && date -s «2030-08-15 21:30:11» && touch file.txt && date -s «$NOW»
needs to be improved as described below :

In some systems like mine, date output doesn’t give a valid format to set with date -s

My system bash shell version : GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
My system date command version : date (GNU coreutils) 8.30

My system date command output and setting the date with this format is:

# date Tue 21 Jan 2020 01:36:22 PM +03 # date -s "Tue 21 Jan 2020 01:36:22 PM +03" date: invalid date ‘Tue 21 Jan 2020 01:36:22 PM +03’ 

So it is necessary to improve evandrix answer;
It would be better to assign the NOW variable to the timestamp value
change NOW=$(date) to NOW=$(date +@%s)

 NOW=$(date +@%s) && date -s "2030-08-15 21:30:11" && \ touch file.txt && date -s "$NOW" 

Adding sudo command for non root user

 sudo bash -c 'NOW=$(date +@%s); date -s "2030-08-15 21:30:11"; touch file.txt; date -s "$NOW"' 
 sudo bash -c 'NOW=$(date +@%s); date -s "$2"; touch "$1"; date -s "$NOW"' -- \ "file.txt" "2030-08-15 21:30:11" 

In this way for easy use, the filename and setting date are assigned as arguments at the end of the line.

Источник

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.
Читайте также:  What is source directory linux

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.

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:

Читайте также:  Linux how to check ram usage

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: —

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

Источник

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