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
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.
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.
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.
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.
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.
Yes! I’m a huge fan of `rsync` too. It makes things so much easier in situations like this!