Posted: . At: 2:10 PM. This was 8 years ago. Post ID: 9333
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

Useful grep tricks and tips for the Linux user.

The grep command on Linux is very good for filtering text to find certain strings. This is mostly used for filtering the output of a command to narrow down what the user is looking for.

Find all instances of a word in a text file.

jason@DESKTOP-R72SPS3:/usr/share/X11$ grep "dark" rgb.txt
 47  79  79             dark slate gray
 47  79  79             dark slate grey
 72  61 139             dark slate blue
  0 206 209             dark turquoise
  0 100   0             dark green
 85 107  47             dark olive green
143 188 143             dark sea green
189 183 107             dark khaki
184 134  11             dark goldenrod
233 150 122             dark salmon
255 140   0             dark orange
153  50 204             dark orchid
148   0 211             dark violet
169 169 169             dark grey
169 169 169             dark gray
0     0 139             dark blue
0   139 139             dark cyan
139   0 139             dark magenta
139   0   0             dark red

Find an instance of a word with a number after it.

jason@DESKTOP-R72SPS3:/usr/bin$ ls | grep "ppm[0-9]"
ppm3d

Delete all mkv files in a directory using grep and xargs. You must be careful with this command, this will not prompt you before deleting files.

jason@jason-desktop:~/Videos$ ls | grep "mkv" | xargs rm

How to return the names of files that do not contain a certain search string.

jason@jason-desktop:~/Documents$ grep -L "linux" *.txt
blog.txt
unix_passwords.txt
unix_users.txt

This will return all files that do contain a certain search string.

jason@jason-desktop:/etc$ grep -l "ubuntu" *.conf
appstream.conf

List all directories or files with a number after the name, this is useful if it is a very long list.

jason@jason-desktop:/etc$ ls | grep "rc[0-9]"
rc0.d
rc1.d
rc2.d
rc3.d
rc4.d
rc5.d
rc6.d

This is how to fine tune that and only list five of them.

jason@jason-desktop:/etc$ ls | grep "rc[0-4]"
rc0.d
rc1.d
rc2.d
rc3.d
rc4.d

Print a sorted alphabetical listing of all files that match a certain string or word.

jason@jason-desktop:/usr/share/backgrounds$ ls | grep by | sort | uniq -c | sort -n
      1 160218-deux-two_by_Pierre_Cante.jpg
      1 Abstract_Ubuntu_by_Marek_Koteluk.jpg
      1 Black_hole_by_Marek_Koteluk.jpg
      1 Blue_by_dariuskws.jpg
      1 Breaker_by_Lyle_Nel.jpg
      1 Cielo_estrellado_by_Eduardo_Diez_Viñuela.jpg
      1 clock_by_Bernhard_Hanakam.jpg
      1 Dans_ma_bulle_by_Christophe_Weibel.jpg
      1 Flora_by_Marek_Koteluk.jpg
      1 Hotel_by_sarcasmrules2011.jpg
      1 Icy_Grass_by_Raymond_Lavoie.jpg
      1 Light_my_fire_evening_sun_by_Dariusz_Duma.jpg
      1 Mediterranean_Sea_by_simosx.jpg
      1 Moss_inflorescence_by_carmelo75.jpg
      1 Night_lights_by_Alberto_Salvia_Novella.jpg
      1 passion_flower_by_Irene_Gr.jpg
      1 Sitting_Here,_Making_Fun_by_Philipp_Haegi.jpg
      1 Spring_by_Peter_Apas.jpg
      1 TCP118v1_by_Tiziano_Consonni.jpg
      1 The_Land_of_Edonias_by_Γιωργος_Αργυροπουλος.jpg
      1 Tramonto_a_Scalea_by_Renatvs88.jpg
      1 Tranquil_by_Pat_David.jpg

Count the number of files with a certain filename.

jason@jason-desktop:/var/log$ ls | grep log | wc -l
42

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.