Search for files, file contents and text manipulation

Search for files

Linux - Search for files in the entire disk

If you are not sure where in the file system a given file is, these are helpful. Because they can scan the entire file system and show the location of the file to you.

99% of the times, fzf does the job pretty good.

fzf

  1. https://github.com/junegunn/fzf
  2. https://github.com/junegunn/fzf?tab=readme-ov-file#installation
  3. https://github.com/junegunn/fzf/wiki
  4. https://www.redhat.com/en/blog/fzf-linux-fuzzy-finder

Usage:

  1. Navigate to the directory you want to search in and then type fzf
  2. This will launch the fzf interactive filter program for any kind of list; files, command history, processes, hostnames, bookmarks, git commits, etc.

broot

Broot (Directory navigators)

There are references to many other tools in this post. Take a look at them.

  1. https://www.reddit.com/r/linux/comments/adfu3o/announce_broot_a_clearer_mix_of_tree_cd_and_fuzzy/
  2. https://github.com/Canop/broot?tab=readme-ov-file
  3. https://dystroy.org/broot/navigation/
  4. https://andydecleyre.github.io/this-and-that/posts/broot-zsh/
  5. https://flox.dev/blog/totally-transform-your-terminal-workflow-with-broot-and-flox/

locate

Faster alternatives to find and locate? https://superuser.com/questions/341232/faster-alternatives-to-find-and-locate

Run this command at the root:

locate fileName

find

Faster alternatives to find and locate? https://superuser.com/questions/341232/faster-alternatives-to-find-and-locate

find -name input-folder

Search for files in a directory:

Type this to find all the files with a given extension:

find . -type f -name "*.txt"

See how fzf will work with Vifm and Broot

Windows

How to find files on windows modified/created after a given date using the command line? You can use PowerShell to do this :

Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -ge "12/27/2016" }

This also works with time:

{ $_.LastWriteTime -ge "12/27/2016 20:00:00" }

In Windows, find a Directory/Folder with CMD prompt without knowing full path

dir <Folder Name> /AD /s

Search file contents

Using Integrated Development Environment (IDEs) and Text Editors

  1. emacs - search and replace
  2. vim - search

grep

ripgrep

How to tell if ripgrep is installed in the computer?

Installation

From terminal, try looking for the help menu: rg -help

  1. If it is not installed, install it using the instructions provided on the GitHub page for ripgrep. https://github.com/BurntSushi/ripgrep#installation
  2. Try not to use homebrew or the package managers supported by the distribution.

For Ubuntu, it is: sudo apt-get install ripgrep

  • Installation in Windows

    Unzip the folder, put the folder in a convenient location and add the location to “Path”.

How to use ripgrep from terminal?

https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md

For case insensitive search, use the -i flag.

Regular Expressions (regex)

Search only in certain file types

  1. https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md#manual-filtering-file-types
  2. https://josephwoodward.co.uk/2017/09/turbo-charging-command-line-ripgrep
  3. https://www.grailbox.com/2018/08/restricting-ripgrep-to-certain-file-types/

rga

https://github.com/phiresky/ripgrep-all

Useful for searching documents of other formats in addition to plain text files that ripgrep can search.

Windows

How to do file search for a string in a folder (including its sub folders) in Windows?

C:\Users\user\Desktop\FolderName>findstr /S /I /M /C:"search text" *.*

Replace file contents

Using Integrated Development Environment (IDEs) and Text Editors

  1. emacs - text manipulation
  2. emacs - Rename something in an entire project
  3. vim - text manipulation

From terminal in a non-interactive way

Sometimes, we can use terminal commands to do this in a non-interactive way

sed command

sed, short for stream editor, is a powerful command-line utility used for text manipulation. It operates on a stream of text, performing actions like searching, replacing, inserting, and deleting text based on specified patterns and commands.

https://www.gnu.org/software/sed/manual/sed.html

For example, see emacs - Rename something in an entire project. In org-mode directory, we can replace the occurences of org-link-expand-abbrev by org-link-RENAMED, by running the following command line (in a terminal):

find . -type f -print0 | xargs -0 sed -i 's/org-link-expand-abbrev/org-link-RENAMED/g'

or

grep -rlZ 'org-link-expand-abbrev' | xargs -0 sed -i 's/org-link-expand-abbrev/org-link-RENAMED/g'