How do I write a bash script to copy files into a new folder based on name? -
i have folder filled ~300 files. named in form username@mail.com.pdf. need 40 of them, , have list of usernames (saved in file called names.txt). each username 1 line in file. need 40 of these files, , copy on files need new folder has ones need.
where file names.txt has first line username only: (eg, eternalmothra), pdf file want copy on named eternalmothra@mail.com.pdf.
while read p; ls | grep $p > file_names.txt done <names.txt
this seems should read list, , each line turns username username@mail.com.pdf. unfortunately, seems last 1 saved file_names.txt.
the second part of copy files over:
while read p; mv $p foldername done <file_names.txt
(i haven't tried second part yet because first part isn't working).
i'm doing cygwin, way.
1) wrong first script won't copy over? 2) if work, second script correctly copy them over? (actually, think it's preferable if copied, not moved over).
edit: add figured out how read lines txt file here: looping through content of file in bash
solution comment: problem just, echo > b
overwriting file, while echo >> b
appending file, replace
ls | grep $p > file_names.txt
with
ls | grep $p >> file_names.txt
there might more efficient solutions if task runs everyday, one-shot of 300 files script good.
Comments
Post a Comment