Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying files (not directory)

Status
Not open for further replies.

MCubitt

Programmer
Mar 14, 2002
1,081
0
0
GB
Take this structure:
/data/files/backup

I want to copy the files inside /data/files
to /data/files/backup

If I use
cp /data/files/* /data/files/backup
UNIX complains that the backup is a directory so I should use -r or -R

If I include -r or -R it complains it is a recursive copy.

How do I tell it to copy only FILES?

 
If you cd into /data/files and then do

cp -p * backup

it will work. you could also do a find command, but that's not really necessary.

the -p in the cp command preserves the ownership, permissions and timestamp of the files you are copying. sometimes that is very important. If you don't use the -p, the timestamp will change, permissions may change, and if you aren't the owner of the files, so will the ownership.
 
I am in the source directory and enter cp -p * backup, the output:
$ cp -p * backup
cp: 0653-436 backup is a directory.
Specify -r or -R to copy.


which is the same result as specifying the source diretcory. I think the message is purely a warning and the reason the system appears to "hang" is that one of the files (I just discovered) is rather large! So it is still copying.

The -p flag was good to know so not a wated post. Thanks bi.

 
An example using 'find' and 'cpio' would be something like

find ./ -type f | cpio -puvd ./backup


But I can't help feeling this is over-egging the pudding. Works though...

LHLTech

IBM Certified ATE
 
You can also use this command

ls -et | grep "^-" | awk '{print$9}' | xargs -t -I {} cp {} backup

By using this command only the files will copy into backup directory.


Khalid Mahmood
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top