Solved

Check if remote files exist (shell script)

Posted on 2008-10-20
5
2,952 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
When it comes to security, there are always trade-offs between security and convenience/ease of administration. This article examines some of the main pros and cons of using key authentication vs password authentication for hosting an SFTP server.
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…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now