Reputation: 143
I have my Windows HDD mounted in /media in Ubuntu 16.04 LTS. In my terminal, I have navigated to the path /media/me/OS/Users/Me/Music. From here, I run a find command to search for all MP3s in this Windows path:
find . -type f -name *.mp3
This returns a bunch of .mp3 file paths (colors mean nothing; must have happened through the wiki formatting):
./iTunes/iTunes Music/Yeah Yeah Yeahs/It's Blitz/01 Skeletons.mp3
./iTunes/iTunes Music/Yeah Yeah Yeahs/It's Blitz/Gold Lion.mp3
./iTunes/iTunes Music/Yeah Yeah Yeahs/It's Blitz/Heads Will Roll.mp3
./iTunes/iTunes Music/Yeah Yeah Yeahs/It's Blitz/Maps.mp3
As you can see, there are spaces in the file names. My goal here is to copy all the files in the list (560 or so of them) to my Ubuntu HDD's /home/me/Music directory. Below is a list of things I've tried, all which end up with the terminal yelling at me that it can't find "./iTunes/iTunes", "Music/Yeah", "Yeah"...you get the idea.
Things I've tried:
for f in $(find . -name *.mp3); do cp $f ~/Music/WindowsMP3s/; done
find . -type f -name *.mp3 > output.txt
and then while read file; do cp $file ~/Music/WindowsMP3s/; done <output.txt
I've tried surrounding the filename in grave accents (`) in the for loops and in the output.txt file itself. I've even tried editing output.txt with 1 and 2 backslashes in front of every space.
Any ideas would be great!
P.S. Yes, I know I can just do this all through the file manager and copy them to a external hard drive and then copy them to Ubuntu but where's the fun in that?! :)
Upvotes: 2
Views: 2763
Reputation: 211
how about trying "-exec"..
find . -type f -name "*.mp3" -exec cp {} ~ \;
replacing the tilde with your intended copy destination.
References: https://unix.stackexchange.com/questions/243095/how-does-find-exec-pass-file-names-with-spaces
Upvotes: 5
Reputation: 4572
There are several ways you can do that. You have to use double-quotes for expanding variables with spaces (backticks are used for executing inline).
The easiest is copying all the contents of ./iTunes/iTunes Music
to /home/me/Music
via cp:
cp -av "./iTunes/iTunes Music"/* /home/me/Music
If you want to build a list, manipulate it, and then copy only the contents of that list, then use while read
, as you did, but enclose each result in quotes:
find . -type f -name *.mp3 > output.txt
cat output.txt | while read file; do
cp "$file" ~/Music/WindowsMP3s/
done
Upvotes: 0
Reputation: 482
You can simply append a wildcard after using quotes.
Example:
cp "./Folder With Spaces/"*.mp3 ./folderWithoutSpaces/
Upvotes: 2