I used ls | agrep "<search pattern #1>;<search pattern #2>;...;<search pattern #n>"
to quickly and/or effectively perform a search/list of some file(s) in a directory with some search pattern(s). As an illustration, I created an empty directory and filled it with some empty files using the touch
command as shown below:
~ ls -l
total 0
-rw-r--r-- 1 me users 0 May 7 14:00 animals-bird_dog_cat.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-bird_dog.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-bird.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-cat_dog_bird.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-cat_dog.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-cat.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-dog_bird.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-dog_cat_bird.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-dog_cat.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-dog.txt
Then, using ls -l | agrep "cat;bird"
, I have no problem to search/find file(s) whose name(s) contain only cat and bird as shown below.
~ ls -l | agrep 'cat;bird'
-rw-r--r-- 1 me users 0 May 7 14:00 animals-bird_dog_cat.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-cat_dog_bird.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-dog_cat_bird.txt
AFAICT, this method is pretty fast and effective (I am all ears for a better solution). Since I do this a lot on a daily base, I thought it may be better to write a simple shell script to perform such a task by simply giving the search pattern(s) in CLI. For instance, I wrote a simple shell scripts as shown below and named it mls
and make it executable:
#!/bin/bash
bold=$(tput bold)
italic=$(tput sitm)
normal=$(tput sgr0)
if [ "$#" -lt 1 ]; then
echo "${bold}Usage${normal}: $0 <search string 0> [... <search string n>]"
exit 0
fi
patterns=""
for ((i=1;i<$#;i++))
do
patterns+="${!i};"
done
patterns+="${!#}"
/usr/bin/ls -lart | agrep '${patterns}${!#}'
Then, when I executed the above shell script with a search pattern of mls cat bird
, it returns nothing as shown below.
~ ls -l | agrep 'cat;bird'
-rw-r--r-- 1 me users 0 May 7 14:00 animals-bird_dog_cat.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-cat_dog_bird.txt
-rw-r--r-- 1 me users 0 May 7 14:00 animals-dog_cat_bird.txt
~ mls cat bird
~
Did I miss something?
ls
. See more here and what to do instead: https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-to-do-instead – Nasir Riley May 08 '21 at 13:59