need to copy files from one folder to another and then then touch each file to copy the access/modified time

0

I cant use cp -p because it also copies the user which I don't want and --preserve doesn't work because AIX apparently doesn't support that option? I get an error. Anyways, I just want to do the following:

cp /folder/file/* /newfolder/file/*

Then use touch -r on each file to copy the modified/access time.

I am terrible with scripting so any help is appreciated.

LeperMessiah

Posted 2019-05-14T15:12:35.663

Reputation: 33

"I just want to do the following: cp /folder/file/* /newfolder/file/*" – Oh, I think you don't, especially if /newfolder/file/ is not empty. See this answer.

– Kamil Maciorowski – 2019-05-14T15:28:02.963

Answers

0

While I'm not familiar with AIX, it seems to support rsync, https://www.ibm.com/developerworks/aix/library/au-rsyncfamily/index.html. So no need to re-invent the wheel, use rsync with the -t option to preserve modified times. See the page https://linux.die.net/man/1/rsync

Note that I would avoid using the -a archive option as that includes too many attributes that it copies for you. So you might use something like rsync -rltvx /folder/file /newfolder

If you're thinking of using this as a backup solution, then you will want to consider what you want to happen with files that are deleted on the source side on purpose, vs ones that were deleted by accident. By default rsync does not delete any files in either source or destination. You can instruct it to delete files in the destination that do not exist in the source, creating a mirror of the source. However if this is your only copy of your data, you won't be protected against accidental deletions. There is also the problem of if a source file becomes corrupt somehow this can mirror to the copy before you realise, and you're in trouble again.

While I've seen (but never used) some posts, such as this, on using rsync to create a system of hard links to make a deduplicated backup repository of multiple 'snapshots' of your data, I personally enjoy the benefits of a more feature rich backup solution such as Borg Backup

BeowulfNode42

Posted 2019-05-14T15:12:35.663

Reputation: 1 629

Its a good thing I had a backup of the files already because apparently rsync moves the files? I tried that command and then deleted the files from my test folder only to find the originals were then gone. – LeperMessiah – 2019-05-14T16:51:25.423

@LeperMessiah that's unusual that it would move the files, you should check the help available from your rsync install to see what options it has and what its defaults are. rsync is normally a copy only tool. It does have some options to delete extra files in the destination that are not in the source, but they are not the defaults. – BeowulfNode42 – 2019-05-15T01:15:16.203

Well I must have been checked out or half asleep. I think I was in the wrong folder when I deleted the files. Anyways the rsync works like a charm now. – LeperMessiah – 2019-05-15T15:52:32.847

My only question now is this. To restore the files ONLY if the ones I backed up are newer than the ones in the original directory, do I use rsync -uv /backupfolder /originalfolder? – LeperMessiah – 2019-05-15T15:58:25.833

@LeperMessiah yes the -u option will update only files that are newer like you are imagining. However there are a few potential issues, 1) you will need to ensure the originalFolder does not have an existing file with a newer timestamp than the one you want to restore, and doing a full rsync may include files that were deleted on purpose, or make extra copies of files that were moved or renamed. – BeowulfNode42 – 2019-05-16T03:16:55.930