如何实现搜索功能以查找文件或目录

如何实现搜索功能以查找文件或目录

本文介绍了如何实现搜索功能以查找文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为bash编写脚本,该脚本将要求用户提供起始位置和文件名,然后搜索该文件.如果可能的话,我也想找到该文件.这是我到目前为止拥有的脚本,如果有人指出我将如何包含该函数的话,我将非常感激.还是Unix初学者,所以请忍受我:)

I am currently working on a script for bash that will ask the user for a starting location and a file name then search for the file. If possible I would also like to cat the file once found. Here is the script I have so far if someone would point out how I would include the function I would be very thankful. Also still a Unix beginner so please bear with me :)

#!/bin/bash
function press_enter
{
    echo""
    echo -n "Press Enter to continue"
    read
    clear
}
selction=
until [ "$selection" = "3" ]; do
echo -e "Where would you like to search
1- Root
2- Home
3- Exit

Enter your choice ---> \c"
read selection
case $selection in
    1) cd / ; press_enter ;;
    2) cd /home ; press_enter ;;
    3) echo "Have a nice day!" ; exit ;;
esac
done

推荐答案

您可以使用查找作为一个命令,或者使用选择的输入来驱动脚本中的find.

You can use find as one command, or use your selection inputs to drive find in your script.

find $where_selection -name $what_selection
# with cat:
find $where_selection -name $what_selection -exec cat {} \;

-exec cat {}{}替换为当前从find找到的文件路径.

-exec cat {} replaces the {} with the current found file path from find.

这篇关于如何实现搜索功能以查找文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:57