我正在尝试为debian包编写维护脚本。假设我有一个如下的目录结构:
application/
----application/file1.txt
----application/file2.txt
----application/config
--------application/config/c1.conf
--------application/config/c2.conf
----application/logs
--------application/logs/l1.txt
--------application/logs/l2.txt
----application/src
--------application/src/static/
------------application/src/static/js
------------application/src/static/css
--------application/src/s1.py
--------application/src/s2.py
----application/lib
--------application/src/js/
--------application/src/css/现在,我想删除除config和logs之外的所有文件/文件夹(在本例中为src和lib文件夹,以及file1.txt和file2.txt文件)。我的PWD当前是appliaction/目录的父目录(即,我可以在我的PWD中看到application )。
我应该使用什么命令(一个小的bash script会更好)?(我用一些选项尝试了rm -rf,但错误地删除了其他文件,所以在尝试任何其他文件之前,我想知道正确的答案!)
发布于 2014-08-08 15:56:49
据我所知,除了src,还有很多其他的文件夹,因为如果不这样的话,你只会使用rm -rf src。
如果您的PWD是application,即您在config logs和src的父目录中,那么您只需要一种方法来对除config和log之外的所有文件/文件夹使用rm -rf,那么为什么不创建一个for循环呢?
#!/bin/bash
cd "/path/to/application"
for file in * ; do
case "$file" in
config )
echo "file is $file: doing nothing"
;;
logs )
echo "file is $file: doing nothing"
;;
* )
echo "file is $file: removing"
rm -rf "$file"
;;
esac
done
exit 0发布于 2014-08-08 16:08:21
这必须行得通:
find ./application -depth -mindepth 1 -type d \
-regextype posix-extended ! -regex ".*\/(config|logs).*" | \
-exec rm -fr {} \;或者在xargs中,如果您没有包含换行符的文件夹名称,您可以这样写:
find ./application -depth -mindepth 1 -type d \
-regextype posix-extended ! -regex ".*\/(config|logs).*" | \
xargs -t -I {} rm -fr {}或者,对于xargs,如果您的文件夹名称中包含换行符,则可以使用:
find ./application -depth -mindepth 1 -type d \
-regextype posix-extended ! -regex ".*\/(config|logs).*" -print0 | \
xargs -0 -t -I {} rm -fr {}find将查找./application下的所有目录(包含/config和/logs的目录除外),并从运行lowestxargs开始打印这些目录,删除目录发布于 2014-08-08 15:40:28
由于您只是删除了application/application/src目录,因此没有理由使用find等。你所需要做的就是:
rm -rf application/application/src剩余:
application/application/logs/l2.txt
application/application/logs/l1.txt
application/application/config/c1.conf
application/application/config/c2.confhttps://stackoverflow.com/questions/25197825
复制相似问题