Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The Best Way to Find Files and Manipulate Them

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

** Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement! **

There are many cool (command-line) tools which can help you quickly find/locate files. Notice that they can all be combined with fzf to make it interactive.

  1. fdfind

  2. find

  3. locate

  4. osquery

  5. fselect

  6. ripgrep

Those tools can be combined with the pipe operator | to do further filtering or manipulation. However, after trying all tools I have to state that the best way for a Python user is leveraging the pathlib module in a Jupyter/Lab notebook (or in a IPython shell). Even though you might have to write slight more code in Python than in other command-line tools, Python code has the following advantages.

  1. Python code is much more intutive to understand. You do not have to remember weird command-line options or to handle corner cases.

  2. Python code is much more flexible especially when used in a notebook or in a IPython shell. You can easily achieve more complicated operations in Python code. If an operation requires lots of Python code, you can also encapsulate it into a Python module. What’s more, you can mix shell command code in your Python code if you are using a notebook or a IPython shell, which makes things much more convenient.

  3. You do have to worry about side effect (e.g., spaces, special character escaping, etc.) caused by shell.

Below are some examples.

  1. Find all files with the extension .out in the current directory and its subdirectory, and then make them executable.

  1. Find files whose names contain “conflicted” and remove them.

rm: missing operand
Try 'rm --help' for more information.
/bin/bash: fselect: command not found
rm: missing operand
Try 'rm --help' for more information.
  1. Find files with 0 size.

./abc/a.out
/bin/bash: fselect: command not found
abc/a.out
  1. Find empty directories.

./.ipynb_checkpoints
.ipynb_checkpoints