Extract filepaths of .MP4 files in subfolders of main folder to textfile

43 views Asked by At

I am on Mac. I want to use terminal or something to extract the filepaths of the .MP4 files within a folder with multiple levels of subfolders. I have a main folder that has subfolders. Each subfolder has its own subfolders. Eventually in this line, there are .MP4 files. I want a text file with the filepath of each .MP4 file, preferably with commas or semicolons between. Can you help me?

I looked into a few options, but they were either for Windows or not that helpful. The dir command in Windows seemed relevant perhaps.

1

There are 1 answers

0
Éric On BEST ANSWER

Considering this file structure:

/
├── Users
│   └── You
│       └── main_dir
│           ├── subdir_1
│           │   └── subdir_1-1
│           │       └── movie_A.mp4
│           ├── subdir_2
.           │   └── movie_B.mp4
.           └── subdir_3
.               └── movie_C.mp4

After installing findutils with this command:

brew install findutils

then running this command in the terminal:

gfind /Users/You/main_dir -iname "*.mp4" -fprintf ~/file.txt "'%p', "

should fill ~/file.txt with this content:

'/Users/You/main_dir/subdir_1/subdir_1-1/movie_A.mp4', '/Users/You/main_dir/subdir_2/movie_B.mp4', '/Users/You/main_dir/subdir_3/movie_C.mp4',

Some insights:

The -fprintf option of gfind (or find) prints the result in the specified format (here '%p', ) to the specified file (here ~/file.txt).

In the '%p', format:

  • %p uses the path and name of the found items, starting from the beginning of the searched path (here /Users/You/main_dir, implying that %p will start from the root /),
  • the above is enclosed between single quotes (') to deal with possible spaces and/or commas in the file paths,
  • then the characters , (comma, space) are appended.