移除指定目录的文件

简介:

#!/bin/bash

dir1=/root/was1

dir2=/root/was2

#dir1=/wasprofiles/c01-`hostname`/installedApps/cell01

#dir2=/wasprofiles/c02-`hostname`/installedApps/cell02


function del_files

{

    i=1

    echo "----------------------------------------------------------------------"


    arr=($1)

    for s in ${arr[@]}

    do

        echo "$i $s"

        i=$[$i + 1]

    done

    

    echo "----------------------------------------------------------------------"

    echo "Choose files to remove.(Example:1 2 3 or all)"

    read rmfiles

    while test -z "$rmfiles"

    do

        echo "Input is null! Please input again.(Example:1 2 3 or all)"

        read rmfiles

    done

    echo "----------------------------------------------------------------------"


    if [ "$rmfiles" = "all" ] ; then

        for rf in ${arr[@]}

        do

    [ ! -d /tmp/$2 ] && mkdir /tmp/$2

    rmname=`sed 's/\///' <<< "$rf" | sed 's/\//#/g'`

            mv $rf /tmp/$2/$rmname

done

    else

        for rf in $rmfiles

        do

            if grep '^[[:digit:]]*$' <<< "$rf" >/dev/null ; then

        arf=$[$rf - 1]

        if test -z ${arr[$arf]} ; then

    echo "Eeror: Input $rf not exist"

        else

                    [ ! -d /tmp/$2 ] && mkdir /tmp/$2

    rmname=`sed 's/\///' <<< "${arr[$arf]}" | sed 's/\//#/g'`

                    mv ${arr[$arf]} /tmp/$2/$rmname

        fi

            else

                echo "Eerror: Input $rf is not number."

            fi

        done

    fi

    echo "ls /tmp/$2"

    ls -l /tmp/$2

}


echo "Please input was1,was2 or exit."

read line

while test -z "$line" || ([ "$line" != "was1" ] &&  [ "$line" != "was2" ] && [ "$line" != "exit" ])

do

    echo "Input error! Please input was1,was2 or exit."

    read line

done

echo "----------------------------------------------------------------------"


if [ $line = "was1" ] ; then

    echo "Files will be found in $dir1"

    echo "Please input filename.(Example: abc 123.txt)"

    read filename

    while test -z "$filename"

    do

        echo "Input is null! Please input again.(Example: abc 123.txt)"

        read filename

    done


    for file in $filename

    do

        ffile=`find $dir1 -type f -name $file\*`

        if test -z "$ffile" ; then

            echo "Info: $file is not found."

        else

            ffiles="$ffile $ffiles"

        fi

    done

    if test -z "$ffiles" ; then

        echo "Can't find input files."

    else

        del_files "$ffiles" was1

    fi

elif [ $line = "was2" ] ; then

        echo "Files will be found in $dir2"

    echo "Please input filename.(Example: abc 123.txt)"

    read filename

    while test -z "$filename"

    do

        echo "Input is null! Please input again.(Example: abc 123.txt)"

        read filename

    done

    for file in $filename

    do

        ffile=`find $dir2 -type f -name $file\*`

        if test -z "$ffile" ; then

            echo "Info: $file is not found."

        else

            ffiles="$ffile $ffiles"

        fi

    done

    if test -z "$ffiles" ; then

        echo "Info: All input files not found!"

    else

        del_files "$ffiles" was2

    fi

elif [ $line = "exit" ] ; then

    exit

fi



     本文转自1321385590 51CTO博客,原文链接:http://blog.51cto.com/linux10000/1960782 ,如需转载请自行联系原作者




相关文章
|
13天前
|
弹性计算 运维 Shell
删除某个目录下大小为 0 的文件
【4月更文挑战第28天】
16 0
|
12天前
|
弹性计算 运维 Shell
判断文件或目录是否存在
【4月更文挑战第29天】
19 0
|
2月前
rm 移除文件或目录
rm 移除文件或目录。
88 5
|
7月前
|
Java
Java 创建文件自动新增父目录、查询目录文件、删除文件目录下面的文件
要处理文件保存和删除的操作,记录一下以免遗忘: 1、创建文件,并且自动创建父目录 2、删除目录下面的所有文件
106 0
|
9月前
|
Java Apache
java复制文件的4种方式及拷贝文件到另一个目录下与删除单个文件和删除整个文件夹
java复制文件的4种方式及拷贝文件到另一个目录下与删除单个文件和删除整个文件夹
427 0
递归删除指定文件-目录
递归删除指定文件-目录
105 0
自定义的删除函数,可以删除文件和递归删除文件夹
自定义的删除函数,可以删除文件和递归删除文件夹
142 0
|
开发框架 .NET Windows
文件和文件夹的操作——获取当前路径方法
文件和文件夹的操作——获取当前路径方法
245 0