我已经开始使用 Linux 并且我正在练习使用/制作代码来定位和做事情。我需要从输入字符串中定位任何文件的代码。
两种选择
find
. 例如find ~/Documents -name '*finances*'
locate
(需要带有 的最新索引updatedb
)。例如locate finances
把它放在一个脚本中,你可以这样做
#!/bin/bash
# pattern="${1}" # first argument to script
# alternatively, ask user
echo "Enter a pattern to be searched for in the current directory"
read pattern
# search current directory `.`
matches=$(find . -type f -name "${pattern}")
# $matches is now a list of matching files
echo "$matches"
Run Code Online (Sandbox Code Playgroud)
小心shell gobbing,即*
模式中的 a 首先由 bash 扩展以匹配当前目录中的文件名。
记录了多种选项find
:man find
.
欢迎来到 Linux!