1

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
    Does your find support -printf? Commented Oct 6, 2019 at 12:29
  • @KamilMaciorowski I believe it works, would prefer commands that work in a generic Unix Shell (so that it works for both OS X and Linux)
    – Mick
    Commented Oct 6, 2019 at 12:44

1 Answer 1

4

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.

3
  • Brilliant, that did the trick!
    – Mick
    Commented Oct 6, 2019 at 13:47
  • Invalid command -c" stat: illegal option -- c On mac Commented Aug 21, 2023 at 23:40
  • @BerlinBrown You are missing a space between -c and the double quote (you entered -c"%n: %y" and not -c "%n: %y")
    – xenoid
    Commented Aug 22, 2023 at 5:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .