Search code examples
linuxbashfindoutputls

How do I list the files in subfolders and output this list to a local file?


I am using find and ls to list the mp3 files contained in subfolders. In each subfolder I want to output the list of the files it contains to a local file (stored in this subfolder).

I tried this command :

find . -type f -name \*.mp3 -execdir basename {} >> playlist.m3u \;  

but the file playlist.m3u it writes in is stored in the root directory . and not the subdirectory of the found mp3 file.

Is there a way to write to a file stored in the subdirectory ?


Solution

  • Try the following command, this will help you :

       find -type d -exec bash -c 'cd $1; find  -maxdepth 1  -not -name "." -name \*.mp3 -printf "%f," | awk "{print substr(\$0,0,length(\$0)-1)}"  > playlist.m3u' bash "{}" \;
    

    This will separate list of files with commas, please change this accordingly.