Unix “find” command usage

后端 未结 2 1743
梦如初夏
梦如初夏 2021-01-22 00:24

This is for a bash installation script. The script foo.sh takes \"DIRECTORY\" as an argument. Say, there is a dir <$HOME>/TEST/TEST_1A/TEST_2A/TEST_3 and anot

相关标签:
2条回答
  • 2021-01-22 01:21

    Wouldn't this work as intended?

    find $HOME -type d -name $1 -exec echo {} ';'  -exec rm -rf {} ';'
    

    It will complain about deleting of processed directory, but toher than some stderr output it's ok...

    You can ofc change "-name" for "-regexp" as you suggested, but if you want something more complex, you can create special script to call from "-exec"

    EDIT: I probably understood you wrong first time (but won't delete anything, it may help somebody) - you want to delete only one of TEST_3 directories (how do you choose which one? first found by find? - it seems quite strange), still as I suggested in previous paragraph, you can provide skript to -exec which will take care of all decision making)

    0 讨论(0)
  • 2021-01-22 01:29

    You could try:

    if [[ "$1" == */* ]]; then
        EXPR="-path *$1"
    else
        EXPR="-name $1"
    fi
    DIR=$(find $HOME -type d $EXPR | head -1)
    

    A simple name like "TEST_3" will translate into find -name TEST_3 but a name with a slash like "ST_2/TEST_3" will translate into find -path *ST_2/TEST3. This will take care of (partial) directory names.

    0 讨论(0)
提交回复
热议问题