BASH:将所有文件和目录复制到同一父目录下的另一个目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20832544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BASH: Copy all files and directories into another directory in the same parent directory
提问by Hugo
I'm trying to make a simple script that copies all of my $HOME
into another folder in $HOME
called Backup/
. This includes all hidden files and folders, and excludes Backup/
itself. What I have right now for the copying part is the following:
我正在尝试制作一个简单的脚本,将我的所有内容复制$HOME
到另一个$HOME
名为Backup/
. 这包括所有隐藏的文件和文件夹,不包括Backup/
自身。我现在复制部分的内容如下:
shopt -s dotglob
for file in $HOME/*
do
cp -r $file $HOME/Backup/
done
Bash tells me that it cannot copy Backup/
into itself. However, when I check the contents of $HOME/Backup/
I see that $HOME/Backup/Backup/
exists.
Bash 告诉我它不能复制Backup/
到自身中。但是,当我检查内容时,$HOME/Backup/
我发现$HOME/Backup/Backup/
存在。
The copy of Backup/
in itself is useless. How can I get bash to copy over all the folders except Backup/
. I tried using extglob
and using cp -r $HOME/!(Backup)/
but it didn't copy over the hidden files that I need.
副本Backup/
本身是没用的。我怎样才能让 bash 复制除Backup/
. 我尝试使用extglob
和使用,cp -r $HOME/!(Backup)/
但它没有复制我需要的隐藏文件。
采纳答案by chepner
I agree that using rsync
would be a better solution, but there is an easy way to skip a directory in bash
:
我同意 usingrsync
是一个更好的解决方案,但有一种简单的方法可以跳过 中的目录bash
:
for file in "$HOME/"*
do
[[ $file = $HOME/Backup ]] && continue
cp -r "$file" "$HOME/Backup/"
done
回答by Tharanga Abeyseela
try rsync. you can exclude file/directories .
试试 rsync。您可以排除文件/目录。
this is a good reference
这是一个很好的参考
http://www.maclife.com/article/columns/terminal_101_using_rsync_locally
回答by user2691041
Hugo,
雨果,
A script like this is good, but you could try this:
像这样的脚本很好,但你可以试试这个:
cp -r * Backup/; cp -r .* Backup/;
cp -r * 备份/;cp -r .* 备份/;
Another tool used with backups is tar. This compresses your backup to save disk space.
另一个用于备份的工具是 tar。这会压缩您的备份以节省磁盘空间。
Also note, the * does not cover . hidden files.
另请注意, * 不包括 . 隐藏文件。
回答by Mzzl
This doesn't answer your question directly (the other answers already did that), but try cp -ua
when you want to use cp to make a backup. This recurses directories, copies rather than follows links, preserves permissions and only copies a file if it is newer than the copy at the destination.
这不会直接回答您的问题(其他答案已经回答了),但是cp -ua
当您想使用 cp 进行备份时可以尝试。这会递归目录、复制而不是跟随链接、保留权限并且仅复制比目的地副本新的文件。