Can cp replace newer files with older files recursively?
Can cp replace newer files with older files recursively (including .* folders) when logged in as root? (old replace new)
First I create new files in both local and remote folders: Code:
a@a-NC210-NC110:~/Documents$ touch local/{1.txt,2.config,3.ini,abcd,efgh.txt,DoNotDelete.me} Code:
a@a-NC210-NC110:~/Documents$ ls -l local/ Now I attempt to copy over the older choice contents from the directory remote into the target directory local replacing newer files. (old replace new) ATTEMPT 01: remote/ Code:
a@a-NC210-NC110:~/Documents$ cp -ap remote/ local ATTEMPT 02: remote/* Code:
a@a-NC210-NC110:~/Documents$ cp -ap remote/* local Unfortunately the /* on the end of "remote/*" probably means we can't execute this recursively. Let's find out. ATTEMPT 03: Same as Attempt02 but with recursion. For Attempt 03, first let me make the complex directory structure below, with identical modification times as above: Code:
a@a-NC210-NC110:~/Documents$ ls -l local/b/3/ii/local/ local/b/3/ii/mozilla/ Code:
a@a-NC210-NC110:~/Documents$ cp -ap remote/* local Everyone probably wants to know why would anyone want to replace newer files with older files? Those who want to to restore previously saved configuration files onto a freshly installed OS. Since the OS (local) drive has just been installed, most mod times will be the time the system was installed. Thus you can't use rsync -au since the option "u" will only replace older files with newer files, and you can't use rsync -a because rsync will see that the file already exists and thus skip it(saving BIG time). ATTEMPT 04: hidden files, logged in as root Now let me apply this to updating the config files in my /home/a directory when logged in as root: Code:
root@a-NC210-NC110:/home/a$ sudo su All of a sudden, the "source/*" method does not work! Why? Does it have to do with running it from root? Is it because of the config files mostly are located in folders that start with "."? The program I am working on must be run from root, so there is no choice. What can I do? |
If you want cp to copy files recursively, you need the -r option.
|
Wouldn't the "-r" option be included in "-a"?
Quote:
Code:
cp manual pages extract: |
For some reason I was confusing cp with something else, and thinking -R and -r were different. Sorry for not checking that before posting. I don't actually use cp very often, preferring rsync most of the time unless it's just a file or two.
Wouldn't it be easier to remove the newer files first, and then not worry about overwrites, or is that a problem? I'm not sure I completely understand your situation. |
Quote:
Code:
$ mkdir remote; touch remote/file{1,2} |
isn't that a case for a dedicated application, like e.g. unison (or rsync)?
|
remove first?
Quote:
Code:
a@a-NC210-NC110:/media/a/Sea_ext4/...$ find /media/a/backupDrive/home/a | wc -l Code:
a@a-NC210-NC110:/media/a/Sea_ext4/...$ find /home/a | wc -l Quote:
Maybe something like: Code:
echo $(find /media/a/backupDrive/home/a) | xargs rm -rf /home/a/{__?__}; |
My apologies...
Quote:
My Attempt 01 redone: Code:
a@a-NC210-NC110:~/Documents$ ls -l local remote Quote:
I guess my cli skills just aren't there yet. |
Why cp? You should be using rsync which provides a wide array of options (including copy only if newer). cp is good for basic copy operations but it's not good at operations where copying under specific conditions is required. It's basically going to always copy.
rsync is better at what you desire. For example, rsync -ruptv /src/ /dst/ See the rsync man page for explanation of options. |
Purpose: remote/old files replace local/new files
Quote:
I always have used rsync to do the opposite: replace older files with newer files. How can rsync/unison be used to replace newer files with older ones? Nota Bene: Quote:
Since the OS (local) drive has just been installed, most mod times will be the time the system was installed(just now), thus newer than the config files you have saved on the remote file. |
Please verify rsync can replace newer with older.
Quote:
Replacing newer files with older ones is the opposite of your clause above "(including copy only if newer)". In my situation, I am trying to copy over configurations from a previous installation to a system THAT WAS JUST INSTALLED. Since the OS (local) drive has just been installed, most mod times will be the time the system was installed. That is, the local OS's /home/a config files(No Configuration) will be newer than the remote hard drive's /home/a's config files (Desired Configuration). Since the desired config files are OLDER than the newer config files, therefore you can't use rsync -au since the option "u" will only replace older files with newer files, and you can't use rsync -a because rsync will see that the file already exists and thus skip it. I always have used rsync to do the opposite: replace older files with newer files. May I ask, are you sure that rsync be used to replace newer files with older ones? |
Quote:
Code:
$ ls /tmp/xxx And yes, rsync is quite willing to replace a newer file with an older one. In fact, you have to go out of your way to tell it not to do that ("-u" or "--update" option for "skip files that are newer on the receiver"). |
Core question
Quote:
|
Quote:
Code:
cp -a remote/.[^.]* remote/* destination |
preconceived notion exposed
Quote:
As usual, when learning commands it is significantly difficult to understand every little nuance for each manual page from the start. Thanks for exposing my preconceived notion and I'll keep improving my ability to read the manual pages closer and closer. I'll go do some experiments and then post my results. |
All times are GMT -5. The time now is 06:37 PM. |