Bash directory loop and order by creation date?

Hello, how in bash i can get directory loop and order by creation date?

THX! :slight_smile:

#!/bin/bash
for folder in /home/test/*
do
if [ -d "${folder}" ]; then
echo $folder;
fi

If you have gnu ls

$ ls --version
ls (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.

then this:

ls --time=ctime -d */

so, --time=ctime to sort by creation time; -d to not list the contents of directories; */ expands to the list of directories in the current directory, forcing ls to only list directories.

Assuming --time=ctime works on your filesystem.

Andrew

1 Like

Hi.

Excerpt from stat (system call) - Wikipedia

Excerpt from: MAC times - Wikipedia

Best wishes ... cheers, drl

Thats all great but how to implement it into this bash script??

#!/bin/bash 
for folder in /home/test/* 
do 
if [ -d "${folder}" ]; then 
echo $folder; 
fi

How about, respecting the caveats by drl above:

ls -tcrd */ | while read DIRN; do echo "$DIRN"; done
1 Like

If you want to ignore everything you've been told and do not want to print directory names in order of date of creation and instead want to print directory names in order of increasing file status change times, you can use:

ls -1cd /home/test/*/

(note that the 1st option in the above command is the digit 1; not the lowercase letter l). Using the -1 option tells ls to produce names one per line without needing to feed the output through a while read echo loop.

If your directories are located on a filesystem type that does save creation times in addition to file status change times, on some systems you could get a list of directories in increasing order of creation times with:

ls -1Ud /home/test/*/

but since standard filesystems don't have a creation time (only a status change time as drl mentioned), the option letter to print the creation time (if there is one) may be different on your system. Search the ls man page on your system for an option containing the word creation to see if there is an option to sort output by creation time on your system, and if there is, what option character invokes it and what filesystem types have that information.

1 Like

i really need to implement it into the code, also it should sort it by make/create folder date, newest on top, not by updated folders

#!/bin/bash 
for folder in /home/test/* 
do 
if [ -d "${folder}" ]; then 
echo $folder; 
fi

I'm afraid that stubbornly repeating your question as well as that code snippet, ignoring any of the proposals given in this thread, won't take you anywhere.
Why don't you take a step back and start over from the beginning, specify your needs - in detail! - , your data (structures), and the desired outcome, describe what you have (obviously that for loop) and its results, what and where it does NOT satisfy your needs, show the results of the proposals given and their shortcomings, and eventually errors occurring? That MIGHT put us in a position to help you.

1 Like

sorry :slight_smile:

i tested all above i am not ignoring the posts above, i thanked for all the posts as i am tried it all, but none worked the way, i want, sort it by latest created dirs by the loop script

that loop i have now only show all dirs in /home/test/, those are NOT sorted by created date in any way, probably just by name...

simply i need in that loop sort dirs by created date/time ...

Example (i do not care the created thing date "Created 2016-*-*", just sort order by create folder) in folder /home/test/:

Folder_test_1 - Created 2016-01-01
Folder_test_2 - Created 2016-05-01
Folder_test_3 - Created 2016-09-01

Output should be 

Folder_test_3 - Created 2016-09-01
Folder_test_2 - Created 2016-05-01
Folder_test_1 - Created 2016-01-01

In what way did they not work? "Does not work" is manifestly unhelpful. They might be one minor correction away from working for all we know - rejecting them out of hand without even bothering to explain why is a huge waste of our time and effort.

  • Show what you tried, word for word, letter for letter, keystroke for keystroke.
  • Show what happened. Do not paraphrase. Show the real result.
  • Show how the result is different from what you wanted.

Either work with the people trying to help you, or I will close this thread.

1 Like

i just said none worked THE WAY i wanted, i did not said "it does not work" :slight_smile:

so all being said, all post above this just do not reproduce following output -> SORT BY DATE CREATION what more to say?

The posts above just showed the way its possible to sort via update folder date&time, thats the one i do not need, i need SORT BY DATE CREATION

And output of the commands from all above replies was just folders sorted by update date&time

Yes, you are ignoring everything that has been said.

First, you can't get output file directory creation order unless the filesystem you are using stores that information. You haven't told us what operating system and filesystem type you're using; so our first guess is that what you want CANNOT be done on your system.

Second you never said anything about sorting in reverse order and until post #9 you never gave any indication that sorting by creation date was not sorting by increasing creation date (instead of decreasing creation date).

Third, if you are using a filesystem type that keeps creation date data, I told you exactly how to find out how print directories in increasing creation date order on BSD systems in post #6 and told you where to look for the option to use on your system. Did you bother looking at the ls man page on your system to see if your system provides an option to sort by file creation time? If so, what filesystem types on your system provide file creation time data? Is the filesystem where the directories you want to process one of those filesystem types? If the answer to any of these questions is no, there is nothing we can do to help you unless you move your files to a filesystem type that keeps track of file creation dates and you mount that filesystem on a system with an ls utility that provides a way to sort its output based on file creation dates.

Fourth, the code that you keep showing us is code that prints directories in increasing alphanumeric order. None of the examples we have suggested print directories in alphabetic order unless the file status change time or file creation time happens to be the same order as alphabetic order. To get output from ls in reverse sorted order add the -r option to any of the ls commands we suggested.

1 Like

Many of them clearly and obviously do. I see literally no indication you've actually tried them. Which isn't to say you haven't, but if you have and didn't post what you did and its results, you've wasted your own time.

Complaining is useless. If you show what you try and exactly the results you get, maybe we can figure out why. (Do those folders actually have ctimes like you think, or are they just NAMED those dates?)

As is, we have nothing to work from, and neither do you. Last chance before thread closure.

1 Like