0

Need move files with multiple, defined patterns, like:

Z_*ana_bwk_na_N*.png
Z_*ana_bwk_dwda*.png
Z_*ana_bwkman_dwdna*
Z_*ana_bwkman_dwdc*
Z_*EDZW*_nwv01*_p00_na_N*WV11.png
Z_*EDZW*_nwv01*_p00_nh_N*WV11.png
Z_*nwv01*_hsy_NA*.png
Z_*EDZW_*_htp_na_N_*_WV11SW.png
Z_*_rft_na_*.png
Z_*_r12*

from one defined folder, to another.

How it can be done in best economic way? In a script, running from cron?

Can this patterns be defined better in same script, or get from separate textfile, containing them?

  • 4
    "Most economic" is probably with a single mv with all those wildcards as its argument and the destination directory as its final argument. But it's not clear if that is actually what you want. Do all the matches go to the same destination? – tripleee Oct 4 '17 at 12:09
  • first, thanks for edition and typo. second - now, yes, i want it all to go on one destination folder, but in future i need group small patterns, by 2 - 3, to different ftp path upload each... as so, is be good to cover both possibilities, if it is possible ;) – Draco Oct 4 '17 at 12:12
  • i find alot solutions like for my problem, but only for one pattern. i here have, opposing, many different patterns, and cannot find my situation silved in google, sadly, so i come here with some hope... – Draco Oct 4 '17 at 12:19
0

The most efficient, but possibly the least legible, approach is to just enumerate everything in one big happy mv command.

mv Z_*ana_bwk_na_N*.png \
    Z_*ana_bwk_dwda*.png \
    Z_*ana_bwkman_dwdna* \
    Z_*ana_bwkman_dwdc* \
    Z_*EDZW*_nwv01*_p00_na_N*WV11.png \
    Z_*EDZW*_nwv01*_p00_nh_N*WV11.png \
    Z_*nwv01*_hsy_NA*.png \
    Z_*EDZW_*_htp_na_N_*_WV11SW.png \
    Z_*_rft_na_*.png \
    Z_*_r12* \
  destination

If you want to split this up (perhaps because you get "command line too long") you can use a here document.

while read files; do
    mv $files dest
done <<____
    Z_*ana_bwk_na_N*.png
    Z_*ana_bwk_dwda*.png
    Z_*ana_bwkman_dwdna*
    Z_*ana_bwkman_dwdc*
    Z_*EDZW*_nwv01*_p00_na_N*WV11.png
    Z_*EDZW*_nwv01*_p00_nh_N*WV11.png
    Z_*nwv01*_hsy_NA*.png
    Z_*EDZW_*_htp_na_N_*_WV11SW.png
    Z_*_rft_na_*.png
    Z_*_r12*
____

If you want to parametrize the destination, you can do that too.

while read destination files; do
    mv $files "$destination"
done <<____
    here       Z_*ana_bwk_na_N*.png
    here       Z_*ana_bwk_dwda*.png
    here       Z_*ana_bwkman_dwdna*
    there      Z_*ana_bwkman_dwdc*
    there      Z_*EDZW*_nwv01*_p00_na_N*WV11.png
    everywhere Z_*EDZW*_nwv01*_p00_nh_N*WV11.png
    here       Z_*nwv01*_hsy_NA*.png
    elsewhere  Z_*EDZW_*_htp_na_N_*_WV11SW.png
    there      Z_*_rft_na_*.png
    anywhere   Z_*_r12*
____

Somewhat confusingly, the wildcards in the here document will be expanded by the shell before the loop runs. Because we need the inner loop to be unquoted, the expanded file names cannot contain any shell metacharacters (which do not match exactly only themselves -- typically irregular whitespace or wildcard expressions).

0

It's simple:

mv Z_*ana_bwk_na_N*.png Z_*ana_bwk_dwda*.png Z_*ana_bwkman_dwdna* -t /your/directory/

If you want it more elegant with input from file:

PATTERNS=`cat input.txt | paste -sd " " - ` && mv $PATTERNS -t /your/directory/
0

assume your patterns in a file called patterns

mapfile -t arr < patterns && mv -t destination "${arr[@]}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.