Moving multiple files in subdirectories (and/or splitting strings by multichar delimeter) [bash]

只谈情不闲聊 提交于 2019-12-10 22:43:33

问题


So basically, I have a folder with a bunch of subfolders all with over 100 files in them. I want to take all of the mp3 files (really generic extension since I'll have to do this with jpg, etc.) and move them to a new folder in the original directory. So basically the file structure looks like this:

/.../dir/recup1/file1.mp3

/.../dir/recup2/file2.mp3

... etc.

and I want it to look like this:

/.../dir/music/file1.mp3

/.../dir/music/file2.mp3

... etc.

I figured I would use a bash script that looked along these lines:

#!/bin/bash
STR=`find ./ -type f -name \*.mp3`

FILES=(echo $STR | tr ".mp3 " "\n")

for x in $FILES
do
    echo "> [$x]"
done

I just have it echo for now, but eventually I would want to use mv to get it to the correct folder. Obviously this doesn't work though because tr sees each character as a delimiter, so if you guys have a better idea I'd appreciate it.

(FYI, I'm running netbook Ubuntu, so if there's a GUI way akin to Windows' search, I would not be against using it)


回答1:


If the music folder exists then the following should work -

find /path/to/search -type f -iname "*.mp3" -exec mv {} path/to/music \;

A -exec command must be terminated with a ; (so you usually need to type \; or ';' to avoid interpretion by the shell) or a +. The difference is that with ;, the command is called once per file, with +, it is called just as few times as possible (usually once, but there is a maximum length for a command line, so it might be split up) with all filenames.




回答2:


You can do it like this:

find /some/dir -type f -iname '*.mp3' -exec mv \{\} /where/to/move/ \;

The \{\} part will be replaced by the found file name/path. The \; part sets the end for the -exec part, it can't be left out.

If you want to print what was found, just add a -print flag like:

find /some/dir -type f -iname '*.mp3' -print -exec mv \{\} /where/to/move/ \;

HTH



来源:https://stackoverflow.com/questions/8705757/moving-multiple-files-in-subdirectories-and-or-splitting-strings-by-multichar-d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!