Solved

Check if remote files exist (shell script)

Posted on 2008-10-20
5
2,961 Views
Last Modified: 2013-12-26
Hello.

I've already searched, but found nothing...

I need a script that checks if remote file exists. Everything that I've found only relates to one particular file. I need to check if there are any files in a particular directory, that start by some string...

I've tried this, unsuccessfully:

if ssh -l "$USER" $POSM200 'ls "'/home/tplinux/SAPiDoc/export/wpubon/processado/WPUBON01*'" >/dev/null'; then
        echo "file '"/home/tplinux/SAPiDoc/export/wpubon/processado/WPUBON01*"' exists";
        exit;
else
        echo "file '"/home/tplinux/SAPiDoc/export/wpubon/processado/WPUBON01*"' doesn't exist";
fi

but I got the following error:
ls: /home/tplinux/SAPiDoc/export/wpubon/processado/WPUBON01*: No such file or directory
file '/home/tplinux/SAPiDoc/export/wpubon/processado/WPUBON01*' doesn't exist

The problem seems to be finding a remote command that accepts wildcards.

Any help would be appreciated.
0
Comment
Question by:rmdb
  • 3
5 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 22756817
Hi,
wildcards are not your problem.
You have way too much  ' s and  " s in your ssh command.
Leave them all out and escape your asterisk with a \.
w
 
0
 
LVL 48

Accepted Solution

by:
Tintin earned 50 total points
ID: 22763682
What you need is:
if ssh $USER@$POSM200 "ls /home/tplinux/SAPiDoc/export/wpubon/processado/WPUBON01*" >/dev/null 2>&1
then
    echo 'file /home/tplinux/SAPiDoc/export/wpubon/processado/WPUBON01* exists'
    exit
else
    echo 'file /home/tplinux/SAPiDoc/export/wpubon/processado/WPUBON01* does not exist'
fi

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 22792313
Any reason for the B grade?
0
 
LVL 1

Author Comment

by:rmdb
ID: 22795949
Hi,

Sorry, no reason at all. I was filling the "accept solution", and put B instead of A, I was distracted... Any chance to change it?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22800021
You have to put an admin request in.  
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question