0

First I should apologize in advance - I have no idea what I'm doing when it comes to the Mac terminal...

I need to do an inventory of all files in a directory/folder. There are several sub directories/folders which also have sub directories/folders, and so on.

I would like to be able to get one line for each file containing:

  1. the path, e.g. DirectoryName1\DirectoryName2\DirectoryName3 etc.
  2. the file name, including the extension (e.g. .docx, .xlsx, etc)
  3. date created (dd/mm/yyyy)
  4. last date modified (dd/mm/yyyy)
  5. file size in KB

I can then copy & paste the results into a spreadsheet and take it from there.

I have tried the ls and find commands, each of which gives me a piece of what I need, but not the whole (without a lot of manual combining) the two outputs. I've tried using the printf command in conjunction with each, but based on the error messages I get, am obviously missing something.

Is there a way to do this?

TIA,

Tom

2
  • Welcome to SE, Tom. Do you just need to list regular files and their sub-directories ? In the *nix world there also are other types of files, such as: block special, character special, symbolic link, sockets, file descriptors, named pipes, etc. All are files. Which do you want ? All ? Some ?
    – Cbhihe
    Jul 4, 2020 at 7:43
  • All files. It’s a mix of new/current and older versions of MS Office files, PDFs, images, etc. thanks!
    – Tom G
    Jul 5, 2020 at 11:13

1 Answer 1

2

Not sure what your default shell is on your macOS or OSX, but if the stat utility either is available (it should) or can be installed, you can do this:

$ find . -exec stat -c "%n,%w,%y,%s" {} \;  # on Arch linux
$ find . -exec stat -t "%F %T" -f "%N,%B,%m,%z" {} \;  # on macOS, OSX

Example of CSV output:

./.bash_history,2020-07-05 19:27:36.691875334 +0200,2020-07-05 19:27:36.691875334 +0200,54669
./.gitignore_global,2018-03-03 14:22:52.298262296 +0100,2018-03-03 14:22:52.298262296 +0100,423
...

If you are not familiar with this, I recommend you type man find and then man stat[1] in your terminal to get oriented. Here the short take on what you will see:

  • . points to your present working directory, i.e. the directory in which you find yourself when you issue the command above. You can change that to any fully qualified directory path if you issue the command from a directory that is different from the one whose tree you want to explore
  • for each file found, execute (-exec flag) the cmd stat with the following output format:

On Arch Linux:

  • %n fully qualified file name (file path + file basename),
  • %w human readable file time of birth in the form YYYY-MM-DD HH:MM:SS +/-hhmm with your system's numeric time zone denoted +hhmm or -hhmm. Seconds' decimals are included,
  • %y human-readable time of file's data's last modification, format as before
  • %s total file size in bytes.

On macOS or OSX, stat has a different implementation than that on GNU-centric platforms. I could not test for format under macOS for this answer, but in principle the correct options should be:

  • %N file name,
  • %B file time of birth, date time format as above, without the numeric time zone,
  • %m time of file's last modification, format as with %B,
  • %z total file size.

If you want to preserve this output for result post-processing, include an output redirection to your cmd, like so:

$ find . -exec stat -t "%F %T" -f "%N,%B,%m,%z" {} \; > stat.out # on macOS or OSX

You can open your CSV output file directly from LibreOffice Calc. If, however, you wish to produce native Excel format output, your best best is to use either perl or python.

The output file stat.out will be rewritten every time you re-issue the cmd. Change its name every time to avoid that.

You must log in to answer this question.

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