<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Grep on Michael’s Domain</title><link>https://jeltsch.org/en/tags/grep/</link><description>Recent content in Grep on Michael’s Domain</description><generator>Hugo</generator><language>en-us</language><copyright>Copyright © 2002 - 2026 Michael Jeltsch.</copyright><lastBuildDate>Fri, 24 Jul 2026 00:18:18 +0300</lastBuildDate><atom:link href="https://jeltsch.org/en/tags/grep/index.xml" rel="self" type="application/rss+xml"/><item><title>Finding files with find</title><link>https://jeltsch.org/en/finding_files_with_find/</link><pubDate>Mon, 24 Sep 2007 00:00:00 +0000</pubDate><guid>https://jeltsch.org/en/finding_files_with_find/</guid><description>&lt;p&gt;Finding files with the &lt;em&gt;find&lt;/em&gt; command is actually more difficult than it should be. This finds and lists the largest files (more than 200 MB) on the whole file system:&lt;code&gt;find / -type f -size +200000k -exec ls -lh {} \; | awk '{ print $9 &amp;quot;: &amp;quot; $5 }'&lt;/code&gt;The same, but simpler syntax:&lt;code&gt;find / -size +200M -ls&lt;/code&gt;This finds the most recently changed files under the current directory:&lt;code&gt;find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort&lt;/code&gt;And this command pipes the results of the &lt;em&gt;find&lt;/em&gt; command into another command. First &lt;em&gt;find&lt;/em&gt; looks for Quicktime movies (based on the file extension .mov) and then &lt;em&gt;ffmpeg&lt;/em&gt; converts them into avi files.&lt;code&gt;find . -name '*.mov' -exec sh -c 'ffmpeg -i &amp;quot;$0&amp;quot; -sameq &amp;quot;${0%%.mov}.avi&amp;quot;' {} \;&lt;/code&gt;This commands finds and lists all filenames that contain the string &amp;lsquo;bitcoin&amp;rsquo;:&lt;code&gt;find . -name '*bitcoin*' -ls&lt;/code&gt;And this command finds Quicktime movies and deletes them:&lt;code&gt;find . -name '*.mov' -exec sh -c 'rm &amp;quot;$0&amp;quot;' {} \;&lt;/code&gt;And this finds files that have been modified (created or updated) during the last 2 minutes:&lt;code&gt;find . -cmin 2&lt;/code&gt;This finds crap which you copied over from a Macintosh and which you don&amp;rsquo;t need under Linux:&lt;code&gt;find . -name '*.DS_Store' -type f -delete&lt;/code&gt;Similar story here:&lt;code&gt;find . -name '._*' -type f -delete&lt;/code&gt;When being in the root directory, this should find all files with the name &amp;lsquo;syncthing&amp;rsquo;:&lt;code&gt;find . -name 'syncthing' -ls&lt;/code&gt;And this finds all files larger than 200MB in the /home directory:&lt;code&gt;find /home -size +200M -ls&lt;/code&gt;Find recursively all files that end in .gz and count them:&lt;code&gt;find . -name '*.gz' | wc -l&lt;/code&gt;Find recursively all files that do NOT end in .gz and count them:&lt;code&gt;find . -not -name '*.gz' | wc -l&lt;/code&gt;When you want to search recursively through the content of text files, you need to use the grep command:&lt;code&gt;grep -rnw '/path/to/target-directory' -e 'pattern'&lt;/code&gt;When you search for PDF files modified in October 2024:&lt;code&gt;sudo find / -type f -name &amp;quot;*.pdf&amp;quot; -newermt &amp;quot;2024-10-01&amp;quot; ! -newermt &amp;quot;2024-11-01&amp;quot; 2&amp;gt;/dev/null&lt;/code&gt;&lt;/p&gt;</description></item><item><title>grep to search for file content or rpm -qa output</title><link>https://jeltsch.org/en/grep_to_search_for_file_content_or_rpm_qa_output/</link><pubDate>Fri, 25 May 2007 00:00:00 +0000</pubDate><guid>https://jeltsch.org/en/grep_to_search_for_file_content_or_rpm_qa_output/</guid><description>&lt;p&gt;In order to search a specific directory (path_to_directory) and all its subdirectories (-r for recursive) for files that contain a specific text string, use the following command:&lt;code&gt;grep -r &amp;quot;textstring&amp;quot; path_to_directory&lt;/code&gt;Also very useful is to pipe the output of another process into grep. E.g. you want to know whether a specific rpm package is installed on your system, but you don&amp;rsquo;t remember the exact name. Thus you cannot use the &amp;ldquo;rpm -q packagename&amp;rdquo;. In that cast you can ask rpm for all installed packages (rpm -qa) and then pipe this output to grep and let grep look for a substring from the packagename that you do remember:&lt;code&gt;rpm -qa | grep substring&lt;/code&gt;E.g. when the package modutils-2.4.22-8 is installed you have to ask rpm:&lt;code&gt;rpm -q modutils OR rpm -q modutils-2.4.22&lt;/code&gt;in order to get an answer. The request rpm -q modutils-2.4 would be: The package modutils-2.4 is not installed. Thus if you don&amp;rsquo;t know the exact name of the package, it is a safe bet to grep the rpm output for something you remember exactly.&lt;/p&gt;</description></item><item><title>Recursive grep</title><link>https://jeltsch.org/en/recursive_grep/</link><pubDate>Thu, 05 Apr 2007 00:00:00 +0000</pubDate><guid>https://jeltsch.org/en/recursive_grep/</guid><description>&lt;p&gt;grep (case-insensitive, recursive) grep doesn&amp;rsquo;t offer any possibility to recursively descent down the directory tree. The following command searches e.g. just thru all the files in the current directory:&lt;code&gt;grep bacteria *&lt;/code&gt;To make the search case-insensitive, one can use the i flag:&lt;code&gt;grep -i bacteria *&lt;/code&gt;But if you want to do a recursive search you have to use a quite complicated construction of UNIX commands to achieve this goal:&lt;code&gt;find ~/Mail -type f -exec grep 'bacteria' {} \; -print&lt;/code&gt;This command searches recursively thru the Mail directory of the current user and prints all the lines that contain the word bacteria.&lt;/p&gt;</description></item></channel></rss>