3
find $(MY_DIR)/source -type f -name "*.wav3" -print0 | \
xargs -0 -P $(NPROC) -n1 -I {} \
mv {} $(MY_DIR)/sound/{}.wav

From the above I'm trying to find all .wav3 files, move them to the sound folder however I'm not too sure how to trim the output file to just retain its basename:

$(MY_DIR)/source/deeply/nested/file/song1.wav3

$(MY_DIR)/source/deeply/nested/file/song2.mp3.wav3

will be moved to:

$(MY_DIR)/sound/song1.wav

$(MY_DIR)/sound/song2.wav

How can I adjust my script?

  • it would strip .song.wav3 and keep old as basename. – sp334 Jul 2 '16 at 11:38
  • use parameter expansion via sh -c ... e.g. find $(MY_DIR)/source -type f -name "*.wav3" -exec sh -c 'bn=${0##*/}; dn="${bn%%.*}".wav; printf %s\\n "$dn"' {} \; will find your files and print the destination files names... It's easy to change that to move each file to the new dn . Post an answer if you manage to do the rest – don_crissti Jul 2 '16 at 11:48
  • @don_crissti That should be sh -c '...' sh, the arguments start with $0. FWIW. – Satō Katsura Jul 2 '16 at 14:39
  • @SatoKatsura - works fine like that if you don't care much about error stuff... if you do sh -c '...' sh you have to use $1 instead of $0 – don_crissti Jul 2 '16 at 14:44
1

Instead of messing with find + xargs + mv just switch to zsh and do

autoload -U zmv
zmv -n '$(MY_DIR)/source/**/(*).wav3' '$(MY_DIR)/source/${1%%.*}.wav'

How it works:

  • first we load zmv via autoload
  • -n parameter is to prevent execution, just see what it will do, and if you are happy with the output remove this option
  • double star ** match all nested directories
  • (*) match anything and store the result in $1 variable
  • ${1%%.*} strip the first dot all subsequent characters
  • and finally add .wav extension
  • I'm actually using this in a makefile and is expected to be used on a linux flavored server, is there any way to not introduce any dependencies? – sp334 Jul 2 '16 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.