4

I need to use a shell script to move all files in a directory into another directory. I manually did this without a problem and now scripting it is giving me an error on the mv command.

Inside the directory I want to move files out of are 2 directories, php and php.tmp. The error I get is cd: /path/to/working/directory/php: No such file or directory. I'm confused because it is there to begin with and listed when I ls the working directory.

The error I get is here:

ls $PWD #ensure the files are there
mv $PWD/* /company/home/directory
ls /company/home/directory #ensure the files are moved

When I use ls $PWD I see the directories I want to move but the error afterward says it doesn't exist. Then when I ssh to the machine this is running on I see the files were moved correctly.

If it matters the directory I am moving files from is owned by a different user but the shell is executing as root.

I don't understand why I would get this error so, any help would be great.

Rob Nordberg
  • 55
  • 1
  • 3
  • 6
  • What happens if you just do `mv $PWD/php /company/home/directory` ? – 123 Jan 15 '17 at 21:20
  • You mentioned ssh'ing to the machine this is running on -- is this script being run remotely or something like that, and if so how? – Gordon Davisson Jan 15 '17 at 21:27
  • How do you lauch your script? – Jdamian Jan 15 '17 at 21:42
  • 4
    The weirdest part is that you get an error from `cd` even though your posted script has no `cd`statement. – that other guy Jan 15 '17 at 21:53
  • @jdamian This is a gitlab runner so, a gitlab server is dumping the repo onto the runner (the machine i ssh to) and the script lives in the repo which is triggered as part of the gitlab server build script. – Rob Nordberg Jan 15 '17 at 23:12
  • @thatotherguy i don't understand that either. I commented out all the other logic in the script and the snippet i posted always recreates the issue – Rob Nordberg Jan 15 '17 at 23:13

2 Answers2

2

Add a / after the path to specify you want to move the file, not rename the directory.

You should try this:

mv $PWD/\*  /home/user/directory/
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Ziyadsk
  • 381
  • 4
  • 8
0

Are your variables properly quoted? You could try :

ls "$PWD" #ensure the files are there
mv "$PWD"/* "/company/home/directory"
ls "/company/home/directory" #ensure the files are moved

If any of your file or directory names contains characters such as spaces or tabs, your "mv" command may not be seeing the argument list you think it is seeing.

Fred
  • 6,590
  • 9
  • 20
  • If quoting is the issue, `ls` should have the same problem as `mv`. – chepner Jan 15 '17 at 23:51
  • The quoting shouldn't matter unless outputting that right? Anyways, I tried quoting the variable but no luck. – Rob Nordberg Jan 15 '17 at 23:59
  • 1
    Quoting is always important. Suppose you have a directory named "/my directory" (with a space). trying "mv somefile /my directory" will not work. Even if this has nothing to do with your current problem, you should always quote variables containing filenames when expanding them. The "ls" command has no globbing, the "mv" does, so nothing says both should have the same issue. – Fred Jan 16 '17 at 00:07