BASH:所有文件和目录复制到另一个目录在同一个父目录 [英] BASH: Copy all files and directories into another directory in the same parent directory

查看:296
本文介绍了BASH:所有文件和目录复制到另一个目录在同一个父目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的脚本,复制我所有的 $ HOME $ HOME另一个文件夹名为备份/ 。这包括所有隐藏文件和文件夹,不包括备份/ 本身。我现在所拥有的用于复制的部分如下:

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:

shopt -s dotglob

for file in $HOME/*
do
    cp -r $file $HOME/Backup/
done

猛砸告诉我,它无法复制备份/ 到自身。然而,当我检查的内容 $ HOME /备份/ 我看到 $ HOME /备份/备份/ 存在。

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的所有文件夹复制到除备份/ 。我试着用 extglob ,并使用 CP -r $ HOME /!(备份)/ 但它没有拷贝过来隐藏的文件,我需要的。

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.

推荐答案

我同意使用的rsync 将是一个更好的解决方案,但有一个简单的方法来跳过在目录庆典

I agree that using rsync would be a better solution, but there is an easy way to skip a directory in bash:

for file in "$HOME/"*
do
    [[ $file = $HOME/Backup ]] && continue
    cp -r "$file" "$HOME/Backup/"
done

这篇关于BASH:所有文件和目录复制到另一个目录在同一个父目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆