Я пытаюсь записать простой сценарий удара, который скопирует все содержание папки включая скрытые файлы и папок в другую папку, но я хочу исключить определенные определенные папки. Как я мог достигнуть этого?
Использовать rsync:
rsync -av --exclude='path1/to/exclude' --exclude='path2/to/exclude' source destination
Обратите внимание, что при использовании source
и source /
различаются. Завершающая косая черта означает копирование содержимого папки источник
в место назначения
. Без косой черты в конце это означает копирование папки источника
в назначения
.
В качестве альтернативы, если у вас есть много каталогов (или файлов) для исключения, вы можете использовать - exclude-from = FILE
, где FILE
- это имя файла, содержащего файлы или каталоги, которые нужно исключить.
- exclude
может также содержать подстановочные знаки, такие как - exclude = * /. Svn *
вы можете использовать tar с параметром --exclude, а затем распаковать его в месте назначения. например,
cd /source_directory
tar cvf test.tar --exclude=dir_to_exclude *
mv test.tar /destination
cd /destination
tar xvf test.tar
см. справочную страницу tar для получения дополнительной информации
Вы можете использовать find
с опцией -prune
.
Пример из find
:
cd /source-dir find . -name .snapshot -prune -o \( \! -name *~ -print0 \)| cpio -pmd0 /dest-dir This command copies the contents of /source-dir to /dest-dir, but omits files and directories named .snapshot (and anything in them). It also omits files or directories whose name ends in ~, but not their con‐ tents. The construct -prune -o \( ... -print0 \) is quite common. The idea here is that the expression before -prune matches things which are to be pruned. However, the -prune action itself returns true, so the following -o ensures that the right hand side is evaluated only for those directories which didn't get pruned (the contents of the pruned directories are not even visited, so their contents are irrelevant). The expression on the right hand side of the -o is in parentheses only for clarity. It emphasises that the -print0 action takes place only for things that didn't have -prune applied to them. Because the default `and' condition between tests binds more tightly than -o, this is the default anyway, but the parentheses help to show what is going on.
EXCLUDE="foo bar blah jah"
DEST=$1
for i in *
do
for x in $EXCLUDE
do
if [ $x != $i ]; then
cp -a $i $DEST
fi
done
done
Непроверено ...
Используйте смолу вместе с трубкой.
cd /source_directory
tar cf - --exclude=dir_to_exclude . | (cd /destination && tar xvf - )
Вы можете использовать эту технику даже в ssh.
Подобно идее Джеффа (не проверено):
find . -name * -print0 | grep -v "exclude" | xargs -0 -I {} cp -a {} destination/