0

I need to make a copy of the content of one directory to another.

I need to copy only 1 sub-folder.

For example; if I have a folder called "from" and want to copy one of it's sub-folders (with all the stuff inside it) that's called "21-01-2015" <- this name changes every day, and I want to execute this via cronjob, to folder "to"

+ from
    - 21-01-2015 (**this name changes all the time**)
    - month (**this stays always the same**)

I'm currently using this command: cp -a /path/from /path/to but this command does a copy of folder "from" along with all the stuff within it...

so when you're inside folder "to" it becomes something like this:

+ to
    + from
        - 21-01-2015
        - month

instead of how I need it which is

+ to
    - 21-01-2015

how can I achieve something like this?

thank you

  • Use a wildcard that holds all the directories you need and excludes home. Possible examples: cp -a /path/from/??-??-??? /path/to/ or if you want to actually test on the digits: cp -a /path/from/[0-3][0-9]-[0-1][0-2]-[0-9][0-9][0-9][0-9] /path/to/. – Rinzwind Jan 21 '15 at 21:59
  • @KasiyA it'll NOT execute the cronjob the same date as the date of the folder name – user4480106 Jan 21 '15 at 22:11
  • 1
    You asked a duplicate of this question on stackoverflow. Why ask again here? Was the answer that you marked as "accepted" on stackoverflow not adequate? – John1024 Jan 21 '15 at 22:15
0

cp -a /path/from/* /path/to will copy the contents of /path/from, not the directory if you have other files/dirs in /path/from, and only want to copy the one, you'll have to come up with a pattern to match it.

  • yep, but as I said... I need to run a cronjob... and the name of the folder I need to copy changes everyday. so basically, what I need is to copy everything except "month"... – user4480106 Jan 21 '15 at 22:18
0

You can try this command:

cp -a from/$(ls from | grep ^[0-9][0-9]) to/
0

the problem with the command cp -a /path/from /path/to is that it will do a recursive copy which means it will copy the source directory (in your case "/path/from") and everything beneath it.

what you want is a command to recursively copy the "21-01-2015" directory within "from". The command you should be using is:

cp -a /path/from/21-01-2015 /path/to

Note that you mentioned that the directory within "from" changes names. You have not mentioned whether or not the directory from the previous job remains behind. If the /path/from contains multiple subdirectories and you use a wildcard in your command you will copy over all the directories you have already copied (this may or may not be what you want). If you dont care about copying over subdirectories you have already copied in a previous cron job then you could use the following command to copy any file or folder that starts with a number (this will of course NOT copy "month")

cp -a /path/from/[0-9]* /path/to

If you do not want to copy over every sub-directory of "from" each time the cronjob runs (i.e. you just want to copy over the files that have been added since last cronjob) you would be better served using rsync. The rsync command you would use would be something similar to:

rsync -avz --exclude 'month' /path/from/ /path/to/

but please check the man page for the correct options to do exactly what you want (e.g. you possibly dont want verbose on in production).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.