-edit: more details, correct code-
I want to move files from a directory into sub directories by data out of a metadata file.
There are groups of files like <name>.<extension>
. Each group consists of 3 files. One of the files in each group has the extension .idx
. This is the metadata file, a text file.
In the metadata file, there is exact one line like VALUE_01=XXXXX
and some others (15 to 60 lines, <key>=<value>
, <key>
is unique).
Now I want to move all files <name>.*
from the current directory to a sub directory named XXXXX
in this case (the value to the key VALUE_01
).
I played around with for
loops, etc., but even a ls *.idx
doesn't work because there are round about 2 million files there! So it doen't work and I need some performance.
I tried
find . -maxdepth 1 -type f -name "*.idx" -exec grep -H "VALUE_01=" {} ";" | perl -pe 's/(.*?).idx:VALUE_01=(.*)$/\1.* .\/\2\//'
So I get a list like
./file1.* ./XXXXX/
./file2.* ./XXXXX/
./file3.* ./YYYYY/
I tried to pass this as arguments via xargs
to mv
.
... | xargs mv
to get
mv ./<name>.* ./XXXXX/
but I get error messages
mv: cannot stat './XXXXX/': No such file or directory
mv: cannot stat './file1.': No such file or directory
mv: cannot stat './XXXXX/': No such file or directory
mv: cannot stat './file2.': No such file or directory
mv: warning: source directory './YYYYY/' specified more than once
mv: cannot stat './file3.*': No such file or directory
I think, it's an incorrect use of xargs
.
I'm not good in shell programming, so I don't get the hint how to use it, or how to avoid it.