问题描述
如何将文件而不是目录移动到另一个文件夹/父文件夹中?
我的文件夹结构非常丑陋,有些.mp3文件在sub-folder中埋藏了6个层次。
我想使用Ubuntu将所有文件(大多数是.mp3,但不是全部)放在一个目录中,根本没有子目录。
救命?
最佳思路
To do so, Open a terminal and execute this command:
mv -v ~/Downloads/* ~/Videos/
It will move all the files and folders from Downloads folder to Videos folder.
To Move all files, but not folders:
But, If you are interested to move all files (but not folders) from Downloads folder to Videos folder, use this command
find ~/Downloads/ -type f -print0 | xargs -0 mv -t ~/Videos
To move only files from the Download folders, but not from sub-folders:
If you want to move all files from the Downloads folder, but not any files within folders in the Download folder, use this command:
find ~/Downloads/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ~/Videos
here, -maxdepth option specifies how deep find should try, 1 means, only the directory specified in the find command. You can try using 2, 3 also to test.
See the Ubuntu find manpage for a detailed explanation.
次佳思路
解决方案
find /src/dir -type f -exec mv --backup=numbered -t /dst/dir {} +
该命令将在/src/dir下找到所有常规文件(包括所有子目录),并使用命令mv将它们移动到/dst/dir。只需将目录替换为您的目录即可。具有相同名称的文件将自动重命名。
选择要移动的文件
如果只想移动MP3文件,请在-type f之后在find命令中添加-iname "*.mp3"选项。
与c0dev的回复比较
只有c0dev答复中的第二个命令才能回答该问题。下面是它与该回复的比较。点3和点4可以在其他答复中以与此处相同的方式解决。
除mv之外,使用-exec +的解决方案无需调用诸如xargs或parallel之类的其他命令,并两次提交文件名。
另一个答复将静默覆盖具有相同名称的文件。由于使用了--backup=numbered选项,此处文件将自动重命名。不幸的是,默认情况下,这些带有后缀的备份(如~3~)将隐藏在大多数文件管理中。不幸的是,mv不允许更改后缀,但是通过附加命令可以很容易地更改post-processed。这是一个GNU扩展。
与-print0相反,-exec command {} +是IEEE Std 1003.1(POSIX),ISO /IEC 9945和Single UNIX Specification标准的一部分。因此,它应该更便携。参见IEEE Std 1003.1, 2004 Edition,IEEE Std 1003.1, 2013 Edition和0000243: Add -print0 to “find”。但是无论如何,mv所需的-t开关是GNU扩展,因此整个命令不能在POSIX系统之间移植。
注意:在这种情况下,find能够产生以-开头的路径(我现在不知道find的任何实现。){}应该以end-of-options指示符开头:-。
第三种思路
不幸的是,我没有足够高的声誉来评论标记的解决方案。但是,我想提醒其他人我遇到的问题。相当业余;但是,当您做几件事时,一开始可能不会想到。希望它将对其他人有所帮助。
Problem
启动命令后,提供了以下消息的变体。然后,该命令将创建多个文件。
mv: `/data/share/docs/src/dir/filename.ext' and `/data/share/docs/src/dst/filename.ext' are the same file
Cause
/src是/dst(例如/src/../dst/)的父级。
解决方案
尽管可能有更好的解决方案,但我只是将文件移到了/src之外的临时目录中,然后重新运行命令以将它们放回我希望它们最终位于的/src/../dst目录中。
参考资料