Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:59:02+00:00 2026-06-14T16:59:02+00:00

I’m writing a cron to backup some stuffs on a server. Basically I’m sending

  • 0

I’m writing a cron to backup some stuffs on a server.
Basically I’m sending specific files form a local directory using scp.
I’m using a public key to avoid authentication.

For reusability I’m passing the local directory and the server url by arguments to my bash script.

How I set my parameters:

#!/bin/bash

DIR="$1"
URL="$2"
FILES="$DIR*.ext"

My problem is about formatting the url.

Without formatting

How I send files to the server:

#!/bin/bash

for F in $FILEs
do
    scp $F $URL;

    if ssh $URL stat $(basename "$F")
    then
        rm $F
    else 
        echo "Fails to copy $F to $URL"
    fi
done

If I try to copy at user’s home on the server I do:

$ ~/backup /path/to/local/folder/ user@server.com:

If I try to copy at a specific directory on the server I do:

$ ~/backup /path/to/local/folder/ user@server.com:/path/to/remote/folder/

In all cases it gives me the well known error (and my custom echo):

ssh: Could not resolve hostname user@server.com: nodename nor [...]
Can't upload /path/to/local/folder/file.ext to user@server.com

And it works anyway (the file is copied). But that’s not a solution, cause as scp fails (seems to), the file is never deleted.

With formatting

I tried sending files using this method:

#!/bin/bash

for F in $FILES
do
    scp $F "$URL:"
done

I no longer get an error, and it works for copying at user’s home directory then deleting the local file:

$ ~/backup /path/to/local/folder/ user@server.com

But, of course, sending to a specific directory don’t work at all.

Finally

So I think that my first method is more appropriate, but how can I get rid of that error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:59:03+00:00Added an answer on June 14, 2026 at 4:59 pm

    Your mistake is that you can scp to user@server.com: but not ssh to it : you need to remove the trailing : character (and possible path after it). You can do it easily like this with bash parameter expansion :

    ssh "${URL%:*}" stat "$(basename "$F")"
    

    RECOMMENDATIONS

    • “USE MORE QUOTES!” They are vital. Also, learn the difference between ‘ and ” and `. See http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words
    • if you have spaces in filenames, your code will breaks things up. Better use while IFS= read -r line; do #stuff with $line; done < file.txt
    • See bash parameter expansion
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.