0

I've built a script for applying a patch to a server. It works great(ish) and makes backups of any files it overwrites using the --backup=t syntax. Now I need to move these backups as they are no longer ending .php but are ending instead .php.~1~ which means if you view the file via a web browser you'd get the plain text output of the php script

I can almost do this when logged on to the webserver fine using the following command

for I in `sudo find /path/to/web/root -type f \( -name "*.*~1~" \)` ; do sudo mv $I ~/$I`date +%Y%m%d` ; done

There's a slight issue that the find command returns the fullpath to the file and as such $I needs truncating after it's been used in the mv, but that's another issue and outside the scope of this question

The problem I'm having is that when I try to execute this command remotely I get the following error

find: command not found

date: command not found

To address concerns raised about the output of find not being suitable to iterate over I knocked up this quick test

findtest lukes$ ls web
1   1.~1~   2   2.~1~   3   4   6   6.~1~   7   8   9
findtest lukes$ for I in `sudo find web -type f \( -name "*.*~1~" \)` ; do sudo mv $I $I`date +%Y%m%d` ; done
findtest lukes$ ls web
1       2       3       6       7       9
1.~1~20140201   2.~1~20140201   4       6.~1~20140201   8
Luke
  • 3,481
  • 6
  • 39
  • 63

2 Answers2

1

Never use this syntax :

for var in in `cmd`; do ... ; done
for var in in $(cmd); do ... ; done

See this reminder for more details.

Otherwise, you don't have to use a loop to execute commands on each items returned by find.

find [...] -exec [commands] {} \;

Here is an example :

find /home/yourdir -name "*.txt" -exec cat {} \;

It's much better than :

for i in $(find /home/yourdir -name "*.txt"); do cat $i; done

Finally, could you give us the content of the following variables remotely ?

echo $PATH
echo $SHELL
Community
  • 1
  • 1
Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
0

Could be there's very little environment set up for the user you're ssh'ing into the remote host as. PATH may not be set, for some reason.

Try using full paths to find and date:

for I in `sudo /bin/find /path/to/web/root -type f \( -name "*.*~1~" \)` ; do sudo mv $I ~/$I`/bin/date +%Y%m%d` ; done

I'd have thought that 'mv' would be not found too though if missing PATH was the case here.

Liam Gretton
  • 370
  • 1
  • 8
  • I don't think it's the PATH as if I ssh into the web server as that user it works fine. I also tried looking up `which find ` and `which date` and tried specifying the fullpath like you've suggested. – Luke Feb 01 '14 at 15:12