Search code examples
bash

Print all files with an extension in all subdirectories to a CSV (BASH)


I'm writing a bash script that handles backup logs. I have month folders which contain subfolders and in those subfolders there are multiple files that have the extension ".ptx".

I want to grab every file ending with .ptx and print this out to a CSV file.

I'm not sure how to approach this as I have pretty limited knowledge but I think I need to use find like in this example.

This is where I was at for the month February:

if [ -d "/Volumes/NAS/01_PROJECTS/01_SHORTFORM/2024/02_FEB/" ]; then
  
cd /Volumes/NAS/01_PROJECTS/01_SHORTFORM/2024/02_FEB/

printf '%s\n' * > AUDIO_BACKUP_LOG_02_FEB.csv

mv AUDIO_BACKUP_LOG_02_FEB.csv /Volumes/NAS/04_ADMIN/06_BACKUP_LOGS/MONTHS/2024/

echo "Feb completed"

fi

I'm not quite sure how to combine printf and find and also look into all the subdirectories.


Solution

  • To get just the name of the file and strip the directory path you can try something like:

    find . -type f -iname '*.ptx' -exec basename {} \; > AUDIO_BACKUP_LOG_01_JAN.csv
    

    If you want to exclude by mask some files you can try something like:

    find . -type f -iname '*.ptx' -exec basename {} \; |grep -v '.bak.' > AUDIO_BACKUP_LOG_01_JAN.csv