Use bash -type `$ {i% .mp3}` syntax with xargs?

Usage Example (Invented!): Rename .mp3 Files to .txt.

I would like to do something like

find -name '*.mp3' -print0 | xargs -0 -I'file' mv file ${file%.mp3}.txt

This does not work, so I resorted to outputting findfor piping through sed -e 's/.mp3//g', which does the trick.

Seems a bit hacky; Is there a way to use the usual bash syntax ${x%y}?

+3
source share
5 answers

No, xargs -0 -I'file' mv file ${file%.mp3}.txtit will not work, because the variable filewill be expanded by the xargs program, and not by the shell. Actually, it is not entirely correct to refer to it as a variable; it is called "replace-str" in the xargs manual.

. xargs bash ( sed ), , , xargs bash, , :

find -name '*.mp3' -print0 | xargs -0 bash -c \
'while [ -n "$1" ]; do mv "$1" "${1%.mp3}.txt" ; shift; done;' "bash"
+2

GNU Parallel:

find -name '*.mp3' -print0 | parallel -0 mv {} {.}.txt

\n ( ), :

find -name '*.mp3' | parallel mv {} {.}.txt

, GNU Parallel http://www.youtube.com/watch?v=OpaiGYxkSuQ

+1

, , sed.

find -name '*.mp3' -print |
sed -e 's/\.mp3$//' -e "s/.*/mv '&\.mp3' '&\.txt'/" |
sh

, mv sh.

, - ( ) . , " xargs, ".

Not all seds have exactly the same syntax. In particular, I'm not 100% sure that you can have several arguments on some cool old SunOS (look for an XPG-compatible sed in something like /usr/ucb/blah/barf/vomit/xpg4/bin/sed)

+1
source
find . -type f -name '*.mp3' | while read -r F
do
  mv "$F" "${F%.mp3}.txt"
done

Bash 4+

shopt -s globstar
shopt -s nullglob
for file in **/*.mp3
do
    mv "$file" "${file%.mp3}.txt"
done
0
source
perl rename.pl 's/\.mp3$/.txt/' *.mp3  
# Or **/*.mp3 for zsh and bash with specific settings set, for a recursive 
# rename.

http://people.sc.fsu.edu/~jburkardt/pl_src/rename/rename.html

0
source

Source: https://habr.com/ru/post/1768631/


All Articles