Search code examples
bashcopyfind

bash: find and copy stopped working in bash after moving script to different directory


I'm very much a rookie on bash, but after a lot of googling, I came up with this script to copy a bunch of files who's filenames are listed in a text file from a source directory (i.e. the directory where the script is being executed from) into a destination directory passed as an argument.

This worked just fine for the first group of files I used this script with. But after copying the script to a different directory and trying with a different set of files, it stopped working.

The strange thing is, if I just call find with one of the filenames listed in the textfile, the command finds the specified file, so I don't think the problem is that it is not finding the files.

Any help is greatly appreciated!

EDIT: I've updated the script per recommendation of the comments and added -print to the find statement. Still no output

The script:

#!/bin/bash
if [ "$#" -ne 2 ]; then
    echo "ARGUMENTS INCORRECT"
    echo "USAGE: bash copyScript.sh <list_of_filenames> <files_destination_address>"
    exit 1
fi
file=$1
while read -r line; do
# echo -e "finding $line"
find -iname "$line" -print -exec cp {} "$2" \;
done <$file

Solution

  • Thanks to all the comments!!

    Especially to @j_b in the comment section for pointing out the carriage return character that lead me to the source of my problem.

    My script has been correct the whole time. The problem was that the input text file comes from a windows system, and it had carriage return line endings (CRLF), but this being a bash script, was expecting an input text file with unix line endings (LF).

    I used dos2unix tool to convert my input textfile and everything worked as intended.