Unix “find” command usage

后端 未结 2 1758
梦如初夏
梦如初夏 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: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.

提交回复
热议问题