4

I would like to search for the string rstudio (and its different possible combinations mentioned below) recursively starting from /.

In some locations, rstudio is a string inside another file (but this file-name is not known); and in other locations, rstudio itself is a (part of a) file-name (e.g., rstudio 2023-10-20 or 2022-10-20-rstudio-file), and this (part of a) file-name is not known. Also, to make matters more complicated, sometimes it is RStudio and sometimes it is R Studio – different cases and/or with spaces.

To summarize, the possible combinations are

  • rstudio is a string inside a file
  • rstudio is a file-name
  • rstudio is a directory-name

In each of the above three lines, do consider that the string can be rstudio or R Studio or Rstudio or r Studio or 2022-10-20 RStudio file. (I hope you get the various possible situations.)

For the output,

  • if rstudio (or any of its above combinations) is the name of a file, then I need the name of the directory for that file,

and

  • if rstudio (or any of its above combinations) is a string inside another file, then I need the corresponding file-name and its location.

Can someone please help me? I looked up on a few answers using find and grep but I am not able to get it.

7
  • But it will always be either rstudio or R Studio, is that correct? Never studioR or r studio or r Studio or anything else?
    – terdon
    Feb 12 at 17:04
  • 1
    Yes it is always r{1 space}studio but it can be capitals ;)
    – Rinzwind
    Feb 12 at 17:14
  • Can it, @Rinzwind? The OP only mentions those two exact cases: rstudio and R Studio. Why do you say the case can change? And why do you say it is always r{1 space}? That one we already know is false since the OP mentioned rstudio.
    – terdon
    Feb 12 at 17:24
  • @terdon - Hello! Yes, it can be r Studio or R studio or any text before/after R studio or r Studio or any such combinations of rstudio; but it will not be Studio R.
    – koustav_ch
    Feb 12 at 17:58
  • 7
    ... what about strings like "Microsoft Developer Studio"? Feb 12 at 18:48

3 Answers 3

6

I haven't done any profiling, but it may be faster to do a single recursive descent rather than a separate find and recursive grep. Some other potential optimizations:

  1. avoid searching /dev and ephemeral directories /proc, /sys etc.

  2. don't search inside files that have already been selected based on their name

  3. avoid searching inside binary files using grep's -I or --binary-files=without-match

  4. use grep's fixed string mode, since neither rstudio nor R studio has any regular expression metacharacters - note that GNU grep still honors the case-insensitive -i and word-boundary flag -w in this mode, so we avoid matches like Developer Studio.

So

find / \( -path /dev -o -path /proc -o -path /run -o -path /sys \) -prune \
    -o \( -iname '*rstudio*' -o -iname '*r studio*' \) -printf '%h/\n' \
    -o -type f -exec grep -IFiwl -e 'rstudio' -e 'r studio' -- {} +

I used -printf '%h/\n' rather than plain -printf '%h\n' because the trailing / makes it easier to distinguish whether the match was by name or by content.

Note that even with the pruning, you should expect many "Permission denied" errors unless the above is run as root.

3
  • 1
    that sure is a monster of a "find". big fan of monsters :+)
    – Rinzwind
    Feb 13 at 8:21
  • 1
    @Rinzwind lol well I tried to format it into recognizable chunks (prune, then match names, then grep) Feb 13 at 13:08
  • I would be hesitant to include optimization 3 as general guidance unless specifically told that binary files aren't relevant. Someone will end up missing a file where the string was a reference to a shared lib or in the header of a custom binary file or in the EXIF data of an image or who knows what else. Feb 13 at 14:11
6

This will be slow, but if you need to look at all the files on your system, that will always be slow. I would do this in two passes. One, with find to look for file names and the other, with grep to look at file contents.

  1. Find files whose name contains the string rstudio or R Studio, case insensitively, and print the name of the directory containing them.

    find / \( -iname "*rstudio*" -or -iname "*R Studio*") -printf '%h\n' 
    

    This will look search all files (and directories, and anything else) in the / directory whose name contains one of the two desired strings and then print the directory name (%h, see man find). You can pass this through sort -u to remove duplicates. To avoid permission denied warnings, either add 2> /dev/null to the end, or run as root:

    sudo find \( -iname "*rstudio*" -or -iname "*R Studio*" \) -printf '%h\n' | sort -u
    

    or, if you don't care about directories your user cannot read

    find  \( -iname "*rstudio*" -or -iname "*R Studio*" \) -printf '%h\n' 2> /dev/null | sort -u
    
  2. Find files containing one of these strings.

    grep -ilR -E 'R Studio|rstudio' /
    

    Here, we run a recursive (-R) grep with extended regular expressions (-E) and tell it to find either of the two strings. The -l tells grep to just print the name of the file, not the matched line from it. As above, to avoid warnings, add 2> /dev/null or run as root.

13
  • those will need sudo. It will be a lot quicker to use locate for finding the files. and you need -i for grep to ignore case. you answer snatcher :+ Gratz on 100k O
    – Rinzwind
    Feb 12 at 17:12
  • @Rinzwind yes, that's why I have a whole sentence explaining that you need to either add 2> /dev/null or run as root if you want to avoid permission denied warnings. I don't like telling people to use sudo when I don't know it is needed. for all I know, the OP is only interested in files they have access to. As for locate, yes, but I don't know how to combine the two options for it. Still, running sudo updatedb and then two locate commands will still be much faster than find, but then you have issues with weird file names to get the dir etc.
    – terdon
    Feb 12 at 17:23
  • Go ahead and post the locate answer, @Rinzwind, looking forward to reading it! And thanks :)
    – terdon
    Feb 12 at 17:25
  • 1
    Also, do try locate -i rstudio; locate -i "R studio" as @Rinzwind suggested, that will do a much better job at finding the file names.
    – terdon
    Feb 12 at 19:22
  • 1
    Eek, thanks, that was a typo @koustav_ch! Also, please see updated answer that incorporates Daniel's suggestion.
    – terdon
    Feb 13 at 13:30
5

locate is perfect for looking for files and directories.

locate -i rstudio
locate -i "R studio"

Easy to remember and it will be the quickest method as it uses its own cache to show the results. It also always shows the full path. If you use -c you only get the number of files found. -0 will use NULL as separator so make it a one-liner.

It probably is not installed: sudo apt install plocate and to update its database: sudo updatedb.

Searching inside files is in Terdon's answer.

1
  • Thank you for the answer! :) On the mac terminal using bash, locate was installed but updatedb was not. But then I was able to use locate.updatedb.
    – koustav_ch
    Feb 13 at 19:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .