Linux: list the files and folders in a directory by date
How to list the files and folders in a directory by date…
Readgrep on Linux, a remarkably useful command that will save you loads of time.
grep is a magic command. It lets you find a piece of code or a snippet of text very quickly across thousands of files.
grep takes its name from Global Regular Expression Print,
But here's where it gets really cool: grep isn't just a word finder. It's extremely flexible thanks to its options:
-i (ignore case): Don't want to bother with upper and lower case? This option ignores all that. You search for "pattern", it finds "Pattern", "PATTERN", whatever!
-r (recursive): Say you have a folder full of stuff and you want to dig through all of it? This option lets you dive into the subfolders.
-v (invert match): This is reverse magic. Instead of finding, it excludes. Want everything except the lines containing "this word"?
-n (line number): Looking for a needle in a haystack? This option numbers the matching lines. "Ah, it's on line 42!"
-E (extended regex): Are you a regular expression pro? Bingo! This option gives you even more power to find trickier things.
-A and -B (after and before): Want more context around your results? These options show you a few lines before and after the line containing your precious text.
-c (count): No need to read every line, you just want to know how many? This option counts the matching lines. Easy as pie!
-F (Fixed strings) : This tells grep to treat the search pattern as a fixed string rather than as a regular expression. It is especially useful for searching patterns that could otherwise be wrongly interpreted as regular expressions, or when you want to guarantee a literal match.
- l : The -l option of the grep command stands for "list matching files".
It tells grep to print only the names of the files that contain the searched string, instead of printing every line that matches the pattern in those files.
--include='*.html' : this option restricts the search to a specific file type. In this example, only files with an html extension.
So, in practice, how do you use it?
Search for a word in a file:
grep "motif" fichier.txt
Search everywhere in a folder and its subfolders:
grep -r "motif" dossier
Ignore upper and lower case:
grep -i "motif" fichier
Exclude lines:
grep -v "motif" fichier
Use powerful regular expressions:
grep -E "motif1|motif2" fichier
Show the number of matching lines:
grep -c "motif" fichier
Of course, options can be combined, for example:
grep -rin -A5 -B5 toto *
Shows every file containing "toto" in the current directory and its subdirectories, with 5 lines of context before and after the match.
It also shows the line number where the searched expression appears.
grep -rFl --include='.js' --include='.html' tartempion /var/www/monrepertoire
The command prints the paths of the .html and .js files in the /var/www/monrepertoire directory (and its subdirectories) that contain the exact string tartempion.
You can also chain grep commands to narrow down a search:
grep -ri "motif" fichier | grep toto
Shows every file containing "motif" in the current directory and its subdirectories, AND that also contains the expression "toto".
There you go! With grep, it's like having an assistant that finds what you're looking for in your tons of text or files, without the headache. So, get typing and let the search begin! ?️♂️✨
Website design, e-commerce or SEO: our web agency in Paris, Besançon and Normandy can help.
Free quoteHow to list the files and folders in a directory by date…
ReadLinux: find the size of the folders and files in a directory…
Read