I am trying to write a shell script to copy specific files from an archive USB thumb drive to my Rasberry Pi 4b. The script is, in my opinion, brain dead simple.

cp /media/usb0/archive/file1.zip /home/pi/app/files/
cp /media/usb0/archive/file2.zip /home/pi/app/files/

ls -Fal /home/pi/app/files/

I get no error messages but neither file1.zip nor file2.zip can be found in /home/pi/app/files/

If file2.zip can't be found on the USB drive, I get this error message, as I expect.

cp: Cannot stat '/media/usb0/archive/file2.zip': No such file or directory

This means that the script is executing. Yay for that!

What am I missing here? Any help will be greatly appreciated.


I edited the script to look like this:

set -x
cp /media/usb0/archive/file1.zip /home/pi/app/files/
cp /media/usb0/archive/file2.zip /home/pi/app/files/
ls -Fal /home/pi/app/files/

Output of the script is /media/usb0/copy.sh: 2: set: Illegal option-

  • 1
    "This means that the script is executing" no, it means that cp isn't reporting an error. Near the top of the script (ideally just after the first line #! marker that defines the script interpreter) add set -x. Now run the script again. Can you see your cp statements being executed? – roaima Jan 14 at 0:24
  • Adding set -x to the second line the error out with /media/usb0/copy.sh: 2: set: Illegal option - – amber Jan 14 at 2:05
  • 1
    It could be Windows line endings. Shell scripts must be saved with unix line endings. – Oskar Skog Jan 14 at 2:26

The easiest way to convert a file from Windows endings to Unix endings is to execute dos2unix (available in apt, etc).

When working on Windows, Sublime Text could be a solution too: in the menu bar, View -> Line endings -> Unix, should do the trick.

  • here a custom plugin to make Sublime Text line ending defaults to Unix: forum.sublimetext.com/t/… – Maik93 Jan 14 at 7:57
  • 1
    @amber please accept the answer that solved the problem. this marks the question as "done" and it no longer shows up in the list of problems that "need attention" – umläute Jan 14 at 8:02
  • @umläute It wanted me to wait to mark it as the answer until tomorrow. – amber Jan 14 at 14:26
  • @mark93 I just used Notepad++'s Edit -> EOL Conversion -> Unix feature. It was super convenient as I already had it and had been editing the script in it to begin with. – amber Jan 14 at 14:28
  • @amber how about accepting an answer that is not your own? – umläute Jan 15 at 7:59

And solved!

It was the Windows line termination getting in the way. Converted it to Unix line termination, using Notepad++'s End Of Line conversion feature, and it worked like a charm.

Thank you!

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.