Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Hot answers tagged

166

locate(1) has only one big advantage over find(1): speed. find(1), though, has many advantages over locate(1): find(1) is primordial, going back to the very first version of AT&T Unix. You will even find it in cut-down embedded Linuxes via Busybox. It is all but universal. locate(1) is much younger than find(1). The earliest ancestor of locate(1) ...


142

If you want to grep recursively in all .eml.gz files in the current directory, you can use: find . -name \*.eml.gz -print0 | xargs -0 zgrep "STRING" You have to escape the first * so that the shell does not interpret it. -print0 tells find to print a null character after each file it finds; xargs -0 reads from standard input and runs the command after it ...


135

Install the package pdfgrep, then use the command: find /path -iname '*.pdf' -exec pdfgrep pattern {} + —————— Simpliest way is pdfgrep 'pattern' *.pdf pdfgrep 'pattern' file.pdf


68

There's a lot of confusion here because there isn't just one zgrep. I have two versions on my system, zgrep from gzip and zgrep from zutils. The former is just a wrapper script that calls gzip -cdfq. It doesn't support the -r, --recursive switch.1 The latter is a c++ program and it supports the -r, --recursive option. Running zgrep --version | head -n 1 will ...


59

You can use Alt+u to remove the highlight on last search results. You can highlight them again with Alt+u, it's a toggle. Switching off the highlight does not switch off the status column, showing marks on each line containing a match, if the column is enabled using options -J or --status-column or keys -J. To hide the status column, use -+J. To show ...


44

You can use zle's history-search functionality: bindkey "^[[A" history-beginning-search-backward bindkey "^[[B" history-beginning-search-forward This binds Up and Down (adjust for your own escape sequences) to a history search, backwards and forwards, based upon what has already been entered at the prompt. So, if you were to enter "vim" and hit Up, zsh ...


40

Here is one way to get the exact output you're looking for: $ grep -nFx "$(sort sentences.txt | uniq -d)" sentences.txt 1:This is sentence X 4:This is sentence X Explanation: The inner $(sort sentences.txt | uniq -d) lists each line that occurs more than once. The outer grep -nFx looks again in sentences.txt for exact -x matches to any of these lines -F ...


37

Add ~i (short for ?installed) to match the installed packages whose name contains bash: aptitude search '~i bash' To match whose description contains bash. aptitude search '~i ~d bash' To limit to the ones that are not installed: aptitude search '!~i bash'


37

You can concatenate several paths for grep to look for: grep -r "some string" /code/internal/dev/ /code/public/dev/ /code/tools/


36

You can access this via the forward-search-history function which is bind per default to ctrl+s. Unfortunately ctrl+s is used to signal xoff per default which means you can't use it to change the direction of the search. There are two solutions for solving the problem, one disabling sending the xoff/xon signaling and the other change the keybinding for ...


35

locate uses a prebuilt database, which should be regularly updated, while find iterates over a filesystem to locate files. Thus, locate is much faster than find, but can be inaccurate if the database -can be seen as a cache- is not updated (see updatedb command). Also, find can offer more granularity, as you can filter files by every attribute of it, while ...


33

For an ext4 filesystem, you can use debugfs as in the following example: $ sudo debugfs -R 'ncheck 393094' /dev/sda2 2>/dev/null Inode Pathname 393094 /home/enzotib/examples.desktop The answer is not immediate, but seem to be better than find. The output obtained can be easily parsed to obtain the filename.


33

For quickly getting help on a Bash builtin, use help: help read is what you want. For man-page-like formatting, use help -m read or, even better, help -m read | less If you still insist on looking for it in the man page, I find what quickly gets me to a command's explanation is /^\s*read [[] This works because when a command is first explained, its ...


30

You may have a look at rlocate, a reimplementation of locate that is always up-to-date. Another interesting project is recoll which also supports real time indexing and allows you (like beagle) additionally do full-text searches. Finally I should mention doodle which also supports real time indexing. For doodle there are some nice frontends like catfish. ...


30

There are different regular expression dialects; some (e.g. Perl's) do not require backslashes in the quantification modifier (\d{2}), some (e.g. sed) require two (\d\{2\}), and in Vim, only the opening curly needs it (\d\{2}). That's the sad state of incompatible regular expression dialects. Also note that for matching exact numbers, you have to anchor the ...


29

To search in reverse from your cursor for a word, just use ?. So to find the word "fred" you would issue ?fred. For forward searching you use /, using "fred" as an example again you would issue /fred. If you want to continue searching for the same term, in the same direction you can use the n command. (Or you can issue ? or / without arguments).


29

search and limit can also actually search inside messages, depending on the search patterns you give. From the Patterns subsection of the Mutt reference: ~b EXPR messages which contain EXPR in the message body ~B EXPR messages which contain EXPR in the whole message That is, ~b only searches in the body, whereas ~B also searches in the ...


29

From man man: -K, --global-apropos Search for text in all manual pages. This is a brute-force search, and is likely to take some time; if you can, you should specify a section to reduce the number of pages that need to be searched. Search terms may be simple strings (the default), or regular expressions if the --regex ...


28

Per the manual, you could use ag with -G -G --file-search-regex PATTERN Only search files whose names match PATTERN. e.g. ag -G '\.java$' 'ftp' . Per the same manual It is possible to restrict the types of files searched [...] For a list of supported types, run ag --list-file-types. So you could also run ag --java 'ftp' . though ...


26

It is in general not possible to search for content within a compressed file without uncompressing it one way or another. Since zipgrep is only a shellscript, wrapping unzip and egrep itself, you might just as well do it manually: for file in *.zip; do unzip -c "$file" | grep "ORA-1680"; done If you need just the list of matching zip files, you can use ...


26

Incremental search has this feature, but the replace functions don't. Fortunately, incremental search does have a way to switch to replace mode once you've selected a search term. So: Press C-s to switch to incremental search mode Press C-w to yank the current word into the search buffer You can keep pressing it to append multiple words, and you can also ...


25

You can do a search from the command line: less -ppattern filename Or, once inside less, use / followed by your pattern to do interactive searching (forwards). n and N repeat the search in the forward and reverse direction, respectively. That's the bare minimum you need to know; there are many more commands for more complex or specific searches. Edit: To ...


25

zsh -x 2>zsh.trace exit grep 'alias.*subl' zsh.trace The -x option causes zsh to print out every command that it executes on stderr. Any command that was executed from reading a file has a prefix with the file name and line. So look for the alias definition in the trace file and you'll know where it was defined.


24

Here's a sed one that will give you grep-like behavior across multiple lines: sed -n '/foo/{:start /bar/!{N;b start};/your_regex/p}' your_file How it works -n suppresses the default behavior of printing every line /foo/{} instructs it to match foo and do what comes inside the squigglies to the matching lines. Replace foo with the starting part of the ...


24

Use \C (uppercase) in your search pattern See :help /ignorecase: "\C" does the opposite: Force matching case for the whole pattern.


23

Have you tried ? fc-list | grep -i "media" Also give a try to fc-scan, fc-match


22

With GNU grep: N=10; grep -roP ".{0,$N}foo.{0,$N}" . Explanation: -o => Print only what you matched -P => Use Perl-style regular expressions The regex says match 0 to $N characters followed by foo followed by 0 to $N characters. If you don't have GNU grep: find . -type f -exec \ perl -nle ' BEGIN{$N=10} print if s/^.*?(.{0,$N}foo.{0,$...


20

Try the pgrep command, which will output the PID of the command you're interested in. pgrep selenium To actually kill the process, use the companion pkill command. pkill selenium


20

With GNU tools: find . -type f -exec grep -lZ FIND {} + | xargs -r0 grep -l ME You can do standardly: find . -type f -exec grep -q FIND {} \; -exec grep -l ME {} \; But that would run two greps per file. To avoid running that many greps and still be portable while still allowing any character in file names, you could do: convert_to_xargs() { sed "s/[[...


20

Try this trick: man chmod | less +'/a\+x' or man chmod | more +'/a\+x' With a backslash before the + sign because what comes after / is an extended regular expression.


Only top voted, non community-wiki answers of a minimum length are eligible