使用 fd 命令快速搜索文件

Windows 里搜文件有 Everything,在 Mac 下搜文件,没找到类似的软件。用 Mac Everything 一段时间,发现占用内存会不断增加,开始1G,后台运行2天后变成2G。
Filelocator,Profind 这些对中文支持不太好。 TextSeek 要联网就很奇怪,只想本地搜索。
找到一个平替 gist: use fd command to find files · GitHub

#!/bin/bash

# Default path is the current directory if not specified
default_path="."

# Initialize variables
extension=""
path=""
type=""
size_range=""
exclude=""
name=""

# Function to parse size and generate fd-compatible size flags
parse_size_range() {
    local range=$1
    IFS=',' read -ra sizes <<< "$range"
    local size_flags=()
    # Check if two sizes are provided (range)
    if [[ "${#sizes[@]}" -eq 2 ]]; then
        size_flags+=("--size" "+${sizes[0]}")
        size_flags+=("--size" "-${sizes[1]}")
    elif [[ "${#sizes[@]}" -eq 1 ]]; then
        size_flags+=("--size" "+${sizes[0]}")
    fi
    echo "${size_flags[@]}"
}

# Check if no arguments
if [ $# -eq 0 ]; then
    echo "Usage: ff -n <name pattern> -e <extension> -p <path> -t <file type> -s <size range> -E <exclude pattern>"
    echo "Example: ff -n 'user contro' -e js -p ~ -t f -s 10M,100M -E node_modules"
    exit 0
fi

# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
  case $1 in
    -e) extension="$2"; shift ;;
    -p) path="$2"; shift ;;
    -t) type="$2"; shift ;;
    -s) size_range="$2"; shift ;;
    -E) exclude="$2"; shift ;;
    -n) # Concatenate all subsequent arguments until another flag is found
        while [[ "$2" != -* ]] && [[ "$#" -gt 1 ]]; do
            name+=" $2"
            shift
        done
        name="${name:1}"  # Remove leading space
        ;;
    *) if [[ -z "$name" ]]; then name="$1"; else name+=" $1"; fi ;;
  esac
  shift
done

# Convert name to regex if it contains spaces indicating multiple terms
if [[ "$name" =~ \  ]]; then
    # Create regex that matches all words in any order
    regex=".*"
    for word in $name; do
        regex+="$word.*"
    done
    name=$regex
else
    name=".*$name.*"
fi

# Build the fd command array
cmd=("fd" "--regex")

# Add the name or pattern
if [[ -n "$name" ]]; then
  cmd+=("$name")
fi

# Add file extension
if [[ -n "$extension" ]]; then
  cmd+=("--extension" "$extension")
fi

# Set file type
if [[ -n "$type" ]]; then
  cmd+=("--type" "$type")
fi

# Set file size constraints
if [[ -n "$size_range" ]] && [[ "$type" == "f" ]]; then
    size_flags=($(parse_size_range "$size_range"))
    for size_flag in "${size_flags[@]}"; do
        cmd+=("${size_flag}")
    done
fi

# Exclude directories or file patterns
if [[ -n "$exclude" ]]; then
  cmd+=("--exclude" "$exclude")
fi

# Add the path
if [[ -n "$path" ]]; then
  cmd+=("--" "$path")
else
  cmd+=("--" "$default_path")
fi

# Echo the command for debugging
echo "Executing command: ${cmd[*]}"

# Execute the fd command
"${cmd[@]}"

使用开源 fd,搜索起来也挺快。

有个类似的 Alfred Workflow,搜文件够用了

6 个赞

macOS 自带搜索啊

始皇驾到!!接驾!

Spotlight吗?体验起来感觉没有 Alfred 好使;fd 补充一些 rg 和 Alfred 照顾不到的场景

raycast?

ntfs先进,所以有everything能取巧。你这fd没有元数据db只能暴力搜索的样子。

虽然不知道你在讲什么,但是好像好厉害的样子