如何在Linux终端

时间:2016-06-16 02:36:53

标签: linux windows bash ubuntu copy

我的Windows硬盘安装在Ubuntu 16.04 LTS的 / media 中。在我的终端中,我已导航到路径 / media / me / OS / Users / Me / Music 。从这里开始,我运行一个find命令来搜索此Windows路径中的所有MP3:

find . -type f -name *.mp3

这会返回一堆.mp3文件路径(颜色毫无意义;必须通过wiki格式发生):

./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

如您所见,文件名中有空格。我的目标是将列表中的所有文件(大约560个)复制到我的Ubuntu HDD的 / home / me / Music 目录中。下面列出了我尝试过的所有内容,所有内容都以终端向我大喊大叫它无法找到" ./ iTunes / iTunes","音乐/是啊","是啊" ...你明白了。

我尝试的事情:

  1. for f in $(find . -name *.mp3); do cp $f ~/Music/WindowsMP3s/; done
  2. find . -type f -name *.mp3 > output.txt然后while read file; do cp $file ~/Music/WindowsMP3s/; done <output.txt
  3. 我已尝试在for循环和output.txt文件本身的严重重音符(`)中包围文件名。我甚至尝试在每个空间前面编辑带有1和2个反斜杠的output.txt。

    任何想法都会很棒!

    P.S。是的,我知道我可以通过文件管理器完成所有操作并将它们复制到外部硬盘驱动器然后将它们复制到Ubuntu,但那里的乐趣是什么?! :)

3 个答案:

答案 0 :(得分:4)

如何尝试“-exec”..

find . -type f -name "*.mp3" -exec cp {} ~ \;

用您想要的复制目的地替换波浪号。

参考文献: https://unix.stackexchange.com/questions/243095/how-does-find-exec-pass-file-names-with-spaces

答案 1 :(得分:2)

您可以在使用引号后附加通配符。

示例:

cp "./Folder With Spaces/"*.mp3 ./folderWithoutSpaces/

答案 2 :(得分:1)

有几种方法可以做到这一点。您必须使用双引号来扩展带空格的变量(反引号用于执行内联)。

最简单的方法是通过cp将./iTunes/iTunes Music的所有内容复制到/home/me/Music

cp -av "./iTunes/iTunes Music"/* /home/me/Music

如果要构建列表,对其进行操作,然后仅复制该列表的内容,然后像您一样使用while read,但将每个结果括在引号中:

find . -type f -name *.mp3 > output.txt
cat output.txt | while read file; do
  cp "$file" ~/Music/WindowsMP3s/
done