6

我正在尝试为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(在本例中为srclib文件夹file1.txtfile2.txt文件)。我PWD目前是appliaction/dir 的父母(即,我可以application在 my中看到PWD)。

我应该使用什么命令(小的bash script会很棒)?(我尝试了rm -rf一些选项,但错误地删除了其他文件,所以我想在尝试其他任何东西之前知道正确的答案!)

4

6 回答 6

1

据我所知,还有很多其他文件夹src,否则你只会使用rm -rf src.

如果你的PWDis application,即你在 and 的父目录中config logssrc你只需要一种方法来使用除andrm -rf之外的所有文件/文件夹,那么为什么不做一个 for 循环呢?configlog

    #!/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-08T07:56:49.040 回答
1

这必须有效:

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并从最低的开始打印它们
  • xargs运行删除目录
于 2014-08-08T08:08:21.360 回答
0

使用重击:

for file in applications/*; do
    target="${file#*/}"
    if [[ $target != config && $target != logs ]]; then
        rm -rf "$file"
    fi
done
于 2014-08-08T09:35:33.323 回答
0
cd application
find . -type d | egrep -v '(^\./$|^\./log|^\./config)' | while read DIR
do
   echo rm -rf $DIR
done

这使用 find 列出所有目录,通过 grep 管道排除我们想要保留的目录(也排除作为 PWD 的“./”),并将过滤后的列表管道传输到执行删除的 while 循环。我已经把“回声”放在那里,这样你就可以测试它是否符合你的实际需要。如果您满意,请删除回声。由于返回的列表 (DIR) 将按树顺序排列,例如 d1、d1/d1a、d1/d1b,并且 rm -rf d1 将删除较低的文件夹,您可能需要添加一个“if [ -d $DIR] " 围绕 rm -rf。

于 2014-08-08T08:17:57.390 回答
0

由于您只是删除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.conf
于 2014-08-08T07:40:28.080 回答
0

不需要复杂的find命令,只需使用 shell(假设您在应用程序文件夹中):

rm -r !(logs|config)

这被称为“路径名扩展”,来自man bash

         !(pattern-list)
                Matches anything except one of the given patterns
于 2014-08-14T09:02:23.043 回答