System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

cp files only preserving timestamp

 
SOLVED
Go to solution
Piotr Kirklewski
Super Advisor

cp files only preserving timestamp

Hi there
I need to copy files from a directory which has some sub directories in it. The problem is that I need only files - no directories and only files of certain extension ( *.srf ). Also i need to preserve the original time-stamp.
How can this be accomplished ?
Jesus is the King
7 REPLIES
TwoProc
Honored Contributor

Re: cp files only preserving timestamp

I'm pretty sure tar will preserve times for you, if I remember correctly.

--------------------------------------
Example: From computer to another computer

On source computer
$>find /mysource -type f -name "*.srf" | xargs -i tar cvf myfiles.tar {}

Copy tar file to other computer, then extract from the tar file.
$> cd mydestination
$> tar xvf myfiles.tar
----------------------------------------
Example same computer, two diff directories
$> cd /mydestination
$>find /mysource -type f -name "*.srf" | xargs -i tar cvf - {} | tar xvf -


We are the people our parents warned us about --Jimmy Buffett
Randy Jones_3
Trusted Contributor

Re: cp files only preserving timestamp

If you do not want to look in the subdirectories for "srf" files to copy then all you need is

/bin/cp -p *.srf /destination/directory/

If you want srf files from the subdirectories too, then perhaps

/usr/bin/find /directory -type f -name "*.srf" -exec /bin/cp -p {} /destination \;
Steven Schweda
Honored Contributor

Re: cp files only preserving timestamp

> If you do not want [...]

> If you want [...]

If you described better exactly what you wish
to do, then we might avoid some of the
guessing. For example:

Where are (all) the files now?

Where would you like (all) the files to be?

Which information would you like preserved?
Matti_Kurkela
Honored Contributor
Solution

Re: cp files only preserving timestamp

On RHEL 5 and probably all equivalent or newer Linux distributions:

cp --preserve=timestamps /source/dir/*.srf /destination/dir/

Since there is no option "-r", directories will not be copied: the cp command will output "cp: omitting directory /source/dir/something.srf" if it finds a directory that would match the *.srf wildcard. But this is information only.

If there are more files than will fit on a single command line (i.e. the above command will produce a shell error message because wildcard expansion fails), a bit more is needed. For example:

Create a script /usr/local/bin/cpto, to allow specifying the copy destination first:
----
#!/bin/sh
DEST="$1"
shift
cp "$@" "$DEST"
----
chmod a+x /usr/local/bin/cpto

Then, perform the copy operation:

cd /source/dir
ls -l | grep '^-.*\.srf$' | awk '{print $9;}' | xargs /usr/local/bin/cpto /destination/dir

This is not limited by maximum command line length, like shell wildcard expansions are.

MK
MK
Piotr Kirklewski
Super Advisor

Re: cp files only preserving timestamp

"cp --preserve=timestamps /source/dir/*.srf /destination/dir/"

This will copy all the files and directories. I need to get only *.srf files from certain directory and it's sub directories preserving the original time-stamp.
Reagrds
Peter
Jesus is the King
TwoProc
Honored Contributor

Re: cp files only preserving timestamp

This command won't take you through the subdirectories, and neither will add the "-R" command to the mix for the cp command.

"cp --preserve=timestamps /source/dir/*.srf /destination/dir/"

Thus, you'll have to go back to the find|xargs|tar command I posted above. The reason I recommend tar is that it does preserve timestamps on recovery. See my perfectly working 4 point suggestion with options posted above. :)
We are the people our parents warned us about --Jimmy Buffett
Steven Schweda
Honored Contributor

Re: cp files only preserving timestamp

> See my perfectly working 4 point suggestion
> [...]

I'll see your 4-point suggestion and raise
you my 0-point request for a problem
statement which was clear enough that a
non-psychic could understand what the actual
task was.

For example, does "I need only files - no
directories" mean that all the files from
many source directories should end up in one
destination directory? It's a mystery which
only one person can resolve. And he's not
talking. (Or, if he is, then he's not saying
anything which I can understand. But my
opinion is apparently worthless.)