I have an application which generates a couple of files in different directories at a regular interval and I need to check the latest files generated, I.e. the file which is generated in the most recent directory (created or updated). The name of the file remains the same in all directories, so I just want a simple script which will change the directory to the last modified subdirectory.[Update] By seeing most of the answers, I think I should clarify that the directory names are dynamic (depends upon some user input & timestamp), so I can't assume a directory pattern like dir[1, 2, 3] or similar
Change to the last modified subdirectory of the current directory
bash;shell script;files;directory;timestamps
Depending on what you want exactly to do, and the structure of your files, other possibilites may be available, such as :ls -1tq /dir/*/readme.txt | head -n 1Returns the full name of the most recent readme.txt found in any subdirectory (not recursive) of /dir/The usage of * as part of the path (not just as part of a filename) often being underknown, I thought it was worth mentioning it.file=$( ls -1tq /dir/*/readme.txt | head -n 1 )cd $( dirname $file )