You can copy with the wildcards, that's fine. However, you need to rename the files, not just copy them which means you have to assign a new name to each file and that means a loop is unavoidable. At best you can use a tool that does the looping for you.
If you have perl-rename
(called renamed
on Debian-based systems), you can do:
$ rename -n 's|raw/([^/]+)/.*pdf|pdf/$1.pdf|' raw/*/*.pdf
raw/F1/file1.pdf -> pdf/F1.pdf
raw/F2/file2.pdf -> pdf/F2.pdf
That will find all filenames ending in .pdf
that are in any of the first level subdirectories of raw
, rename them to the name of their parent folder plus a .pdf
extension and save them under pdf/
. The -n
means "just print what you will do, don't do it", so if you are satisfied this works, run the command without the -n
to actually rename the files:
rename 's|raw/([^/]+)/.*pdf|pdf/$1.pdf|' raw/*/*.pdf
Alternatively, you can use a shell loop:
for f in raw/*/*pdf; do
new=$(sed -E 's|raw/([^/]+)/.*|pdf/\1.pdf|' <<<"$f");
echo "mv -- $f $new";
done
mv raw/F1/file1.pdf pdf/F1.pdf
mv raw/F2/file2.pdf pdf/F2.pdf
Here too, if you are satisfied it works, remove the echo to actually rename the files:
for f in raw/*/*pdf; do
new=$(sed -E 's|raw/([^/]+)/.*|pdf/\1.pdf|' <<<"$f");
mv -- "$f" "$new";
done
IMPORTANT: Both solutions assume you only have one file per directory so there can be no collisions. They will delete any extra files you have since al files in the same subdir will get the same name.
You could try using the -i
option of each respective tool (mv
an rename
) which will cause them to ask you for confirmation before overwriting a file with the same name. That would make the solutions:
rename -i 's|raw/([^/]+)/.*pdf|pdf/$1.pdf|' raw/*/*.pdf
and
for f in raw/*/*pdf; do
new=$(sed -E 's|raw/([^/]+)/.*|pdf/\1.pdf|' <<<"$f");
mv -i -- "$f" "$new";
done
raw/F1/file1.pdf
should be copied topdf/F1.pdf
using the original directory name as part of the file name. Is it necessary to check that the numbers inF1
andfile1.pdf
are the same? Is it guaranteed that there is only one file in every directoryF1
F2` etc? Please edit your question to confirm / clarify. Don't use comments to answer.