Bash find files from list and copy to another directory

hello
I have a txt file with file names and folder that contains sub-folders with lots of files I want to search in the folder the files from the list and copy the files to another directory.

I tried this:

    while IFS=, read -r file rest
    do
    find . -print -exec cp "$file" ./new_folder \; | grep -i '.*[.]zip' 
    done < 1.txt

but it doesnt work.

example for file name is :

> JLJA824J_ELS_Log_20200122055710+0000

when i run it for one file and not recursively it worked so i think the find command not working properly and not find what i need.

any suggestions ?

thanks

Hi and welcome.
Perhaps this will be the solution:

while IFS=, read file rest; do
        find -not -path "./new_folder/*" -type f -name "$file" -exec cp -t new_folder/ {} \;
done <1.txt

Or do you want to add a .zip extension to every file?

Hey
Thanks for your answer but this not coping the files..

Check the functionality, add the echo command:

while IFS=, read file rest; do
        find -not -path "./new_folder/*" -type f -name "$file" -exec echo cp -t new_folder/ {} \;
done <1.txt

if the file names contain a full path, then the find command is superfluous
added:
the new_folder directory must exist in the current one.

Thanks for your answer.
nothing happens. prompt returns and that's it
the file name is exactly as its written in the question

> JLJA824J_ELS_Log_20200122055710+0000

and all the files and folders are located at the same place

are saying your file is named as above with the leading >spaceWhatever (mnemonically speaking)?
Also would you do cat -vet 1.txtand provide the output using markdown tags, pls.

1 Like

It's easy to fix. Change the variable "$file" to "${file##*[[:blank:]]}" in the find command.
This is a universal solution, as noted above, perhaps a tabulation or several spaces acts as a separator, but it is quite possible that this will also work: "${file#* }"

thanks for your answer
im getting this error:

cp: cannot stat 'to': No such file or directory
cp: cannot stat 'JLJA833J_CSM_Security_20190517021148+0000-[787ML017]'$'\r': No such file or directory

the is not with >

this the result of cut

JLJA824J_ELS_Log_20200119072723+0000.zip^M$
JLJA824J_ELS_Log_20200119072650+0000.zip^M$
JLJA824J_ELS_Log_20200119072719+0000.zip^M$
JLJA824J_ELS_Log_20200119072715+0000.zip^M$
JLJA824J_ELS_Log_20200119072711+0000.zip^M$

Then like this:

"${file%$'\r'}"

Or convert 1.txt file in unix format, or just add in last line of the script next:

done < <(tr -d '\r' <1.txt)
2 Likes

this is the error im getting:

cp: cannot stat 'to': No such file or directory
cp: cannot stat 'JLJA833J_CSM_Security_20190517021148+0000-[787ML017]': No such file or directory

maybe there is something else i should add and im missing ?
if its not rude, maybe you can try to write it from the beginning and see if it is working for you ?
many thanks

Let's go in order, to begin with, let's check to read file correctly:

IFS=$'\r' read file rest <1.txt
echo "$file" | cat -vet

Post output please.

1 Like

JLJA833J_CSM_Security_20190517021148+0000-[787ML017]$

it outputs only one row

1 Like

Run the command and see the result:

ls -d new_folder/
new_folder/

If this command does not output "new_folder", then create a directory:

mkdir new_folder

Substitute the previous command into the find command:

find -name "$(IFS=$'\r' read file rest <1.txt; echo $file)"

output:

./path/to/dir/JLJA833J_CSM_Security_20190517021148+0000-[787ML017]

all right now?

1 Like

In my opinion, I understood. You have more than just filenames in your list. File names end with a .zip extension and need to be filtered? Or are there files both with the .zip extension and without any extension?

output for the last one is empty
for the first one is ok

i have only file names in my list
they ends with .zip and these are the files i need to copy
in the folder where i want to run the find command there are files with .zip and without but i need to copy the files with .zip extension

Try this:

while IFS=$'\r' read file rest; do
    find -not -path "./new_folder/*" -type f -name "$file" -name "*.zip" -exec echo cp -t new_folder/ {} \;
done <1.txt

Make sure that your version of the cp utility has a -t option.
If the output matches the desired, remove echo command.

Alternative options:

for file in $(grep -o '^[^,]*\.zip' 1.txt); do
    find -not -path "./new_folder/*" -type f -name "$file" -exec cp -t new_folder/ {} \;
done
grep -o '^[^,]*\.zip' 1.txt |
while read file; do
    find -not -path "./new_folder/*" -type f -name "$file" -exec cp -t new_folder/ {} \;
done

when copying files nothing is displayed on the screen, just check the new_folder directory

nothing happens for all the options :frowning:

ls new_folder