# What is the closest thing to xcopy in Ubuntu



## ChristianComputerHelp.org (Jan 7, 2015)

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


----------



## Blue-Knight (Jan 7, 2015)

ChristianComputerHelp.org said:


> 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?




```
cp -r -u /example/folder/*.txt /example/destination
```

if target folder may not exist:

```
mkdir -p /example/destination && cp -r -u /example/folder/*.txt /example/destination
```
Not sure.


----------



## blobster21 (Jan 7, 2015)

Additionnaly, don't forget to set the path to the command interpreter with the shebang at the beginning of your script

```
!#/bin/bash
```

and make this script executable


```
chmod +x your_script.sh
```


----------



## Blue-Knight (Jan 7, 2015)

blobster21 said:


> !#/bin/bash




```
#!/bin/bash
```
 ??


----------



## blobster21 (Jan 7, 2015)

you fixed it for me, thanks


----------



## ChristianComputerHelp.org (Jan 7, 2015)

So if I write the whole thing would that be


```
#!/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


----------



## Blue-Knight (Jan 8, 2015)

ChristianComputerHelp.org said:


> So if I write the whole thing would that be
> 
> 
> ```
> ...


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:

```
#!/bin/bash
mkdir -p /example/destination && cp -r -u /example/folder/*.txt /example/destination
```


----------



## ChristianComputerHelp.org (Jan 8, 2015)

Thank you

Does this do subfolders?

Rick


----------



## Aquinus (Jan 8, 2015)

You could also use rsync to do the same thing.


```
#!/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.


----------



## Blue-Knight (Jan 8, 2015)

ChristianComputerHelp.org said:


> Does this do subfolders?


Ops, I think I misunderstood something...


----------



## Aquinus (Jan 8, 2015)

ChristianComputerHelp.org said:


> 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:

```
#!/bin/bash
mkdir -p /example/destination && rsync -av /example/folder/*.txt /example/destination
```


----------



## Easy Rhino (Jan 8, 2015)

rsync is the recommended way of handling that situation.


----------



## Blue-Knight (Jan 8, 2015)

ChristianComputerHelp.org said:


> 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!? 

*EDIT:*
I think the OP wanted something that would work like this instead!?

```
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).



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.


----------



## ChristianComputerHelp.org (Jan 8, 2015)

Blue-Knight said:


> 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!?
> 
> ...




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


----------



## Cybrnook2002 (Jan 8, 2015)

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.


----------

