Moving multiple files to subdirectories (and / or splitting strings by multi-channel divider) [bash]
So I have a folder with a bunch of subfolders with over 100 files. I want to take all the mp3 files (really a generic extension since I will have to do it with jpg, etc.) and move them to a new folder in the original directory. Thus, the file structure looks like this:
/.../rezh/recup1/file1.mp3
/.../rezh/recup2/file2.mp3
... etc.
and I want it to look like this:
/.../rezh/music/file1.mp3
/.../rezh/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 only have this echo right now, but eventually I would like to use mv
to get it in the correct folder. Obviously this doesn't work because tr sees each character as a delimiter, so if you guys understand better, I'd appreciate it.
(FYI, I'm running an Ubuntu netbook, so if there is a GUI way similar to Windows search, I wouldn't use it)
source to share
If the folder exists music
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 ;
(so you usually need to type \;
or ';'
to avoid shell interpretation) or +
. The difference is that using the ;
command is called once for each file with +
, it is called as few times as possible (usually once, but the maximum length for the command line can be separated) with all the filenames.
source to share
You can do it like this:
find /some/dir -type f -iname '*.mp3' -exec mv \{\} /where/to/move/ \;
The part \{\}
will be replaced with the found filename / path. The part \;
sets the end for the part -exec
, it cannot be ignored.
If you want to print what you find, just add a flag -print
, for example:
find /some/dir -type f -iname '*.mp3' -print -exec mv \{\} /where/to/move/ \;
NTN
source to share