3

In my Downloads folder I have a lot of files. I need to arrange them according to the following criteria:

  • they shall be arranged by type;
  • files of the same type shall be listed one after the other by creation date, or alternatively according to the date I put them to my Downloads folder.

Please take a look: Find the latest file by modified date

Example: in my Downloads directory I have *.tar.gz, *.pdf, *.zip files. When I go to my Downloads directory I want to see my *.tar.gz files arranged together and sorted by the time I added them to this directory. I expect that the fifth *.tar.gz file is the fifth youngest in terms of birth time.

Mohammad Reza Rezwani
  • 10,286
  • 36
  • 92
  • 128

1 Answers1

3

My previous answer was began with this wrong statement:

Linux does not store file creation time

Yes - Linux kernel does not provide API to get creation time stamps from file system.

But as it is written in wiki ext4 provide file creation timestamps:

ext4 also adds support for date-created timestamps. But, as Theodore Ts'o points out, while it is easy to add an extra creation-date field in the inode (thus technically enabling support for date-created timestamps in ext4), it is more difficult to modify or add the necessary system calls, like stat() (which would probably require a new version) and the various libraries that depend on them (like glibc).

Also as answered in unix.stackexchange

Several file systems store the file creation time, 
although there is no standard name for this field:

   - ufs2  → st_birthtime
   - zfs   → crtime
   - ext4  → crtime
   - btrfs → otime
   - jfs   → di_otime

Here is great question/answer how to find file creation stamp. Script written by @terdon:

get_crtime() {

    for target in "${@}"; do
        inode=$(ls -di "${target}" | cut -d ' ' -f 1)
        fs=$(df  --output=source "${target}"  | tail -1)
        crtime=$(sudo debugfs -R 'stat <'"${inode}"'>' "${fs}" 2>/dev/null | 
        grep -oP 'crtime.*--\s*\K.*')
        printf "%s\t%s\n" "${target}" "${crtime}"
    done
}

Add this script to your .bashrc.

Now using this script you can get what you want.

Go to folder which content you want to sort

$ cd ~/Downloads
$ get_crtime * | sed -r 's/\s+/ /g' | cut -d" " -f1,3,4,6 | sort -k2M -k3,4n

For tar.gz files

$ get_crtime *.tar.gz | sed -r 's/\s+/ /g' | cut -d" " -f1,3,4,6 | sort -k2M -k3,4n

Sources

c0rp
  • 9,820
  • 3
  • 38
  • 60