943,771 Members | Top Members by Rank

Ad:
Aug 8th, 2006
2

XCOPY Alternative in UNIX

Expand Post »
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
Similar Threads
Reputation Points: 166
Solved Threads: 0
Junior Poster in Training
Shane_Warne is offline Offline
59 posts
since Jul 2006
Aug 8th, 2006
1

Re: XCOPY Alternative in UNIX

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
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Aug 9th, 2006
1

Re: XCOPY Alternative in UNIX

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.

Shell Scripting Syntax (Toggle Plain Text)
  1. SomeFolder
  2. |
  3. +--Source
  4. |
  5. +--Dest
if you type
Shell Scripting Syntax (Toggle Plain Text)
  1. 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.
Shell Scripting Syntax (Toggle Plain Text)
  1. $ cp -R *.h ../temp
  2. cp: cannot stat `*.h': No such file or directory
  3.  
Is there any way i can do that in bash?
Reputation Points: 166
Solved Threads: 0
Junior Poster in Training
Shane_Warne is offline Offline
59 posts
since Jul 2006
Aug 9th, 2006
0

Re: XCOPY Alternative in UNIX

Quote ...
Shell Scripting Syntax (Toggle Plain Text)
  1. $ cp -R *.h ../temp
  2. cp: cannot stat `*.h': No such file or directory
  3.  
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
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Sep 8th, 2006
1

Re: XCOPY Alternative in UNIX

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.

Shell Scripting Syntax (Toggle Plain Text)
  1. ├─dest
  2. └─source
  3. │--test1.h
  4. └─child
  5. └─test2.h
The above command


Shell Scripting Syntax (Toggle Plain Text)
  1. cp -r source/*.h dest
doesnt copy the test2.h file in the child directory. Only the test1.h is copied.
Reputation Points: 166
Solved Threads: 0
Junior Poster in Training
Shane_Warne is offline Offline
59 posts
since Jul 2006
Sep 9th, 2006
1

Re: XCOPY Alternative in UNIX

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).
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Sep 9th, 2006
1

Re: XCOPY Alternative in UNIX

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.
Reputation Points: 166
Solved Threads: 0
Junior Poster in Training
Shane_Warne is offline Offline
59 posts
since Jul 2006
Sep 11th, 2006
1

Re: XCOPY Alternative in UNIX

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. # we want to copy from path1 to path2
  3. # step 1 make all the subdirectories
  4. find /path1 -type d | \
  5. while read dir
  6. do
  7. mkdir /path2"${dir#/path1}"
  8. done
  9. # step 2 cp the files
  10. find /path1 -type f | \
  11. while read $file
  12. do
  13. cp "$file" /path2"${file#/path1}"
  14. done
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Sep 11th, 2006
1

Re: XCOPY Alternative in UNIX

Shell Scripting Syntax (Toggle Plain Text)
  1. tar -cvf - $(find . -name "*.h") | (cd ../dest ; tar -xvf -)
sut
Reputation Points: 20
Solved Threads: 1
Light Poster
sut is offline Offline
30 posts
since Sep 2006
Jun 18th, 2009
0

Re: XCOPY Alternative in UNIX

find . -name "*.h" -print0 | cpio --null -pvd new-dir
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bianxi is offline Offline
1 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Shell Script That Acts Like Dos
Next Thread in Shell Scripting Forum Timeline: Replacing a Line feed with a new line itself.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC