I want to search for all files with a specific name in a directory and apply ls -l on it , to inspect its size,
First I went with find . -name .ycm* | ls -l
but it didn't work , got the explanation from this link.
Then I tried to create a script which will recursively go through the directory and search for the file name and do ls -l
on it or any other command in general.
I came with the following script , but it turned out that its stuck in the first call itself , and its calling it again and again .
#!/bin/bash
for_count=0
file_count=0
dir_count=0
search_file_recursive() {
# this function takes directory and file_name to be searched
# recursively
# :param $1 = directory you want to search
# :param $2 = file name to be searched
for file in `ls -a `$1``:
do
let "for_count=for_count+1"
echo "for count is $for_count"
# check if the file name is equal to the given file
if test $file == $2
then
ls -l $file
let "file_count++"
echo "file_count is $file_count"
elif [ -d $file ] && [ $file != '.' ] && [ $file != '..' ]
then
echo "value of dir = $1 , search = $2, file = $file"
search_file_recursive $file $2
let "dir_count++"
echo "directory_count is $dir_count"
fi
done
return 0
}
search_file_recursive $1 $2
This is how my output looks without echo
anupam … YouCompleteMe third_party ycmd ae8a33f8 … 5 ./script.sh pwd .ycm_extra_conf.py
Segmentation fault: 11
anupam … YouCompleteMe third_party ycmd ae8a33f8 … 5 echo $?
139
ls
. Quote your variable expansions. Test your script in ShellCheck. What Unix are you on? You first say you just want the size of files that matches a particular pattern, but your code seems to be doing a lot more than that. Could you clarify what you want to do please? – Kusalananda Nov 07 '18 at 10:29ls -l
on that file – lazarus Nov 07 '18 at 10:39\
ls -a `$1``` seems to evaluate to the list given by\
ls -a .`` concatenated to$1
. It spells like "executels -a
(that defaults to.
) and concatenate$1
to the result". – fra-san Nov 07 '18 at 11:04