107 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 <...
Tom Hale's user avatar
  • 30.5k
90 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 ...
don_crissti's user avatar
  • 82.8k
52 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
Eric Leschinski's user avatar
43 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.
Philippos's user avatar
  • 13.5k
23 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 /.
maccam94's user avatar
  • 339
22 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 '
Matija Nalis's user avatar
  • 3,111
18 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 ...
Tom Hale's user avatar
  • 30.5k
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 ...
ilkkachu's user avatar
  • 139k
16 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\...
Stéphane Chazelas's user avatar
16 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 ...
laktak's user avatar
  • 5,976
15 votes
Accepted

How to search for text files containing a set of words (e.g., AAAA & (BBB | CCCCC) & ~DDD)?

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 ...
Quasímodo's user avatar
  • 18.9k
13 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 ...
SergioAraujo's user avatar
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'...
zzxyz's user avatar
  • 319
13 votes
Accepted

Grep lines that match pattern twice

grep -E "(foo.*){2}" file This matches at least 2 times on each line of file or output, you can give minimum number of matches.
Stalin Vignesh Kumar's 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 ...
terdon's user avatar
  • 242k
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 ...
B Layer's user avatar
  • 5,171
11 votes

How can I use grep to search only on the first line of files for a specific string?

Two more alternatives : With awk awk '{if ($0~"pattern") print $0; nextfile;}' mydir/* or if your awk version doesn't support nextfile (thanks to Stéphane Chazelas for the suggestion) : awk 'FNR==...
Skippy le Grand Gourou's 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 ...
Jay jargot's user avatar
  • 1,175
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 ...
Kusalananda's user avatar
  • 334k
11 votes
Accepted

Using anchor ^ pattern when using less / search command

/^-p means there are no characters before -p. But if you look at the man pages, you will see there are some blank characters preceding these flags. The search pattern should be /^\s+-p where: ^ ...
GMaster's user avatar
  • 6,332
11 votes
Accepted

Search PDF files with more than 100 pages and moving them into directory in UNIX/LINUX terminal

The difficult bit here is to count the number of pages in a PDF document. The find utility can't do this by itself, so we need an external tool to do this. On most Unix systems, you will be able to ...
Kusalananda's user avatar
  • 334k
10 votes

grep command to display same word with different spellings

[...] creates a character class, not a group, you want to use a group that contains an | or operator grep 'S\(ee\|i\)ya' emp will do such a group and find those two lines. You can also clean that ...
Eric Renouf's user avatar
  • 18.4k
10 votes

How to list .txt files with specific content?

GNU grep supports recursive searching based on a glob-pattern, e.g.: grep --include='*.txt' -ril ubuntu /path/to/dir If you are not searching for a regular expression, this process will be much ...
Thor's user avatar
  • 17.2k
9 votes
Accepted

Search for special characters using grep

grep "[]:/?#@\!\$&'()*+,;=%[]" Within a bracketed expression, [...], very few character are "special" (only a very small subset, like ], - and ^, and the three combinations [=,...
Kusalananda's user avatar
  • 334k
9 votes

How to remove trailing forward slash

Your command would try to delete a $ followed by a / at the end of the lines in your file. You don't need the initial $ in the regular expression: sed 's/\/$//' myfile.txt >myfile_noslash.txt ...
Kusalananda's user avatar
  • 334k
8 votes

Is there a file search engine like "Everything" in Linux?

I know that this answer is really late, but this might help someone else, especially since there is no real solution till now to this problem. Linux kernel 5.1 Introduced a kernel API called 'fanotify'...
Yehia Amer's user avatar
8 votes

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

That code doesn't work for previous lines. To get lines before the matched pattern, you need to somehow save the lines already processed. Since awk only has associative arrays, I can't think of an ...
terdon's user avatar
  • 242k
8 votes

Print out list of files less than specified file size

Your approach is clumsy (and fair to say wrong). There are dedicated tools for these sort of tasks, find is one of those. For example, to find all files in the current directory that are less than 1 ...
heemayl's user avatar
  • 56.3k
8 votes
Accepted

Extract Sub-Directory Path from Partially Known Directory

Let's start with your filename: $ f=base/app/main/sub/first/tib1.ear To extract the base name: $ echo "${f##*/}" tib1.ear To extract the desired part of the directory name: $ g=${f%/*}; echo "${g#...
John1024's user avatar
  • 74.7k

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