How to use 'cp' command to exclude a specific directory?
rsync
is fast and easy:
rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude
You can use --exclude
multiples times.
rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude
Note that the dir thefoldertoexclude
after --exclude
option is relative to the sourcefolder
, i.e., sourcefolder/thefoldertoexclude
.
Also you can add -n
for dry run to see what will be copied before performing real operation, and if everything is ok, remove -n
from command line.
Exclude directory while copy directory
You could exclude the directories as part of find results before the copy, but using rsync
or cp
with the '!'
support enabled as suggested by Sathiya is a much simpler solution.
See find
example below:
find /root/tmp/ -mindepth 1 -maxdepth 1 -type d ! -regex '\(.*a\|.*b\)' -exec cp -r {} /root/tmp1/ \;
Copy whole directory but exclude all folders and subfolders with certain name
Instead of cp
, you can use tar
with option --exclude
to control what you want copied or not.
The full command is:
tar --exclude="outdir" -cvpf - . | (cd TARGET_DIRECTORY; tar -xpf -)
- So any path that contains the "outdir" pattern will be excluded.
- Without the
--exclude
option, it will copy the entire structure of your current directory under TARGET_DIRECTORY. - You can replace the
.
in the first tar by your desired source directory.
cp dir recursivly excluding 2 subdirs
Maybe the find
command will help you:
$ find /home/directory -mindepth 1 -maxdepth 1 -name 'subdirectory[57]' -or -exec cp -r {} /path/to/dir \;
Copy folder recursively, excluding some folders
Use rsync:
rsync -av --exclude='path1/to/exclude' --exclude='path2/to/exclude' source destination
Note that using source
and source/
are different. A trailing slash means to copy the contents of the folder source
into destination
. Without the trailing slash, it means copy the folder source
into destination
.
Alternatively, if you have lots of directories (or files) to exclude, you can use --exclude-from=FILE
, where FILE
is the name of a file containing files or directories to exclude.
--exclude
may also contain wildcards, such as --exclude=*/.svn*
Shell command to tar directory excluding certain files/folders
You can have multiple exclude options for tar so
$ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .
etc will work. Make sure to put --exclude
before the source and destination items.
cp command should ignore some files
To ignore a git directory specifically, I'd try git export
first.
But in general, to copy a directory tree excluding certain files or folders, I'd recommend using rsync
instead of cp
. The syntax is mostly the same, but rsync
has way more options, including one to exclude selected files:
rsync -rv --exclude=.git demo demo_bkp
See e.g. the man page for more info.
script to copy from source to target folder, how to exclude one folder from source folder
Assuming odd_one
is the name of directory you want to exclude, you can do :
shopt -s extglob
cp -r --preserve=ownership,mode $source_folder/!(odd_one) $target_folder
As you use a normal use to write into ~/.snapshot, you should not use sudo
to do rm
.
Related Topics
How to Make a Program Continue to Run After Log Out from Ssh
Best Practices When Running Node.Js With Port 80 (Ubuntu/Linode)
Linking Openssl Libraries to a Program
What Is Double Dot(..) and Single Dot(.) in Linux
Using Printf in Assembly Leads to Empty Output When Piping, But Works on the Terminal
Read Values into a Shell Variable from a Pipe
How to Link to a Specific Glibc Version
Lost Httpd.Conf File Located Apache
Difference Between Clock_Realtime and Clock_Monotonic
How to Automatically Redirect Http to Https on Apache Servers
How to Recursively Find All Files in Current and Subfolders Based on Wildcard Matching
What Does "&" At the End of a Linux Command Mean
How to Use Sed to Change My Configuration Files, With Flexible Keys and Values
Cronjob Does Not Execute a Script That Works Fine Standalone
Asynchronous Io Io_Submit Latency in Ubuntu Linux
Linux: Copy and Create Destination Dir If It Does Not Exist
How to Send a File as an Email Attachment Using Linux Command Line