Thursday, May 2, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 1997  / 2 Years ago, fri, april 22, 2022, 3:10:11

I am trying to make a bash script that will find all the mp3s in my Downloads folder and move them into a music folder. Why won't this line work?



find ./ -type f -name *.mp3 | mv *.mp3 /home/mitch/Desktop/Music/$ARTIST


I tried using a pipeline to have the information from the first command translate to the second, but I have never used a pipeline before and don't know if I'm doing it wrong or if I have the wrong idea of what they are used for.


More From » command-line

 Answers
0

The mv command do not use its stdin, so you have to translate the stdin to a parameter, and for this can be used xargs:



find ./ -type f -name '*.mp3' | xargs mv -t /home/mitch/Desktop/Music/$ARTIST


Also, you can directly use the -exec action of find:



find ./ -type f -name '*.mp3' -exec mv {} /home/mitch/Desktop/Music/$ARTIST ';'


Edit



As pointed out by @geirha, the first command do not work if you have filenames containing spaces. In such a case it should be as follows



find ./ -type f -name '*.mp3' -print0 | xargs -0 mv -t /home/mitch/Desktop/Music/$ARTIST

[#40097] Friday, April 22, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
warrdel

Total Points: 356
Total Questions: 103
Total Answers: 118

Location: Bangladesh
Member since Sat, Jan 23, 2021
3 Years ago
;