Solved

Check if remote files exist (shell script)

Posted on 2008-10-20
5
2,966 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

734 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