【问题标题】:Delete file using linux使用linux删除文件
【发布时间】:2014-05-25 11:07:43
【问题描述】:

这段代码是我想给两个目录,这段代码会查看这两个目录是否包含两个包含相同信息的文件,并询问我要删除哪个文件。

我已经写了这段代码,但我不知道如何编写删除文件的代码,请帮助

#!bin/bash
echo "give the first directory"
read firstdir

echo "give the second directory"
read seconddir

for i in ` ls $firstdir`

do

echo $i

t= `md5sum $firstdir/$i`

s= `md5sum $seconddir/$i`

if [ "$t" ! = "$s" ]

then

echo " of which directory will be eliminated? $i"

read direct

( here I want the code to delete the directory ex : delete  direct/i )

fi

done

【问题讨论】:

    标签: linux file directory delete-file


    【解决方案1】:

    替换:

    echo " of which directory will be eliminated? $i"
    
    read direct
    
    ( here I want the code to delete the directory ex : delete  direct/i )
    

    与:

    echo "of which directory will be eliminated? $i:
    1)$firstdir
    2)$seconddir
    "
    read -p "(1/2)" direct
    case $direct in
            1)
                    rm -v $firstdir/$i
            ;;
            2)
                    rm -v $seconddir/$i
            ;;
            *)
            echo "ERROR: bad value, 1 or 2 expected!" ; exit 1
    esac
    

    【讨论】:

    • 欢迎 :) 请为我的解决方案投票,如果您觉得有用,请接受它。
    【解决方案2】:

    好的,试试这个。我只是根据您的要求制作了自己的解决方案。我希望你喜欢它。谢谢

    #!/bin/bash
    
    # check for a valid first directory
    while true
    do
        echo "Please, enter the first directory"
        read FIRST_DIR
    
        if [ -d $FIRST_DIR ]; then
            break
        else
            echo "Invalid directory"
        fi
    done
    
    # check for a valid second directory    
    while true
    do
        echo "Please, give the second directory"
        read SECOND_DIR
    
        if [ -d $SECOND_DIR ]; then
            break
        else
            echo "Invalid directory"
        fi
    done
    
    
    for FILE in `ls $FIRST_DIR`
    do
        # make sure that only files will be avaluated
        if [ -f $FILE ]; then
    
            echo $SECOND_DIR/$FILE
    
            # check if the file exist in the second directory
            if [ -f $SECOND_DIR/$FILE ]; then
    
                # compare the two files
                output=`diff -c $FIRST_DIR/$FILE $SECOND_DIR/$FILE`
    
                if [ ! $output ]; then
                    echo "Which file do you want to delete?
                    1)$FIRST_DIR/$FILE
                    2)$SECOND_DIR/$FILE
                    "
    
                    # read a choice
                    read -p "(1/2)" choice
    
                    # delete the chosen file
                    case $choice in
                        1)
                            rm -v $FIRST_DIR/$FILE
                            ;;
                        2)
                            rm -v $SECOND_DIR/$FILE
                            ;;
                        *)
                            echo "ERROR invalid choice!"
                    esac      
                fi
            else
                echo "There are no equal files in the two directories."
                exit 1  
            fi
        else
            echo "There are no files to be evaluated."
        fi
    done
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2013-02-23
    • 2012-03-04
    相关资源
    最近更新 更多