-1

Here is my created file using linux command.

touch tmp.txt

Now i tried to replace it date and time so what i did is as follows:

ls -l tmp.txt
-rwrx-rx-x 1 yush guest 600 jul 10 16:58 tmp.txt

I had used the following command to replace year for the above cretaed file?

touch -d "1 year ago" tmp.txt

Now,

ls -l tmp.txt 
-rwxr-xr-x 1 yush guest 600 jul 10 2016 tmp.txt

My query is there any other command to do the above things using linux?

1

2 Answers 2

2

Explicitly Set the Access and Modification time:

# touch -t [[CC]YY]MMDDhhmm[.SS]

e.g.

# touch -t 201212101830.55 tmp.txt
# touch -d "2012-10-19 12:12:12.000000000 +0530" tmp.txt
# touch -d "2017-10-19 14:59:12" tmp.txt

You can use the time stamp of another file:

# touch -r file1.txt file2.txt
2
  • I think he was asking for a different command, not alternate invocations of touch.
    – Bob Eager
    Jul 10, 2017 at 11:49
  • 1
    @BobEager, perhaps, but I think this answers the only useful interpretation of the question.
    – Wildcard
    Jul 11, 2017 at 23:55
0

You can use archiving programs to do this (although not cleanly or easily).  Note that the metadata (permissions mode, owner, group, modification date/time, etc.) of actual files (in the filesystem) are stored in the inode (i.e., “system data”), but the metadata of archived file(s) are stored as user data in the archive file.  These can usually be manipulated, with various degrees of difficulty.

I’ll give an example with cpio; similar effects can probably be achieved with other archiving programs, e.g., ar, tar, pax, the zip family, etc.  First, create your tmp.txt file with a known modification date/time (for reproducibility); then create a cpio archive containing only that file.  cpio options include o for output (it means that cpio will be writing an archive) and c to use a portable (compatible) format, which may be easier to manipulate.

$ touch -d "7/12/2017 00:00" tmp.txt

$ ls -l tmp.txt
-rw-r--r-- 1 myusername mygroupname  0 Jul 12 00:00 tmp.txt

$ echo tmp.txt | cpio -oc > tmp.cpio
1 block

Check the archive.  cpio -itv means read an archive (input) and produce a verbose table of contents.

$ cpio -itv < tmp.cpio
-rw-r--r--   1 myusername  mygroupname  0 Jul 12 00:00 tmp.txt
1 block

$ cat tmp.cpio
070701000595CC000081A4000303E900030201000000015965668000000000000068E70000C36100
000000000000000000000800000000tmp.txt0707010000000000000000000000000000000000000
0010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!!

I’ve discovered through reverse engineering (it may be documented somewhere; I didn’t check) that the modification date/time of the first file in the archive is stored as a Unix time (a 32-bit integer representing the number of seconds since 1/1/1970 00:00 GMT), formatted as an eight-digit hex number at character positions 47-54.  I have highlighted the value, 59656680, above.  Once you know that it’s a Unix time formatted as an eight-digit hex number, it’s not hard to find.  You can get the current Unix time (in decimal) with the command date +%s, and you can get it as hex by saying

(date +%s; echo "16op") | dc

Now, to modify the metadata!  Edit the archive with your choice of editor:

$ vi tmp.cpio
0707010005807C000081A4000303E900030201000000015965668000000000000068E70000C36100
000000000000000000000800000000tmp.txt^@^@^@0707010000000000000000000000000000000
0000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER
!!!^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
~
~
~
~
~
"tmp.cpio" [Incomplete last line] 1 line, 512 characters

Figure out what you want to change it to.  In your question, you mentioned rolling the clock back a year.  Well, the number of seconds in a normal, 365-day year is 365×24×60×60 = 31536000 (decimal) = 1E13380 (hex).  So subtract  59656680−1E13380; the result is 57843300.  And change it in the file:

0707010005807C000081A4000303E900030201000000015784330000000000000068E70000C36100
000000000000000000000800000000tmp.txt^@^@^@0707010000000000000000000000000000000
0000000010000000000000000000000000000000000000000000000000000000B00000000TRAILER
!!!^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
~
~
~
~
~
"tmp.cpio" [Incomplete last line] 1 line, 512 characters

Save and exit.  Check your changes:

$ cat tmp.cpio
070701000595CC000081A4000303E900030201000000015784330000000000000068E70000C36100
000000000000000000000800000000tmp.txt0707010000000000000000000000000000000000000
0010000000000000000000000000000000000000000000000000000000B00000000TRAILER!!!

$ cpio -itv < tmp.cpio
-rw-r--r--   1 myusername  mygroupname  0 Jul 12  2016 tmp.txt
1 block

$ cpio -imuv < tmp.cpio
tmp.txt
1 block

$ ls -l tmp.txt
-rw-r--r-- 1 myusername mygroupname  0 Jul 12  2016 tmp.txt

and lo and behold: you have changed your file’s modification date/time.  (cpio -i without t reads the archive and extracts files; -m restores the modification time; -u means unconditional (without this option, cpio won’t overwrite a newer file with an older file); and, as usual, -v means verbose (display file names).)

Notes:

  • You can get the Unix date (number of seconds blah blah blah) for a human-readable date with a command like date -d "Jan 1, 2001" +%s.  Use the dc (desk calculator) trick I showed earlier to convert it to hex.
  • The cpio archive format includes null bytes.  The current version of vi (vim) seems to respect those (i.e., tolerate them and leave them alone).  Other editors, including older versions of vi, might corrupt such files (by deleting the nulls), or even refuse to edit them.
  • vi detects that the cpio archive ends with a character other than a newline (“[Incomplete last line]”).  When it saves the file, it adds a newline.  Other archiving programs (other than cpio) might detect this as tampering and might refuse to honor the modified archive.
  • Other archive formats might store metadata (such as modification date/time) in binary, and/or have checksums.  Editing such files might be more difficult than what I described here.
1
  • Fun fact: Unix time 1500000000 (1.5×10⁹) will be July 13, 2017 at 22:40:00Z (GMT) — a little over a day from now. Jul 12, 2017 at 18:20

Not the answer you're looking for? Browse other questions tagged .