Recursively list all files and subdirectories with modified timestamp

In a Unix Shell you can use the following to print a list of all files and subfolders recursively:

cd some/path/to/folder
du -a

and you can list file information (such as permissions and timestamps) with the following:

stat filename.ext

Is there a way you can combine the commands to list all the files and subfolders of a directory recursively (with relative path) followed by the modified time? For example:

a_file.txt: 06/27/19 - 08:34:16
a_subfolder: 07/30/18 - 18:23:14

Bonus: Something that will work for both Linux and OSX, and output to a txt file

2

1 Answer

You can provide a format to stat: stat -c '%n: %y' somefile will print the name and last modification time of somefile (%n actually replicates the parameter passed as a filename).

find [...search parameters...] -exec somecommand {} \; iterates alld files and directories and executes somecommand on every file/directory that matches, passing the file/directory name as a parameter.

Putting it all together:

find . -exec stat -c "%n: %y" {} \;

... will iterate all the files and directories under the current directory, and print their name and last modification date.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like