0

I want to search methyldopa in .tex files by the Ubuntu search. I just included the files in my Dropbox folder which may be confusing Ubuntu's search tool. However, I see those files in Recent files of the search tool. I search also unsuccessfully methyldopa .tex but also doing unsuccessfully just methyldopa and limiting Type to Documents, Folders and Other.

I run sudo updatedb and still unsuccessful searches.

How can you search .tex files in Ubuntu 14.04 LTS?

4
  • You don't want command line options, I suppose?
    – muru
    Dec 20, 2015 at 13:29
  • Looks like it is taking some time for those files to be indexed. Try running sudo updatedb
    – Ron
    Dec 20, 2015 at 13:49
  • 1
    @Ron updatedb? You should read man updatedb and man locate
    – A.B.
    Dec 20, 2015 at 15:11
  • @A.B. looks like someone found that useful. askubuntu.com/questions/612354/…
    – Ron
    Dec 20, 2015 at 15:32

4 Answers 4

2

Although you are looking for a GUI solution, I would like to share a command line solution, because I think it is quite handy and easy to use:

tl;dr:

  • Use grep -w methyldopa *.tex | cut -d":" -f1 if all files are in the same directory.
  • Use find -name "*.tex" -exec grep -H -w methyldopa {} \; | cut -d":" -f1 in a directory, where all files sou want to find are in subdirectories.
  • Edit: Shortest Way (credits to @A.B.): grep -rwl methyldopa | grep .tex

long version with explanations:

First Case:

All files, you want to search are in the same directory.
This is the easiest scenario. You can simply use

grep -w methyldopa *.tex | cut -d":" -f1 

to find all .tex files containing methyldopa.

Explanation:
Directory and file content:

file1.txt
file2.txt
file3.list

#file1.txt
foo
bar
foobar

#file2.txt
foo
foobar
1337

#file3.list
foo
bar
foobar

Now, you can use grep foo * to search for foo in all files. This is waht you will get:

file1.txt:foo
file1.txt:foobar
file2.txt:foo
file2.txt:foobar
file3.list:foo
file3.list:foobar

Using the -w option, will prevent finding foobar:

grep -w foo *

file1.txt:foo
file2.txt:foo
file3.list:foo

If you want to restrict your search to files with a special ending, you can do the following:

grep -w foo *.txt

file1.txt:foo
file2.txt:foo

Last, but not least, you can pipe the results to a cut command to extract only the filenames (-d":" sets the field separator to :, -f1 returns the first field of each line):

grep -w foo *.txt | cut -d":" -f1

file1.txt
file2.txt

Second Case:

Your files are in different directories. In this case, you should use find to find the files first:

find -name "*.txt" -exec grep -H -w foo {} \; | cut -d":" -f1

Explanation:

find -name "*.txt" searches for files ending with .txt in your current directory and all subdirectories. You can use something like find ~/Documents/ -name "*.txt" to start searching at ~/Documents.

-exec grep -H -w foo {} \; searches for foo in each file that was found and returns something like this (the -H flag makes sure, that the filename is printed):

./testdir/file2.txt:foo
./testdir/file1.txt:foo

Like in the first case, | cut -d":" -f1 cuts out the filenames from the output.

6
  • 3
    The easiest way is a grep -rwl methyldopa *.tex
    – A.B.
    Dec 20, 2015 at 15:08
  • @A.B. damn - I did not think about that one ;-)
    – Wayne_Yux
    Dec 20, 2015 at 15:20
  • @A.B. Your command is searching only in one directory. I think you need to expand it with find to get search in many directories. Dec 20, 2015 at 15:49
  • @Masi No, -r is for recursive
    – A.B.
    Dec 20, 2015 at 16:05
  • @A.B. Unsuccessful in my system for many directories. It only searches the current directory, even with grep -rwl and *.tex. Dec 20, 2015 at 16:10
2

For a graphical solution (and a more powerful one, when it works correctly --- which is most of the time) is to install recoll.

You will need to install it using the PPA --- look at the download instructions (scroll down to the part for Ubuntu systems).

After having installed it, choose a kind of indexing (I do mine overnight for this still unresolved problem --- which is probably due to some strange configuration in my system), you have a very powerful tool to find files in your system (even contents of files like ODS spreadsheet or compressed archives ort similar things).

In your case simply call the Recoll interface:

recoll interface

(I used "puente" as the search term because I have no methyldopa around ;-)) --- the search is written in the recoll query language..

0

I suggest to use Recoll for an indexed search with a graphical interface and a preview functionality! It is in the official repositories, easy to use and very powerful.

1
  • Recoll seems to be a descent tool with active development. Do you know what are the benefits of having python-recoll extension with Recoll? Dec 20, 2015 at 13:50
0

Use the script which I call haetex for the system-wide search. Put the script to /usr/local/bin/ as just haetex with such permissions/owners; which you can do by sudo chmod 755 /usr/local/bin/haetex

-rwxr-xr-x  1 root root  932 Jun 21 17:16 haetex

Characteristics

  • use find -L ... -exec fgrep -l ... 2>/dev/null because it takes a bigger space in the system than find -H ... -exec fgrep -l ..., without giving errors about faulty symlinks
  • I think fgrep fits here better than grep
  • /dev/null is necessary in the second command set because we are using find -L, since we want to include all symlinks and go them through and not exclude anything in the search

File haetex

#!/bin/bash
# $Id: haetex, v 0.01 2016/5/4 17:33:12 Masi $
# v 0.011 2016/7/1 08:28:11 Masi $ [location], show filename when one file, search symlinks by default 
# v 0.012 2016/8/16 19:21:00 Masi $ fix for find: ‘/home/masi/LOREM’: Too many levels of symbolic links


# Search .tex files
#
# .vimrc
# http://vi.stackexchange.com/q/7782/2923
#
# http://unix.stackexchange.com/a/287344/16920

usage() {
# a function that prints an optional error message and some help.
# and then exits with exit code 1
[ -n "$*" ] && printf "%s\n" "$*" > /dev/stderr

cat <<__EOF__
Usage:
      $0 [-h] [ -i ] [ location ] [ word(s) ] [ -x example_data ]

-i    Ignore case
-x    The example option, requires an argument.
-h    This help message.

Detailed help message here
__EOF__

exit 1
}

case_option=''
case_example=''

while getopts "hix:" opt; do
    case "$opt" in
        h) usage ;;
        i) case_option='-i' ;;
        x) case_example="$OPTARG" ;;
        *) usage ;;
    esac
done
shift $((OPTIND-1))

# include symlinks
# http://unix.stackexchange.com/a/293147/16920
# http://unix.stackexchange.com/a/303694/16920
# http://unix.stackexchange.com/a/304027/16920
find -L "$1" -xtype f -name "*.tex" \
   -exec fgrep -l $case_option "$2" {} + 2>/dev/null | vim -R -
# The /dev/null makes sure `fgrep` prints a filename even when it has only a single file to search. 

# TODO +perm 0666 not working in Ubuntu 16.04; instead fix the origin of the permission denied alerts
#sudo gfind /Users/masi/Math/ -type d -exec chmod 0755 {} \;
#sudo gfind /Users/masi/Math/ -iname '*.docx' ! -readable -exec chmod 0644 {} \;

# TODO remove -l but make opening file only filename in Vim

# TODO less -s -M +Gg - Less as frontend with selection possibility? Maybe with GNU Screen

Select links in the visual mode. Press Enter (=leader) + g + t on the visual mode. Output: files in the tabs. So you redirect the output to Vim. Expected $HOME/.vimrc

"" Mapleader is space now from \ 
let mapleader=" "
"" Quick tabs - Go to tab by number 
noremap <leader>1 1gt
noremap <leader>2 2gt
noremap <leader>3 3gt
noremap <leader>4 4gt
noremap <leader>5 5gt
noremap <leader>6 6gt
noremap <leader>7 7gt
noremap <leader>8 8gt
noremap <leader>9 9gt
noremap <leader>0 :tablast<CR>
"" Go to last active tab 
au TabLeave * let g:lasttab = tabpagenr()
nnoremap <C-Left> :tabprevious<CR>
nnoremap <C-Right> :tabnext<CR>

"" http://vi.stackexchange.com/a/7787/2923 
function! OpenSelectionAsTabs() range
    let tabnr = tabpagenr()
    for line in range(a:firstline, a:lastline)
        "" to allow whitespaces in names http://vi.stackexchange.com/a/7865/2923
        execute "tabedit " . fnameescape(getline(line))
        execute "tabp"
    endfor
endfunction

nnoremap <leader>gf :'<,'>call OpenSelectionAsTabs()<CR>
vnoremap <leader>gf :call OpenSelectionAsTabs()<CR>

Press Space + [1-9] to browse the tabs in Vim.

Known Limitations: none

Systems: Ubuntu 14.04, Ubuntu 16.04, Debian 8.5, ...
Hardware: Asus Zenbook UX303UA

0

You must log in to answer this question.

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