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 7700253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:39:57+00:00 2026-05-31T22:39:57+00:00

Basically I want to move files to another server on creation preserving the directory

  • 0

Basically I want to move files to another server on creation preserving the directory structure. I have a solution put it lacks elegance. Also I feel like I’m missing the obvious answer, so thanks in advance for your help and I totally understand if this bores you.

The situation

I have server with limited disk space (let’s call it ‘Tiny’) and a storage server. Tiny creates files every once in a while. I want to store them automatically on the storage server and remove the originals when it’s safe. I have to retain the directory structure of tiny. I don’t know in advance how the dir structure looks like. That is, all files are created in the directory /some/dir/ but sudirectories of this are created on the fly. They should be sotred in /other/fold/ on the storege server preserving the substcrutre under /some/dir. E.g:

/some/dir/bla/foo/bar/snap_001a on tiny —> becomes /other/fold/bla/foo/bar/snap_001a on the storage server. They are all called snap_xxxx wgere xxxx is a four letter alphanumeric string.

My old solution

Now I was thinking to loop over files and scp them. Once scp is finished and returns without error the files on tiny are removed with rm.

#!/bin/bash
# This is invoked by a cronjob ever once in a while.

files=$(find /some/dir/ -name snap_*)

IFS='
'
for current in $files; do
   name=$(basename $current)             # Get base name (i.e. strip directory)
   dir=$(dirname $current)               # Get the directory name of the curent file on tiny
   dir=${dir/\/some\/dir/\/other\/fold}  # Replace the directory root on tiny with the root on the storage server
   ssh -i keyfile myuser@storage.server.net \
     mkidir -p $dir                      # create the directory on the storage server and all parents if needed
   scp -i keyfile $current myuser@storage.server.net:$dir$name \
     && rm $current                      # remove files on success
done

This however strikes me as unnecssarily complicated and maybe error prone. I thought of rsync but when coping single files, there is no option to create a directory and it’s parents if they don’t exist. Does anyone have an idea, better than mine?

What I ended up using after this thread

rsync -av --remove-sent-files --prune-empty-dirs          \
      -e 'ssh -i /full/path/to/keyfile'                   \
      --include="*/" --include="snap_*" --exclude="*"     \
      /some/dir/ myuser@storage.server.com:/other/fold/

More recent versions then the one I was using take --remove-source-files instead of --remove-sent-files. The former being more of a telling name in that it’s clearer what files are deleted. Also --dry-run is a good option to test your parameters BEFORE actually using rsync.

Thanks to Alex Howansky for the solution and to Douglas Leeder for caring!

  • 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-05-31T22:39:58+00:00Added an answer on May 31, 2026 at 10:39 pm

    How do I tell rsync just to copy the snap_xxxx files?

    See the –include option.

    How do I change the direcotry root?

    Just specify it on the command line.

    rsync [options] source_dir dest_host:dest_dir
    

    How do I delete the originals on Tiny after transfer to the storage server?

    See the –remove-source-files option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to bulk move a large number of files from one directory to
I have a large multidimensional array and I basically want to drop the first
I have an SPListItemCollection. I basically want to get one of the items (randomly)
On localhost. I have the following directory structure: /share/www/trunk/wp-content/plugins/otherfolders /share/www/portfolio/wp-content/symlink Where symlink is a
Basically I want my HTML structure to be restructured when window gets smaller than
Okay so I have two png files, a circle and a maze. Basically the
I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
I basically want to be able to deploy multiple versions of the same EAR
I basically want to make things easier by just looping LinkButtons instead of making
I basically want to open a browser window from Word using VBA that does

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.