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¶
is_*
columns supporting comparing with1
,true
andyes
, etc.where name like '%.markdown'
can be written aswhere name like %.markdown
and is equivalent towhere name = '*.markdown'
It is suggested that you use
gt
instead of>
as>
is interpreted as the redirect operator in shell if no quotes is used.
!fselect * from . where is_file = 1
!fselect name from . depth 1 where is_file = 1
- Find all files with the extension
.markdown
in the current directory and its subdirectory.
!fselect name from . where is_file = 1 and name like %.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.
!fselect name from . where is_file = 1 and name like %.markdown | grep cargo
Of course, this example is not very useful as it is equivalent to the following simpler command (no need of grep)
!fselect name from . where is_file = 1 and name like %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.
!fselect name from . where is_file = 1 and name like %.markdown | xargs grep cargo
File Size¶
- Find files with size greater than 10 (bytes).
!fselect name, size from . where is_file = 1 and size gt 10
!fselect "name, size from . where is_file = 1 and size > 10"
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
.
- Find files with size greater than 5k (bytes).
!fselect name, size from . where is_file = 1 and size gt 5k
Find empty directories.
:::bash find / -type d -empty
Time Related¶
!fselect name, accessed from . where is_file = 1 and accessed gt '1 hour ago'
!fselect "name, accessed from . where is_file = 1 and accessed gt '1 hour ago'"
!fselect "name, accessed from . where is_file = 1 and accessed lt '1 hour ago'"
File Type Related Finding¶
Find broken symbolic links.
:::bash find . -xtype l # or find -L . -type l
Find executable files in current directory
:::bash find . -maxdepth 1 -type f -executable
User Related Finding¶
Find files that belong to a user but writable by its group or other people.
:::bash find /path/to/file -user user1 -perm /022
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/*'