Search code examples
bashcp

Why are "/r" characters being appended to filenames when I try to copy them?


I have a web server that generates GUID as filenames for JPGs stored on a web directory. Part of the GUID contains hyphens, an example would be:

34bc9ead-f8f0-467d-8605-df77f392da07.jpg

It then produces a text file stored on the server that contains all those JPGs in a list and would look like this

www.awebsite.com/img/34bc9ead-f8f0-467d-8605-df77f392da07.jpg

On a raspberry pi client, I run wget to get this text file and the images. I then need to copy those images to another drive.

If I run bash to read the text file, strip out the full URL so it's only the file name, and then use cp to copy the file it produces a strange error saying it cannot copy file "34bc9ead-f8f0-467d-8605-df77f392da07.jpg/r". Note the end of the file seems to append "/r".

Here's my code:

while read -r p; do
[[ "$p" == */* ]] || p="./$p"
b="${p##*/}"
cp $b /newdirectory/$b
done < wget_filelist.txt
sleep 5
done;

Just to add that I can't use wget to download the files to the destined directory. I use wget to put it in one directory and then copy the images to ram-disk to read for another job.

I thought about removing the hyphens from the file name which I can do successfully but would cause me headaches elsewhere for what I'm trying to achieve. It just seems reading a text file and passing that onto cp causes this odd "/r" to appear.


Solution

  • Your file wget_filelist.txt has DOS style CRLF(\r) endings present, remove it by running

    dos2unix wget_filelist.txt
    

    and run your original bash script.