cp launched from code results in cannot stat '/tmp/somedir' works in shell

5,116

Solution 1

Well I apologize in advance, the real problem was actually really quite simple... it was a problem with globbing files, but not because my shell had explicitly disabled it, or relative pathing.

I was launching "shell scripts" and shell commands but outside the context of bash/sh/tcsh etc. Executing cp directly is gonna just be problematic as far as that goes.

Executing

/bin/bash -c "cp /blah/* /someotherdir/"

worked fantastically. Appreciate the clues!

Solution 2

The call in the script where you're having the problem isn't globbing the files, but the shell does. I'm not quite clear on how you're calling it, but you'll need to either find a way to glob or loop over the files.

Solution 3

There's a bug in your script. We can't tell where because you haven't shown your script.

Given your limited description, it seems that the shell is not expanding /tmp/mnt221234jk/* to a list of files, even though the cp command, which is running with the same privileges, can traverse the directory. So a likely explanation is that you've turned off globbing with set -f or set -o noglob at some point. Turn in back on with set +f.

Share:
5,116

Related videos on Youtube

Ronnyek
Author by

Ronnyek

Updated on September 18, 2022

Comments

  • Ronnyek
    Ronnyek almost 2 years

    I'm working on making some scripts more elegant by implementing them in a real x application etc...

    At some point in the scripts we basically want to take everything from a mounted removable storage device, and copy into specified folder in a tmp work folder. When executing in the shell it works without a hitch. When executed from the code, it fails.

    Copy Command:

    cp -avfr /tmp/mnt221234jk/* /tmp/mnt23255/disk1/
    

    and the error I get from native app is:

    cp: cannot stat: '/tmp/mnt221234jk/*' file not found.
    

    The strange part is that it is absolutely there, has appropriate permissions for the user running the app etc. I can both stat /tmp/mnt221234jk/* and run the cp command from the shell and both work just fine.

    In addition to this, if I omit the /* from the file copy origin, it works, but creates a new folder in the destination with name mnt221234jk which is not what I am looking for.

    A couple of side notes: my code to execute shell commands absolutely works, and works well. But there very well may be something funky about needing work paths or something like that.

    Does failing to stat based on file not existing error ring any bells with anyone?

    I'd be willing to attempt to achieve this differently if there are alternative shell commands to effectively copy a file structure recursively with permissions etc.

    • Alen Milakovic
      Alen Milakovic over 12 years
      Give a minimal example that fails for you.
    • ChrisCornwall
      ChrisCornwall over 12 years
      To be clear, you actually have a file called /tmp/mnt221234jk/*?! Really?
  • alex
    alex about 9 years
    That's the answer I was looking for! Quick and working!