-1

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
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
lazarus
  • 185
  • 1
  • 1
  • 10
  • Backticks do not nest. Don't parse the output of `ls`. Quote your variable expansions. Test your script in [ShellCheck](https://www.shellcheck.net/). 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:29
  • @Kusalananda , I am on Mac 10.14 , I added extra variables just for debugging , I just want to go into every directory recursively and search if it has a matching file name in it .. its same as find but just I have to run `ls -l` on that file – lazarus Nov 07 '18 at 10:39
  • About the endless loop: `\`ls -a \`$1\`\`` seems to evaluate to the list given by `\`ls -a .\`` concatenated to `$1`. It spells like "execute `ls -a` (that defaults to `.`) and concatenate `$1` to the result". – fra-san Nov 07 '18 at 11:04

1 Answers1

1

With GNU find, to get the sizes (in bytes) of the regular files whose filenames match the pattern .ycm*, you would do

find . -type f -name '.ycm*' -printf '%s\t%p\n'

This would print the size followed by a tab and the pathname of the file. Note the quoting of the filename pattern to avoid using it as a shell globbing pattern on the command line.

The following would use the external stat command (on Linux only) on each file in a similar way:

find . -type f -name '.ycm*' -exec stat --printf '%s\t%n\n' {} +

The following would work on a BSD system (for example macOS):

find . -type f -name '.ycm*' -exec stat -f '%z%t%N' {} +

In the BSD stat format string, %z will be replaced by the size in bytes, %t will be replaced by a tab character, and %N will replace by the pathname of the file.

See also:

Kusalananda
  • 320,670
  • 36
  • 633
  • 936