How do I copy a directory while excluding a specific subdirectory?

0
1
Asked By CoolBean123 On

Hey everyone! I'm trying to copy a directory but want to exclude just one specific subdirectory during that process. I've got this alias set up: `fecha="cp -r ../parcial/ ./$(date +%y%m%d)"` where `dir/` is `../parcial/` and the subdirectory I want to exclude is called `some_subdir_name/`. Any advice on how I can make this happen? Thanks in advance!

4 Answers

Answered By BashWhiz On

For a quick method using `cp`, you can try globbing with an exclamation mark: `cp -r mydir/!(some_subdir_name) todir`. Just make sure you have `extglob` enabled in your shell session. This will copy everything except the excluded directory.

Answered By FindRanger On

Another option is to use the `find` command with `cp`. You can write something like this: `find ../parcial/ -type d -name 'some_subdir_name' -prune -o -exec cp -r {} ./$(date +%y%m%d) ;`. This will find everything except the named directory and copy it over.

Answered By CleverCoder88 On

Definitely go for `rsync` if you can! If not, `cp` isn't going to work for excluding directories directly unless you use those extra steps. Just depends on your exact needs.

Answered By TechGuru89 On

You might want to check out `rsync` for this! It's super handy for copying files and can exclude directories easily. For example, you can use the command: `rsync -aq --exclude 'some_subdir_name/' ../parcial/ ./$(date +%y%m%d)` This copies everything from `../parcial/` to a new folder with the current date, excluding just the `some_subdir_name/`. The `-a` flag preserves the attributes and `-q` runs it quietly.

SimpleUser77 -

Yes! I’m a huge fan of `rsync` too. It makes things so much easier in situations like this!

Not_a_RsyncFan -

I see your point about `rsync`, but I need to stick to `cp` for this since it's part of an alias I'm using.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.