find-filter - Linux


Overview

find-filter allows the user to operate on specified files/directories based on a given filter expression. This tool is specifically designed for searching and filtering directory hierarchies, making it highly effective for tasks such as file management, cleanup, and data extraction.

Syntax

find-filter [-di] [-e <filter>] [-f <file>] [-h <home>]  [-o <operation>] [-s]
               [-t <type>] [-v] <path/to/directory>

Options/Flags

Required Arguments:

  • <path/to/directory>: Specify the starting directory for the search.

Options:

  • -d: Only consider directories in file operations.
  • -e : Specify a filter expression to match files/directories. See [Filter Expressions](#Filter Expressions) for details.
  • -f : Read filter expressions from the specified file.
  • -h : Change the home directory to the specified path.
  • -i: Ignore case when matching file/directory names.
  • -o : Specify the operation to perform on matched files/directories. Options: print, delete, move <target_dir>, or copy <target_dir>.
  • -s: Print only the list of files matching the filter without performing any operations.
  • -t : Filter by file type. Options: f for files, d for directories, and l for symlinks.
  • -v: Display verbose output.

Filter Expressions

Filter expressions use a simple syntax to match file/directory attributes and metadata.

Syntax:

[<attr1> <op1> <val1>] [<attr2> <op2> <val2>] ...

Attributes:

  • name: File/directory name.
  • size: File size in bytes.
  • type: File type (f, d, l).
  • mtime: Modification time.
  • ctime: Creation time.
  • atime: Last access time.

Operators:

  • =: Equality (name = "foo")
  • !=: Inequality (size != 1024)
  • <: Less than (mtime < 1660741600)
  • <=: Less than or equal to (size <= 100000)
  • >: Greater than (atime > 1660741600)
  • >=: Greater than or equal to (size >= 100000)
  • *: Contains (name * ".txt")
  • @: Filename extension (name @ ".zip")

Combining Expressions:
Expressions can be combined using logical operators:

  • &: AND (eg., name * ".txt" & size > 100000)
  • |: OR (eg., name * ".txt" | name * ".csv")
  • !: NOT (eg., ! name @ ".log")

Examples

Delete files ending in .log:

find-filter -o delete -e "name @ '.log'" /home

Copy files larger than 1MB to /tmp:

find-filter -o copy /tmp -e "size > 1000000" /home/user

Find and print directories created after a specific date:

find-filter -s -e "ctime > 1660741600 & type = d" /

Move all .zip files in a directory to /archives:

find-filter -o move /archives -e "type = f" -t l /home/user/Documents

Common Issues

  • Matching no files: Verify the filter expression syntax and ensure it matches the desired criteria.
  • Permission denied: Ensure you have sufficient permissions to perform the specified operation on the files/directories.
  • Invalid file path: Double-check the path and file/directory names specified.

Integration

Combine with xargs: Pipe output to xargs for further processing, such as find-filter -e "size > 100000" /home | xargs rm -rf.

Use with find: Leverage find to locate files and then filter them using find-filter, such as find /home -name "*.txt" | find-filter -e "size > 100000".

Related Commands

  • find: Locate files in a filesystem.
  • grep: Search for text patterns within files.
  • xargs: Execute commands on multiple arguments.