You are not logged in.
I'm working on a script that processes files under a specified root directory. The tree command is ideal for this.
The specific command I have ended up with is-
tree -afugpiQDt --timefmt='%s' /dirpathWhich produces output like-
[drwxrwsr-- username admin 1743552894] "/dirpath"
[drwxrwsr-x username admin 1740576183] "/dirpath/archive"
[drwxrwsr-x username admin 1739765107] "/dirpath/archive/Admin"
[-rw-rw-r-- username admin 1288133839] "/dirpath/archive/Admin/Serial Numbers Games.txt"
[-rw-rw-r-- username admin 1292952813] "/dirpath/archive/Admin/Serial Numbers Apps.txt"
[-rw-rw-r-- username admin 1345070773] "/dirpath/archive/Admin/Thumbs.db"
[-rw-rw-r-- username admin 1576249716] "/dirpath/archive/Admin/Serial Numbers.xlsx"
[-rw-rw-r-- username admin 1576671892] "/dirpath/archive/Admin/Software List.ods"
[-rw-rw-r-- username admin 1576703734] "/dirpath/archive/Admin/Software List.xlsx"
[-rw-rw-r-- username admin 1660600694] "/dirpath/archive/Admin/Serial Numbers.ods"
[-rw-rw-r-- username admin 1661344145] "/dirpath/archive/Admin/Serial Numbers OS.csv"
[-rw-rw-r-- username admin 1661344258] "/dirpath/archive/Admin/Serial Numbers OS.txt"
[drwxrwsr-x username admin 1739765059] "/dirpath/archive/Admin/Applications"
[-rw-rw-r-- username admin 1050511481] "/dirpath/archive/Admin/Applications/Xara X.html"
[-rw-rw-r-- username admin 1058037173] "/dirpath/archive/Admin/Applications/Xara_Gallery_CD.reg"
[-rw-rw-r-- username admin 1058044314] "/dirpath/archive/Admin/Applications/Xara_Gallery_Disk.reg"
[-rw-rw-r-- username admin 1081720124] "/dirpath/archive/Admin/Applications/Xara User Defaults.reg"
[-rw-rw-r-- username admin 1120521729] "/dirpath/archive/Admin/Applications/XaraX Precision.txt"
[-rw-rw-r-- username admin 1201454348] "/dirpath/archive/Admin/Applications/Xara_Gallery_Merge.reg"
[-rw-rw-r-- username admin 1296488453] "/dirpath/archive/Admin/Applications/Xara enable thumbnails Win7 64.TXT"What's confusing me with this is-
The -t option specifies sort by file modified time.
The -timefmt specifies printing the file modified time as a unix timestamp.
But the output of tree is NOT a list of paths with either monotonically increasing or monotonically decreasing modified timestamps(?) Instead the list still appears to be sorted by the full path string.
Note: I tried adding the -U option, which is supposed to suppress the default sorting, but this makes no difference.
I was hoping to generate a file list in "last modified time" order (ideally, descending), so I can process the files one at a time and when I hit a cutoff time of (say) one hour ago, stop processing knowing I have dealt with all the recently changed files.
Have I misunderstood how the options (especially -t) of the tree command are supposed to work?
Last edited by incans (2025-04-05 19:53:36)
Offline
bash 101: Do not parse the output of ls. Same goes for tree.
You want to use find which can filter by timestamps and pass the match to some exec.
Offline
bash 101: Do not parse the output of ls. Same goes for tree.
You want to use find which can filter by timestamps and pass the match to some exec.
I'm not using bash.
I am aware I could use find. However I want to have access to the full file list (partly so I have the full count for reporting purposes), so I don't want to pre-filter the list with the "since time X" rule. Also, I have some include and exclude rules to apply to the file set, which results in either a horribly complex find command line and/or multiple find passes over the same file tree. By comparison, having a flat file list and then applying the complex rules in a loop over that list is easier to both write and test.
What I want, at this point, is an answer to the original question. Positive advice is always welcome, "don't start from here" is rather less helpful.
Offline
Look at lines 4&5 of your output. How are they sorted?
tree still produces a hierarchical structure ("tree") and sorting happens within that.
You could pipe it through sort or use ls -R except all of that is categorically wrong no matter the shell.
Use find.
Offline
Look at lines 4&5 of your output. How are they sorted?
tree still produces a hierarchical structure ("tree") and sorting happens within that.
...
Use find.
Right. So tree always emits a valid tree structure, and then applies sort rules to the peer-level items within a directory. Ok, that makes sense, and is useful information. Thank you.
Offline
Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.
Offline