Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cp command should ignore some files

Sometimes I need to perform following command

cp -rv demo demo_bkp 

However I want to ignore all the files in directory .git . How do I achieve that? It takes a long time to copy .git files and I do not need those files.

like image 615
Nick Vanderbilt Avatar asked Sep 08 '10 22:09

Nick Vanderbilt


People also ask

How do I use cp to exclude a specific directory?

You can use --exclude multiples times. 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.

Does cp command remove original file?

cp does not delete files. Show activity on this post. -i Cause cp to write a prompt to the standard error output before copying a file that would overwrite an existing file.

How do I exclude a file in Linux?

To do so, create a text file with the name of the files and directories you want to exclude. Then, pass the name of the file to the --exlude-from option.


1 Answers

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.

like image 170
David Z Avatar answered Oct 02 '22 17:10

David Z