shell - BASH script for sorting files recursively by base filename into folders of the same name -


i have following file structure:

\hi

  • actionpotential_hi.mp4
  • adhd_hi.mp4
  • alzheimersdisease_hi.mp4
  • alzheimers_art_hi.mp4
  • artificial_eye_hi.mp4
  • more files ...

\lo

  • actionpotential_lo.mp4
  • adhd_lo.mp4
  • alzheimersdisease_lo.mp4
  • alzheimers_art_lo.mp4
  • artificial_eye_lo.mp4
  • etc.

\med

*base_filename*_med.mp4

\stills

*base_filename*_med.jpg

\captions

*base_filename*.adb.xml

\transcripts

*base_filename*.txt

in order ingest these marklogic environment, need these rearranged following structure, asset base filename.

\asset

  • asset_lo.mp4
  • asset_med.mp4
  • asset_hi.mp4
  • asset.txt
  • asset.adb.xml
  • asset_med.jpg

i bash script sort these out me. suggestions?

find . -type f -print | while read -r pathname;     filename=${pathname##*/}     case "$filename" in         *_hi* | *_med* | *_lo*)             # strip off last underscore , following chars             new_dirname=${filename%_*}              ;;         *)             # strip off first dot , following chars             new_dirname=${filename%%.*}              ;;     esac     mkdir -p "../$new_dirname"     echo mv "$pathname" "../$new_dirname/$filename" done  

untested. remove echo when you're satisfied mv commands correct.

i moved destination directories parent dir of cwd because i'm not whether find pick newly created directories. can address point?


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -