简体   繁体   English

Linux bash:查找包含特定文本的文件夹

[英]Linux bash: find folders containing specific text

I learned how to use a script yesterday that would display all files containing the entered text. 我昨天学习了如何使用脚本,该脚本将显示包含输入文本的所有文件。

echo "enter serial number"
read number

matchfiles=(`find /home/data_files -maxdepth 1 -name "*$number*" -type f -exec basename {} \;`)

COUNTER=${#matchfiles[@]}

echo serial number data folder found:
until [  $COUNTER -lt 0 ]; do
echo ${matchfiles[$COUNTER]}
let COUNTER-=1
done

If the number 12 was entered it would output all files with the number 12 contained in the filename' 如果输入数字12,它将输出文件名中包含数字12的所有文件。

For ex: 例如:

enter serial number
12
file12.fee
12file.foo
file.12

Im looking for a way to do the exact same thing but with directories 我正在寻找一种方法来做完全相同的事情,但使用目录

so the output would be 所以输出将是

enter serial number
12
folder12
12folder

I need the folder names contained in an array so they may be used later for manipulating the data within them. 我需要包含在数组中的文件夹名称,以便以后可用于在其中处理数据。

find {path} -name "*$number*" -type d -exec readlink -f {} \;

if your optional {path} is relative, you want readlink -f is to convert relative paths to absolute path. 如果您的可选{path}是相对的,则您希望readlink -f将相对路径转换为绝对路径。 You may want to add back -maxdepth if you want to restrict recursive search in the dir path. 如果要限制目录路径中的递归搜索,则可能需要添加回-maxdepth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM