2

Is it possible to create a txt, that lists the folders in a directory and the date of creation (or ctime) of that folder?

Something like:

Folders.txt:

Folder1 2016/08/02

FolderA 2016/08/03

This is what I have right now, but it doesn't put the date of creation:

find /directory/* -type d -exec bash -c 'echo ${0#/directory/}' {} \; > /directory/folders.txt
1
  • Do you have GNU find? if so, you can do stuff like -printf '%P %CD\n' Aug 3, 2016 at 12:15

2 Answers 2

3

Note that the ctime is the inode change status time, not the creation time. The creation time aka inode birth time, sometimes refered to as btime is not available on all Unices or all file systems.

For instance, on Linux, the birth time is recorded on most native file systems, but there is currently no API to retrieve it.

In any case, that is the last time the inode came to life. That doesn't reflect the time the data was put in there (which generally happens after the inode was created), nor necessarily the time the file was linked to a directory (by the name you're currently refering to it as) as that birth time is not updated when the file is renamed or linked to another directory (or the same as a different name).

Best would is generally to use the last modification time. That better represents the age of the data in the file.

With GNU find, you can use:

find /directory -mindepth 1 -maxdepth 1 -type d -printf '%P %TY/%Tm/%Td\n'

With zsh:

zmodload zsh/stat
(cd /directory && zstat -LF '%Y/%m/%d' +mtime -- *(D/))

(gives you a sorted list compared to find. You can disable sorting with the oN glob qualifier, or sort by modification time with om)

1
  • I used that, but instead of %P , I used %f (to print only the name of the folders). Do you know if it is possible to sort the list by the date? Instead of filenames? ---> Edit: I found, used ' |sort '
    – blocnt
    Aug 3, 2016 at 14:24
3

Normally ls -lc would should this. But that shows files as well as directories.

So we can cheat, and ask it for */ entries; only directories will match that. But we then also need the -d flag:

ls -lcd */

If you want a specific layout then ls isn't necessary the best output.

We could use stat, if you have it present:

stat --format="%n %z" */

Both of those will follow symlinks, if a symlink points to a directory.

If you don't want that then we can use find

find * -maxdepth 0 -type d -printf "%p %c\n"

If you want it recursive, then

find * -type d -printf "%p %c\n"
1
  • It assumes none of the file names start with - (or are otherwise find predicates with find). That doesn't include hidden dirs. The ls and stat ones include symlinks to directories. The stat ones assumes the GNU implementation of stat (other stat implementations have a different interface). The find ones assume GNU find. Aug 3, 2016 at 14:10

You must log in to answer this question.

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