3

I have two folders A and B, inside that there are two files each. which are created in the below order

mkdir A 
cd A
touch a_1
touch a_2
cd ..
mkdir B
cd B
touch b_1
touch b_2
cd ..

From the above i need to find which folder was created first(not modified).

ls -c  <path_to_root_before_A_and_B> | tail -1

Now this outputs as "A" (no issues here). Now i delete the file a_1 inside the Directory A. Now i again execute the command

ls -c  <path_to_root_before_A_and_B> | tail -1

This time it shows "B".

But the directory A contains the file a_2, but the ls command shows as "B". how to overcome this

Nidheesh V
  • 49
  • 10
  • replaced `,`s with `;`s so readers can cut/paste your setup cmds. Good luck. – shellter Oct 31 '19 at 12:52
  • 1
    Why do you think `-c` should sort by creation time? If you look at the manpage that's not what that option does. Heck, depending on the filesystem, creation time might not even be tracked at all. (And getting at it is difficult even on filesystems that do record it) – Shawn Oct 31 '19 at 13:05
  • just checked tha man page, so -c is ctime (time of last modification of file status information) . So if -c does not sort by creating time, Is there any other method i can use to accomplish this? – Nidheesh V Oct 31 '19 at 13:11
  • If you have a new enough version of gnu `stat(1)` *and* your file system supports it, the `%w` and `%W` formats can get the file creation/birth time. – Shawn Oct 31 '19 at 13:13
  • thanks all, There is no fix for this, so i had make another attempt to find the oldest file. Making folders and files with names as the time stamp and then using the ls -v command to sort the lowest file number name – Nidheesh V Nov 05 '19 at 06:04

2 Answers2

0

How To Get File Creation Date Time In Bash-Debian

You'll want to read the link above for that, files and directories would save the same modification time types, which means directories do not save their creation date. Methods like the ls -i one mentioned earlier may work sometimes, but when I ran it just now it got really old files mixed up with really new files, so I don't think it works exactly how you think it might.

Instead try touching a file immediately after creating a directory, save it as something like .DIRBIRTH and make it hidden. Then when trying to find the order the directories were made, just grep for which .DIRBIRTH has the oldest modification date.

destent
  • 16
  • 2
0

Assuming that all the stars align (You're using a version of GNU stat(1) that supports the file birth time formats, you're using a filesystem that records them, and a linux kernel version new enough to support the statx(2) syscall, this script should print out all immediate subdirectories of the directory passed as its argument sorted by creation time:

#!/bin/sh
rootdir=$1
find "$rootdir" -maxdepth 1 -type d -exec stat -c "%W %n" {} + | tail -n +2 \
 | sort -k1,1n | cut --complement -d' ' -f1
Shawn
  • 28,389
  • 3
  • 10
  • 37