Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving a file/directory the same modification date as another

Tags:

bash

unix

How do I "copy" the modification date and time from one file/dir to another in Unix-based systems?

like image 999
user2015453 Avatar asked Mar 03 '13 00:03

user2015453


People also ask

Does copying a file change the modified date?

If you copy a file from C:\fat16 to D:\NTFS, it keeps the same modified date and time but changes the created date and time to the current date and time. If you move a file from C:\fat16 to D:\NTFS, it keeps the same modified date and time and keeps the same created date and time.

What is the difference between change time and modification time of a file?

Modification Time: is the time when the contents of the file was last modified. For example, you used an editor to add new content or delete some existing content. Change Time: is the time when the file's inode has been changed. For example, by changing permissions, ownership, file name, number of hard links.

How do I change a timestamp on a directory in Linux?

Setting specific timestampsUse the -d ( --date= ) option to specify a date string and use it instead of the current time. The date string needs to be enclosed in single quotes. For example, the following command will set the last access and modification times of file1 to 1 June 11:02 of the current year.

How do I move a file to a specific date in Linux?

* -type f -exec bash -c 'mv "$@" $(date --date=@$(stat -c %Y "$@") +%Y-%m)/"$@"' _ {} \; it moves all files in followingly e.g. zoo. txt goes to 2020-01/zoo. txt etc.


2 Answers

You have some options:

  • Use touch -t STAMP -m file if you want to change the time
  • Use cp --preserve=timestamps if you're copying the files and want to preserve the time
  • Use touch -r to set the time to a "reference" file
like image 159
cnicutar Avatar answered Nov 03 '22 08:11

cnicutar


For convenience later on, put the following line in your .bashrc file:

cptimestamp() {
  if [ -z $2 ] ; then
    echo "usage: cptimestamp <sourcefile> <destfile>"
    exit
  fi
  touch -d @$(stat -c "%Y" "$1") "$2"
}

Execute "source ~/.bashrc" and you're ready to go. If you prefer a script instead, remove the first and last lines -- then prepend "#!/bin/sh"

like image 30
Frotz Avatar answered Nov 03 '22 09:11

Frotz