Use fselect to Find Files

Posted on Apr 05, 2021 in Computer Science

Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.

It is suggested that you use Python (the pathlib module) instead of the fselect command to find files.

Tips and Traps

  1. is_* columns supporting comparing with 1, true and yes, etc.

  2. where name like '%.markdown' can be written as where name like %.markdown and is equivalent to where name = '*.markdown'

  3. It is suggested that you use gt instead of > as > is interpreted as the redirect operator in shell if no quotes is used.

In [13]:
!fselect * from . where is_file = 1
tips-on-cargo.markdown	tips-on-rustc.markdown	use-fselect-to-find-files.ipynb
In [16]:
!fselect name from . depth 1 where is_file = 1
tips-on-cargo.markdown
tips-on-rustc.markdown
use-fselect-to-find-files.ipynb
  1. Find all files with the extension .markdown in the current directory and its subdirectory.
In [26]:
!fselect name from . where is_file = 1 and name like %.markdown
tips-on-cargo.markdown
tips-on-rustc.markdown

The pipe operator | is very useful for piping the result of a command to another one. Below is an example of find all markdown files in the current directory and keep only those that have cargo in the name.

In [39]:
!fselect name from . where is_file = 1 and name like %.markdown | grep cargo
tips-on-cargo.markdown

Of course, this example is not very useful as it is equivalent to the following simpler command (no need of grep)

In [41]:
!fselect name from . where is_file = 1 and name like %cargo%.markdown
tips-on-cargo.markdown

However, the below example is an useful one which find all markdown files in the current directory and then find lines containing the keyword cargo in those files.

In [40]:
!fselect name from . where is_file = 1 and name like %.markdown | xargs grep cargo
tips-on-cargo.markdown:Slug: tips-on-cargo
tips-on-cargo.markdown:Tags: Computer Science, programming, rust, cargo
tips-on-cargo.markdown:    cargo init
tips-on-cargo.markdown:    cargo new project_name
tips-on-cargo.markdown:    cargo run
tips-on-cargo.markdown:    cargo build
tips-on-cargo.markdown:    cargo build --release
tips-on-rustc.markdown:Tags: Computer Science, programming, Rust, rustc, optimization, cargo
tips-on-rustc.markdown:    and `cargo build --release` uses the release profile which defaults to `-C opt-level=3`.

File Size

  1. Find files with size greater than 10 (bytes).
In [35]:
!fselect name, size from . where is_file = 1 and size gt 10
tips-on-cargo.markdown	403
tips-on-rustc.markdown	1005
use-fselect-to-find-files.ipynb	7505
use-fselect-to-find-files-checkpoint.ipynb	1121
In [37]:
!fselect "name, size from . where is_file = 1 and size > 10"
tips-on-cargo.markdown	403
tips-on-rustc.markdown	1005
use-fselect-to-find-files.ipynb	7505
use-fselect-to-find-files-checkpoint.ipynb	1121

Notice that the following query won't work.

fselect name, size from . where is_file = 1 and size > 10

It is interprecated as redirecting the result of fselect name, size from . where is_file = 1 and size into a file named 10.

  1. Find files with size greater than 5k (bytes).
In [38]:
!fselect name, size from . where is_file = 1 and size gt 5k
use-fselect-to-find-files.ipynb	8169
  1. Find empty directories.

     :::bash
     find / -type d -empty
In [46]:
!fselect name, accessed from . where is_file = 1 and accessed gt '1 hour ago'
[/home/dclong/.cargo/registry/src/github.com-1ecc6299db9ec823/fselect-0.7.4/src/parser.rs:48] fields = [
    Expr {
        left: None,
        arithmetic_op: None,
        logical_op: None,
        op: None,
        right: None,
        minus: false,
        field: Some(
            Name,
        ),
        function: None,
        args: None,
        val: None,
    },
    Expr {
        left: None,
        arithmetic_op: None,
        logical_op: None,
        op: None,
        right: None,
        minus: false,
        field: Some(
            Accessed,
        ),
        function: None,
        args: None,
        val: None,
    },
]
[/home/dclong/.cargo/registry/src/github.com-1ecc6299db9ec823/fselect-0.7.4/src/parser.rs:49] roots = [
    Root {
        path: ".",
        min_depth: 0,
        max_depth: 0,
        archives: false,
        symlinks: false,
        gitignore: None,
        hgignore: None,
        dockerignore: None,
        traversal: Bfs,
        regexp: false,
    },
]
query: Could not parse tokens at the end of the query
In [48]:
!fselect "name, accessed from . where is_file = 1 and accessed gt '1 hour ago'"
use-fselect-to-find-files.ipynb	2021-04-04 22:52:11
use-fselect-to-find-files-checkpoint.ipynb	2021-04-04 22:48:11
In [49]:
!fselect "name, accessed from . where is_file = 1 and accessed lt '1 hour ago'"
tips-on-cargo.markdown	2021-04-02 08:16:26
tips-on-rustc.markdown	2021-04-02 08:16:26
  1. Find broken symbolic links.

     :::bash
     find . -xtype l
     # or
     find -L . -type l
  2. Find executable files in current directory

     :::bash
     find .  -maxdepth 1 -type f -executable
  1. Find files that belong to a user but writable by its group or other people.

    :::bash
    find /path/to/file -user user1 -perm /022
  2. Check file type of all files under the current directory.

    :::bash
    find . -type f | xargs file

-perm mode: File's permission bits are exactly mode (octal or symbolic). -perm -mode: All of the permission bits mode are set for the file. -perm /mode: Any of the permission bits mode are set for the file. a little bit trick about how to understand the last 2 permission criterias. as suggested, think in terms of permission BITs (0/1)

The following command finds all files that readable or writable by the group or (readable or writable) by others.

:::bash
find /path/to/file -user user1 -perm /066

The following command find all files that readable and writable by the group and (readable and writable) by others.

:::bash
find /path/to/file -user user1 -perm -066

The following command find all files that readable or writable by the group and (readable or writable) by others.

:::bash
find /path/to/file -user user1 -perm /060 -perm /006


Find Python scripts in the current directory recursively but ignore those under directories with the name .ipynb_checkpoints.

:::bash
find . -type f -iname '*.py' -not -path '*/.ipynb_checkpoints/*'
In [ ]: