• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

What is the closest thing to xcopy in Ubuntu

Joined
Jan 7, 2015
Messages
4 (0.00/day)
Location
Honolulu Hawaii
Let's say I want to do this with a batch file

xcopy c:\*.txt m:\ /s/d/e/y/i

What would be the closest thing to an Ubuntu script?

First time poster,

Rick
 
Let's say I want to do this with a batch file

xcopy c:\*.txt m:\ /s/d/e/y/i

What would be the closest thing to an Ubuntu script?
Code:
cp -r -u /example/folder/*.txt /example/destination

if target folder may not exist:
Code:
mkdir -p /example/destination && cp -r -u /example/folder/*.txt /example/destination
Not sure.
 
Last edited:
Additionnaly, don't forget to set the path to the command interpreter with the shebang at the beginning of your script
Code:
!#/bin/bash

and make this script executable

Code:
chmod +x your_script.sh
 
So if I write the whole thing would that be

Code:
#!/bin/bash
chmod +x your_script.sh
mkdir -p /example/destination && cp -r -u /example/folder/*.txt /example/destination

I love batch files and just getting into the geekier side of Ubuntu. Thank you all for your patients and help.

Rick
 
So if I write the whole thing would that be

Code:
#!/bin/bash
chmod +x your_script.sh
mkdir -p /example/destination && cp -r -u /example/folder/*.txt /example/destination

I love batch files and just getting into the geekier side of Ubuntu. Thank you all for your patients and help.

Rick
No. That "chmod +x your_script.sh" is to be run externally by you, not to be included in your script.

That is to make your script executable.

Your script would look like this:
Code:
#!/bin/bash
mkdir -p /example/destination && cp -r -u /example/folder/*.txt /example/destination
 
You could also use rsync to do the same thing.

Code:
#!/bin/bash
rsync -avd /example/folder/*.txt /example/destination

The -d flag actually isn't necessary (create directory if it doesn't exist) since it's implied, but I'm being specific because of the prior examples.
 
Does this do subfolders?
The -r in cp and -a in rsync handles recursively going through sub-directories, so yes.

Edit: The -d flag on rsync doesn't do that. One sec.

Edit 2: Yeah, you still need the mkdir:
Code:
#!/bin/bash
mkdir -p /example/destination && rsync -av /example/folder/*.txt /example/destination
 
Last edited:
rsync is the recommended way of handling that situation.
 
Does this do subfolders?
I am a bit lost. Because here no commands are working recursively.

When I type "/dir/*.txt" on the command line it will include all files within that directory only, not recursively (e.g. rsync /dir/0.txt /dir/1.txt /dir/2.txt /dest). And the program is not going to check recursively as well because there is no folder given as option!?

And I think the OP wants other thing. Or no!? :confused:

EDIT:
I think the OP wanted something that would work like this instead!?
Code:
find . -type f -name "*.txt" -exec cp -u --parents {} /example/destination \;

Note: You have to execute this where the root of your "*.txt" files are to work as intended (e.g. ./copy.sh).

:confused:

That worked like this:
Source: All "*.txt" from a folder and its sub folders.
Destination: Another folder on another disk with the exact same directory structure but only those with ".txt" files.

I guess that was what the OP intended. Still not sure. :confused:
 
Last edited:
I am a bit lost. Because here no commands are working recursively.

When I type "/dir/*.txt" on the command line it will include all files within that directory only, not recursively (e.g. rsync /dir/0.txt /dir/1.txt /dir/2.txt /dest). And the program is not going to check recursively as well because there is no folder given as option!?

And I think the OP wants other thing. Or no!? :confused:

EDIT:
I think the OP wanted something that would work like this instead!?
Code:
find . -type f -name "*.txt" -exec cp -u --parents {} /example/destination \;

Note: You have to execute this where the root of your "*.txt" files are to work as intended (e.g. ./copy.sh).

:confused:

That worked like this:
Source: All "*.txt" from a folder and its sub folders.
Destination: Another folder on another disk with the exact same directory structure but only those with ".txt" files.

I guess that was what the OP intended. Still not sure. :confused:


Yes that was it, just an example so I can start my geeky experimentation :)

As soon as my slightly ADHD brain locks onto this I'll gain some progress.

Thanks,

Rick
 
can also just do an easy cp -pr or cp -pR if it complains about lower case "r". That is copy retaining permissions and scan recursively.

so:

cp -pr /path/to/source /path/to/target

Make sure your target path exists, if not, then mkdir /path/to/target

chmod (-r for recursively)777, 755, etc... to set your permissions on directory tree

Also make sure to check your ulimits as well ulimit-a to make sure you don't have any file size restraints. (I know I run into that on AIX in enterprise env. Home distros might be wide open to begin with).

Get familiar with grep too, grep is a nice little find tool.
 
Back
Top