0

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).

Norbert Willhelm
  • 2,397
  • 1
  • 16
  • 27

2 Answers2

1

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

Community
  • 1
  • 1
Xen2050
  • 2,144
  • 18
  • 17
0

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