BASH-复制时忽略隐藏文件和空源目录 [英] BASH - ignore hidden files and empty source directory when copying

查看:61
本文介绍了BASH-复制时忽略隐藏文件和空源目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是BASH的新手,并且我一直在通过cron job使用此脚本。我遇到了一些麻烦,最初使用 mv ,但事实证明它不会与匹配的目录或任何其他内容合并。因此建议我使用 cp ,因为据称它将合并并替换。

I'm new to BASH, and I've been working with this script with cron job. I had some trouble with it, originally using mv, but it turns out it won't merge with matching directories, or anything. So I was advised to use cp, since that supposedly will merge and replace.

/ schedule / =源目录
/ public_html / =目标目录

/schedule/ = source directory /public_html/ = target directory

#!/bin/bash

[ignore dot files/hidden files when copying]
cp -a schedule/* public_html/
[empty contents of source directory(schedule/) after copying]

来源: / schedule / (包含文件夹/文件)

Source: /schedule/ (containing folders/files)

/files/
--4.html
--5.html
/assets/
--sitemap.xml

目标位置: / public_html / (上一个文件夹已经存在)

Destination: /public_html/ (previous folders already exist)

/files/
--1.html
--2.html
--3.html
/assets/
--sitemap.xml

最终,我试图找出如何在使用 cp 时忽略隐藏文件的方法,并在复制后清空源目录。我正在阅读BASH参考手册,并看到 nullglob ,但是我不知道这是否应该使用。

Ultimately, I'm trying to find out how to ignore hidden files while using cp, and to empty source directory after copying. I was reading the BASH reference manual and saw nullglob, but I don't know if that's what should be used.

有人可以提供一个例子或向我指出正确的方向吗?

Could anyone provide an example or point me in the right direction?

推荐答案

您可以使用regex捕获所有隐藏文件和文件夹,然后用取反。然后,您可以列出所有文件并复制并删除文件。

You can use regex to catch all the hidden files and folders and negate this with !. You can then list all FILES and copy and remove the files.

FILES=$(find /PATH/TO/SOURCE/FOLDER \( ! -regex '.*/\..*' \) -type f ) 
for f in $FILES; 
do 
 cp /PATH/TO/SOURCE/FOLDER/$f /PATH/TO/DEST/FOLDER/$f;
 rm /PATH/TO/SOURCE/FOLDER/$f
done

或者,您可以使用 mv 代替 cp rm

Alternatively you could use mv instead of cp and rm.

这篇关于BASH-复制时忽略隐藏文件和空源目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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