Help regarding a shell script

Hi,

Below script is grabbing the  Sub directories from day before yesterday.

Can anyone  help me how to perform the same thing by logging into a different server (like ssh)


#!/bin/bash


cd  '/usr/HOME/TEST';

find *  -maxdepth 1 -mindepth 1  -type d   -ctime 2 >/usr/HOME/LOGS/out.txt ;

Open in new window

new_perl_userAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xtermCommented:
Not sure if I'm reading your question correctly, but if you just want to run that same operation on a new server, then simply log in via ssh to the new server and type in line 4 above, hit enter, and then type in line 6 above, and hit enter.

Alternately, you can use a text editor to create a new file, and paste the contents of that script into the new file (let's call it myscript.sh)
Then you would need to make the file executable (chmod u+x myscript.sh)
Then you can just run it (./myscript.sh)

At any rate, let me know if you're asking something different that I'm missing, and I'll clarify
0
new_perl_userAuthor Commented:
Hi,

For example I am running this script on server x , now I want to run the script still on server x but what this script should do is ssh to server y  execute the find command and write to a log file.

I was trying this but it is not working..
 
#!/bin/bash

ssh servery@140.12.256.13

cd  '/usr/DATA/FILES';

find *  -maxdepth 1 -mindepth 1  -type d   -ctime 2 >/usr/HOME/LOGS/out.txt ;

Open in new window

0
xtermCommented:
Oh, I see.

Alright, if you want to write the log file on server y where you're running the command then its really easy:

ssh myuser@server_y "cd /usr/DATA/files;  find *  -maxdepth 1 -mindepth 1  -type d   -ctime 2 >/usr/HOME/LOGS/out.txt"

That will ssh as user myuser to host server_y and run the command specified, and put the output file in /usr/HOME/LOGS/out.txt on server_y

If you want the output to be written to server_x (the one you're coming from) then do:

ssh myuser@server_y "cd /usr/DATA/files;  find *  -maxdepth 1 -mindepth 1  -type d   -ctime 2" > /tmp/out.txt

Notice, the location of the quotes is the difference.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

new_perl_userAuthor Commented:
Hi,

I tried the below format and it is erring out as :

FIND: Parameter format not correct
0
xtermCommented:
Change the "*" to a "." - I just copied the command from your original post even though it looked wrong from the start.

0
xtermCommented:
Another question though, what operating system is server_y running?  If its not the GNU find (as you'd see on pretty much any Linux box) then it probably wouldn't know the maxdepth/mindepth options.
0
new_perl_userAuthor Commented:
Still the same error

ssh testbox@140.12.256.13 "cd /usr/HOME/FILES;
find .  -maxdepth 1 -mindepth 1  -type d   -ctime 2" >/usr/HOME/LOGS/out.txt ;
0
omarfaridCommented:
are you trying to collect the output on one server or you want it on the remote server? Also, do you want it to run without prompting for password?

For 2nd question, you can set trust between the two accounts on the two systems by exchanging ssh keys

for the 1st question, you may do

#!/bin/bash
ssh servery@140.12.256.13 "cd  /usr/DATA/FILES
find *  -maxdepth 1 -mindepth 1  -type d   -ctime 2 > /usr/HOME/LOGS/out.txt 2> /dev/null"
0
xtermCommented:
Please paste the output of "uname -a" from server_y.

BTW, the IP address you have in your command is invalid - you can't have a "256" in an IP address... not sure if that was just a typo posting here.
0
new_perl_userAuthor Commented:
I am collecting the output on the server where I am running the script not on the remote server where I am executing the find command.

Tried the above code and  the error was:

bash: line 1:/usr/HOME/LOGS/out.txt: No such file or directory
0
new_perl_userAuthor Commented:
The  above  ip address is just for an example
0
xtermCommented:
Use my original example from above and redirect it to /tmp/out.txt - the path /usr/HOME/LOGS obviously doesn't exist on your server.

So:

ssh testbox@server_y "cd /usr/DATA/files;  find .  -maxdepth 1 -mindepth 1  -type d   -ctime 2" > /tmp/out.txt

Then look in /tmp for your output file.
0
new_perl_userAuthor Commented:
I used the same above format. It is creating the Output file in tmp location . But no data is being written to it because it is showing an error as :

FIND: Parameter format not correct  

I changed from * to .
0
xtermCommented:
Please run "uname -a" on server_y and paste the output here - the remote server may have a different OS with a different version of the find command, if that is even possible - the output of your find error looks like the remote ssh server might be some kind of windows/cygwin server - is that accurate?
0
new_perl_userAuthor Commented:
cool for your hint it worked.
You  are right that server is a windows/cygwin kind of thing and it is trying windows find.

I changed the find from find  to /usr/bin/find and it worked.

Thanks all for the help.
0
xtermCommented:
awesome, you're welcome.  Enjoy.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.