How can I retrieve the sub-directory, which had most recently been modified, in a directory? I am using a shell script on a Linux distribution (Ubuntu).
2 Answers
Sounds like you want the ls options
-t sort by modification time, newest first
And only show directories, use something like this answer suggests Listing only directories using ls in bash: An examination
ls -d */
And if you want each directory listed on one line (if your file/dirnames have no newlines or crazy characters) I'd add -1
So all together, this should list directories in the current directory, with the newest modified times at the top
ls -1td */
And only the single newest directory:
ls -1td */ | head -n 1
Or if you want to compare to a specific time you can use find
and it's options like -cmin -cnewer -ctime -mmin -mtime
and find can handle crazy names like newlines, spaces, etc with null terminated names options like -print0
How much the subdirectory is modified is irrelevant. Do you know the name of the subdirectory? Get its content like this:
files=$(ls subdir-name)
for file in ${files}; do
echo "I see there is a file named ${file}"
done

- 706
- 4
- 5
-
For me it isn't irrelevant. – Norbert Willhelm May 01 '14 at 08:01