Home > database >  Make an alias for find command handles glob
Make an alias for find command handles glob

Time:02-16

I want to use this alias for all files that end with .txt

$ f "*.txt"

I expect to get all files that end with txt like foo.txt and bar.txt, not a file called ".txt" The problem is that this command is this searching for files called ".txt" not count * as a glob.

my .bashrc:

#f
f() {
    sudo find . -name "$1" 2> /dev/null | grep --color=always $1
}

# F
F() {
    sudo find / -name "$1" 2> /dev/null | grep --color=always $1
}

CodePudding user response:

Shell glob syntax and regexes are two different syntaxes. You can't use them interchangeably.

Fortunately, you don't need to get grep involved.

find . -name '*.txt'

will find all the files that end in .txt. If you want to exclude .txt then change your glob pattern to require at least one character.

find . -name '?*.txt'

Side note: Putting sudo in a shell alias is a bad idea. You only want to run sudo when you specifically ask for it.

CodePudding user response:

This works for me.

# f
f() {
    p1="sudo find . -name \"${1}\""
    p2="2> /dev/null"
    p3="grep --color=always \".${1}\""
    eval ${p1} ${p2} "|" ${p3}
}

# F
F() {
    p1="sudo find / -name \"${1}\""
    p2="2> /dev/null"
    p3="grep --color=always \".${1}\""
    eval ${p1} ${p2} "|" ${p3}
}

Demo: https://youtu.be/RCsxLHPlzZ4

  • Related