为 Linux 上的查找设置默认选项
Set default options for find on Linux
我的 Ubuntu 机器上经常安装其他文件系统,因此,当我执行 find
时,我必须确保包含 -mount
选项(与 -xdev
选项相同)以避免它也在那些文件系统上搜索(通常很慢)。但是,有时我会忘记;然后我想知道为什么 find
花了这么长时间!我想要的是一种让 find
一直使用 -mount
的方法。
似乎没有我可以使用的 environment variable,而且似乎没有 .findrc
文件之类的东西可以指定选项。而且我不能轻易使用 alias
,因为 -mount
需要在我要搜索的位置之后。我 可以 创建一个 bash
函数,该函数接受搜索参数,然后在所有位置之后但在第一个开关之前插入 -mount
,然后再将其传递给 find
命令;但在我开始努力之前,是否已经有一种方法可以确保 find
每次 运行 时都使用 -mount
?
如果它对其他人有用,这里是我想出的 bash
函数:
find () {
# Make a copy of the arguments so they can be altered
local args=("$@")
# Will start to look at the first (zeroth) element
local i=0
# While element i exists and doesn't start with a "-", increment i
while [[ $i -lt ${#args[@]} && ${args[$i]} != -* ]]; do
let ++i
done
# Insert -mount at position i, which is where the first switch currently is
# (or is the end of the argument list).
args=("${args[@]:0:$i}" '-mount' "${args[@]:$i}")
# Use env to locate the find command in the path, and pass the manipulated
# arguments to it.
env find "${args[@]}"
}
我的 Ubuntu 机器上经常安装其他文件系统,因此,当我执行 find
时,我必须确保包含 -mount
选项(与 -xdev
选项相同)以避免它也在那些文件系统上搜索(通常很慢)。但是,有时我会忘记;然后我想知道为什么 find
花了这么长时间!我想要的是一种让 find
一直使用 -mount
的方法。
似乎没有我可以使用的 environment variable,而且似乎没有 .findrc
文件之类的东西可以指定选项。而且我不能轻易使用 alias
,因为 -mount
需要在我要搜索的位置之后。我 可以 创建一个 bash
函数,该函数接受搜索参数,然后在所有位置之后但在第一个开关之前插入 -mount
,然后再将其传递给 find
命令;但在我开始努力之前,是否已经有一种方法可以确保 find
每次 运行 时都使用 -mount
?
如果它对其他人有用,这里是我想出的 bash
函数:
find () {
# Make a copy of the arguments so they can be altered
local args=("$@")
# Will start to look at the first (zeroth) element
local i=0
# While element i exists and doesn't start with a "-", increment i
while [[ $i -lt ${#args[@]} && ${args[$i]} != -* ]]; do
let ++i
done
# Insert -mount at position i, which is where the first switch currently is
# (or is the end of the argument list).
args=("${args[@]:0:$i}" '-mount' "${args[@]:$i}")
# Use env to locate the find command in the path, and pass the manipulated
# arguments to it.
env find "${args[@]}"
}