How to recursively find the latest modified file in a directory?
find . -type f -printf '%T@ %p\n' \
| sort -n | tail -1 | cut -f2- -d" "
For a huge tree, it might be hard for sort
to keep everything in memory.
%T@
gives you the modification time like a unix timestamp, sort -n
sorts numerically, tail -1
takes the last line (highest timestamp), cut -f2 -d" "
cuts away the first field (the timestamp) from the output.
Edit: Just as -printf
is probably GNU-only, ajreals usage of stat -c
is too. Although it is possible to do the same on BSD, the options for formatting is different (-f "%m %N"
it would seem)
And I missed the part of plural; if you want more then the latest file, just bump up the tail argument.
Bash - What is a good way to recursively find the type of all files in a directory and its subdirectories?
This may help: How to recursively list subdirectories in Bash without using "find" or "ls" commands?
That said, I modified it to accept user input as follows:
#!/bin/bash
recurse() {
for i in "$1"/*;do
if [ -d "$i" ];then
echo "dir: $i"
recurse "$i"
elif [ -f "$i" ]; then
echo "file: $i"
fi
done
}
recurse $1
If you didn't want the files portion (which it appears you don't) then just remove the elif and line below it. I left it in as the original post had it also. Hope this helps.
Method to recursively search for a file in directory and subdirectories doesn't find the file
I believe that return $this->findFileInPathRecursive($file_to_find, $new_path);
will cause an early return when the first subfolder does not contains the file.
You can avoid by only returning when the file is found.
if($found = $this->findFileInPathRecursive($file_to_find, $new_path)) {
return $found;
}
public function findFileInPathRecursive($file_to_find, $path) {
if (substr($path, -1) != "/") $path .= "/"; // Add slash at path's end
$files = scandir($path);
foreach($files as $file) {
if (substr($file, 0, 1) == ".") continue; // skip if "." or ".."
if (is_dir($path.$file)) { // if "file" is a directory, call self with the new path
$new_path = $path.$file;
// We only want to return file
if($found = $this->findFileInPathRecursive($file_to_find, $new_path)) {
return $found;
}
}
else {
if (pathinfo($file)['basename'] == $file_to_find OR pathinfo($file)['filename'] == $file_to_find) {
return $path.$file;
}
}
}
return false;
}
Related Topics
How to Find Certain Character Position in Shell
Connection Refused to Mongodb Errno 111
Using the "Alternate Screen" in a Bash Script
[ :Unexpected Operator in Shell Programming
Edit Shell Script While It's Running
How to Programmatically Disable Hardware Prefetching
Get the Perl Rename Utility Instead of the Built-In Rename
Minimal Executable Size Now 10X Larger After Linking Than 2 Years Ago, For Tiny Programs
Free Git Server With Web Gui a La Bitbucket/Github
Extract File Basename Without Path and Extension in Bash
How to Shutdown a Spring Boot Application in a Correct Way
Linking Against an Old Version of Libc to Provide Greater Application Coverage