Hi all,
Can you please tell me if there is a bash command in UNIX that is similar in functionality to XCOPY in Windows? XCOPY can copy who directory trees from a source folder to a destination folder, but from what I saw about the cp command in bash, it is not possible. Also XCOPy can copy only new files, and returns if files were copied or not. I would like if the bash command did that as well. Here is the XCOPY functional specs for further reference. You can find if files had been copied or not by looking at the XCOPY return codes.

Thanks in advance.
Shane

WolfPack commented: Good Question +3

Recommended Answers

All 10 Replies

Obviously you haven't looked very carefully at the man page.
Copying directory trees (recursive mode):
cp -r
Copying directory trees, viewing all files copied (verbose mode):
cp -rv

You can string any of the cp options together, like that.
http://linux.about.com/od/commands/l/blcmdl1_cp.htm

oh. sorry I missed something in my question. I wanted to selectively copy the files in the source directory into the destination directory. Say for example all the .h files and .c files. XCOPY can do this by using the /S switch.

SomeFolder
|
+--Source
|
+--Dest

if you type

XCOPY /S *.h ..\dest

it will recursively copy the .h files in source to dest while retaining the directory structure.

I tried it with cp but it didnt do that.

$ cp -R *.h ../temp
cp: cannot stat `*.h': No such file or directory

Is there any way i can do that in bash?

$ cp -R *.h ../temp
cp: cannot stat `*.h': No such file or directory

You need a path. You can't just put "*.h". You need either to use working directories, or the full path:
cp -r somefolder/*.h ../someotherfolder

I am afraid that doesn't work. Only the files in the somefolder is copied to the destination. Recursive seraching is not done. Can you please try the above command and see if my observation is correct.
Create a folder like this.

├─dest
└─source
    │--test1.h
    │
    └─child
           └─test2.h

The above command

cp -r source/*.h dest

doesnt copy the test2.h file in the child directory. Only the test1.h is copied.

The reason cp -r source/*.h is not working is because you've only told it to look at .h files. For that reason, it doesn't copy the child directory (since it's not a .h file itself), much less its contents (though they may be .h files).

So how do I tell to look for .h files in child folders as well? I want to copy them so that the directory structure is the same as the source directory structure. I hope you understand what I am trying to do here. I only want the shell command please. I have looked the documentation of cp and tried all combinations. So if you know the exact answer please reply.

#!/bin/bash
# we want to copy from path1 to path2 
# step 1 make all the subdirectories
find /path1 -type d | \
while read dir 
do
    mkdir /path2"${dir#/path1}"
done
# step 2 cp the files
find /path1 -type f | \
while read $file
do
     cp "$file" /path2"${file#/path1}"
done
tar -cvf - $(find . -name "*.h") | (cd ../dest ; tar -xvf -)

find . -name "*.h" -print0 | cpio --null -pvd new-dir

Just very simple question:

what is the unix equivalent of the following command

xcopy s: d:/s/d

where s: source drive and d: destination drive, /s - subdirectories, /d - replace with newer date/time

This commands serves well as incremental backup which saves time and effort in DOS/Windows Era.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.