带有文件管理递归提示的脚本?

带有文件管理递归提示的脚本?

抱歉,我是 BASH 脚本的新手,可能来错地方了。我刚买了一台新 Mac,正在尝试编写 BASH 脚本。我的第一次尝试将帮助我从下载目录中移动文件。

我希望有一个程序要求我输入搜索字符串以在我的下载文件夹中查找。然后我会将这些文件的结果移动到另一个目录。我希望提示循环 - 允许我优化搜索词,直到我只列出我想要的文件。

我已经想出了以下内容。但我遇到了一个明显的问题,那就是必须if无限次复制/粘贴我的语句。有没有更好的方法来做到这一点?也许把整个事情放在一个循环中?这是我目前所拥有的。

#!/bin/bash


# This script helps manage files that are found in the Downloads DIRECTORY
# and can relocate them to other directories.


# Set up an if-then statement/loop that allows you to search by extension or
# other wildcard until you find what you're looking for.

echo Searching Downloads directory

read -p 'Search ... [regex]? ' RESPONSE

find Downloads -maxdepth 1 -name "${RESPONSE}"

FOUND=$(find Downloads -maxdepth 1 -name "${RESPONSE}")


# Prompt to move these files



read -p 'Would you like to move these files [y = Yes] ' FILE_MOVE


if [ "$FILE_MOVE" = "y" ]
  then
    read -p 'Which directory? ' DIRECTORY_MOVE
    mv FOUND DIRECTORY_MOVE
  else
    read -p 'Would you like to keep searching [y = Yes] ' KEEP_SEARCHING
fi


if [ "$KEEP_SEARCHING" = "y" ]
  then
    read -p 'Search ... [regex]? ' RESPONSE
    find Downloads -maxdepth 1 -name "${RESPONSE}"
  else
    echo Goodbye!!
fi

相关内容