Hot answers tagged

193 votes

How do I grep recursively through .gz files?

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 ...
user avatar
89 votes
Accepted

Unhighlight last search in 'less'

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 ...
user avatar
86 votes

How do I grep recursively through .gz files?

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. ...
user avatar
  • 73.9k
70 votes
Accepted

How do I use ag to look for text in files with certain extensions?

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 ...
user avatar
  • 73.9k
66 votes
Accepted

ripgrep: print only filenames matching pattern

Print only the filename where the contents match: rg -l regex # OR: long-option form rg --files-with-matches regex Print only the directory entries (filenames) which match the given pattern under <...
user avatar
  • 25.4k
43 votes
Accepted

How to search the whole manual pages on Linux?

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 ...
user avatar
  • 65k
42 votes

Limit grep context to N characters on line

Try to use this one: grep -r -E -o ".{0,10}wantedText.{0,10}" * -E tells, that you want to use extended regex -o tells, that you want to print only the match -r grep is looking for result ...
user avatar
  • 1,015
38 votes
Accepted

Limit grep context to N characters on line

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 ...
user avatar
  • 37.6k
38 votes
Accepted

Use case sensitive search for lower-case string using smartcase

Use \C (uppercase) in your search pattern See :help /ignorecase: "\C" does the opposite: Force matching case for the whole pattern.
user avatar
  • 12.1k
32 votes

Limit grep context to N characters on line

Piping stdout to cut with the -b flag; you can instruct grep's output to only bytes 1 through 400 per line. grep "foobar" * | cut -b 1-400
user avatar
30 votes
Accepted

how to find file defining an alias

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 ...
user avatar
29 votes

How can I grep in PDF files?

pdfgrep was written for exactly this purpose and is available in Ubuntu. It tries to be mostly compatible to grep and thus provides "the power of grep", only specialized for PDFs. That includes ...
user avatar
  • 391
24 votes

How can I search history with text already entered at the prompt in zsh?

If you are using oh-my-zsh, add history-substring-search to the plugins=(...) line. Then add bindkey "^[[A" history-substring-search-up bindkey "^[[B" history-substring-search-down somewhere below ...
user avatar
  • 1,749
21 votes
Accepted

Trying to use `pwd` inside an alias giving unexpected results

Use single quotes to avoid shell expansion at time of definition alias search='find `pwd` -name '
user avatar
  • 2,819
18 votes
Accepted

How to grep '$$$$$'?

It seems that $ is a special character for end of line? Yep, exactly. And there's an end-of-line on each and every line. You'll need to use \$\$\$\$\$ as the pattern, or use grep -F '$$$$$', to tell ...
user avatar
  • 119k
15 votes

Quickly find which file(s) belongs to a specific inode number

btrfs man btrfs-inspect-internal says: inode-resolve [-v] <ino> <path> (needs root privileges) resolve paths to all files with given inode number ino in a given ...
user avatar
  • 25.4k
15 votes

How can I "grep" patterns across multiple lines?

Simply a normal grep which supports Perl-regexp parameter P will do this job. $ echo 'abc blah blah blah def blah blah blah' | grep -oPz '(?s)abc.*?def' abc blah blah blah def (?s) called DOTALL ...
user avatar
  • 3,515
15 votes

How to search a log file in Linux and escape special characters like square brackets (i.e. [ and ])

The manual does mention you can switch off regular expression search for the whole search string by pressing ^R (CTRL+R) after you press /.
user avatar
  • 259
15 votes
Accepted

How to find files that don't have empty line at the end?

To clarify, the LF (aka \n or newline) character is the line delimiter, it's not the line separator. A line is not finished unless it's terminated by a newline character. A file that only contains a\...
user avatar
15 votes
Accepted

less - How to get matching line not be the top line on screen?

You want the -j option. See under "OPTIONS" in the man page: -jn or --jump-target=n Specifies a line on the screen where the "target" line is to be positioned. The target line ...
user avatar
  • 4,931
15 votes
Accepted

How to search for multiline text files containing a set of words (e.g., AAA & (BBB | CCC) & ~DD)?

Your solution is pretty legible for the task, in my opinion. However, it's slow, because it spawns 3 processes per file. I reckon Awk is better suited here because it will allow to read a whole batch ...
user avatar
  • 17.5k
14 votes
Accepted

How to search for text in a file ignoring newlines?

The GNU grep can do it grep -z 'is\san\sexample\sfile.' file To fulfill some points which arise in comments there are some modifications to script: grep -oz '^[^\n]*\bis\s*an\s*example\s*file\.[^\...
user avatar
  • 14.4k
13 votes

How can I search history with text already entered at the prompt in zsh?

I'd highly recommend using "$terminfo[kcuu1]" or "$key[Up]" rather than hard-coded stuff like "^[[A" which may or may not work on any particular system. Check out /etc/zsh/zshrc for more keys. Here'...
user avatar
  • 319
13 votes
Accepted

Is there a way to search manpages for typical use scenario?

I think what most closely fits what you're looking for is apropos (a.k.a. man -k), which will search the one-line descriptions preceding every manpage. If you want to search through whole manpages, ...
user avatar
12 votes

How can I search history with text already entered at the prompt in zsh?

Third part solutions 1 - What about using zsh-autosuggestions? The thing is, for each command you type this plugin will show you some suggestions that could be accepted or not. Try it out and give us ...
user avatar
12 votes
Accepted

grep --exclude does not exclude my file

The --exclude option takes globs that are matched against file names, not directories or full paths: --exclude=GLOB Skip files whose base name matches GLOB (using wildcard ...
user avatar
  • 219k
12 votes
Accepted

ls *.csv --ignore="*pattern*" returns files which contain "pattern"

The flag is described in the ls man page like so (emphasis is mine) ... do not list implied entries matching shell PATTERN The man page doesn't have much to say about "implied" so what the heck ...
user avatar
  • 4,979
11 votes
Accepted

How to tell "grep -w" what characters make up "words"?

In versions prior to 2.19, GNU grep's -w would only consider single-byte character alnums and underscore (so in UTF-8 locales, only the 26+26+10+1 (ASCII letters, digits and underscore)) as word ...
user avatar
11 votes
Accepted

Print only the Nth line before each line that matches a pattern

A buffer of lines needs to be used. Give a try to this: awk -v N=4 -v pattern="example.*pattern" '{i=(1+(i%N));if (buffer[i]&& $0 ~ pattern) print buffer[i]; buffer[i]=$0;}' file Set N ...
user avatar
  • 1,101
11 votes

Why is the command "find | grep 'filename'" so much slower than " find 'filename' "?

(I'm assuming GNU find here) Using just find filename would be quick, because it would just return filename, or the names inside filename if it's a directory, or an error if that name did not exist ...
user avatar
  • 276k

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