修改shell脚本以删除文件夹和文件

发布于 2024-09-17 17:50:15 字数 575 浏览 8 评论 0原文

我的 shell 脚本:

#!/bin/bash
if [ $# -lt 2 ]
then
    echo "$0 : Not enough argument supplied. 2 Arguments needed."
    echo "Argument 1: -d for debug (lists files it will remove) or -e for execution."
    echo "Followed by some path to remove files from. (path of where to look) "
    exit 1
fi

if test $1 == '-d'
then
    find $2 -mmin +60 -type f -exec ls -l {} \;
elif test $1 == '-e'
then
    find $2 -mmin +60 -type f -exec rm -rf {} \;
fi

基本上,这将在作为第二个参数提供的给定目录中查找文件,并列出(-d 表示参数 1)或删除(-e 表示参数 1)60 分钟前修改的文件。

我怎样才能修改它以删除文件夹?

My shell script:

#!/bin/bash
if [ $# -lt 2 ]
then
    echo "$0 : Not enough argument supplied. 2 Arguments needed."
    echo "Argument 1: -d for debug (lists files it will remove) or -e for execution."
    echo "Followed by some path to remove files from. (path of where to look) "
    exit 1
fi

if test $1 == '-d'
then
    find $2 -mmin +60 -type f -exec ls -l {} \;
elif test $1 == '-e'
then
    find $2 -mmin +60 -type f -exec rm -rf {} \;
fi

Basically this will find files in a given directory provided as second argument and either list (-d for argument 1) or remove (-e for argument 1) files modified >60 minutes ago.

How can I rework this to also remove folders ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有木有妳兜一样 2024-09-24 17:50:15
  • 删除 -type f
  • ls -l 更改为 ls -ld

更改 1 将列出所有内容,而不仅仅是文件。这也包括链接。如果您不喜欢列出/删除文件和目录以外的任何内容,那么您需要单独列出/删除文件和目录,如下所示:

if test $1 == '-d'
then
    find $2 -mmin +60 -type f -exec ls -ld {} \;
    find $2 -mmin +60 -type d -exec ls -ld {} \;
elif test $1 == '-e'
then
    find $2 -mmin +60 -type f -exec rm -rf {} \;
    find $2 -mmin +60 -type d -exec rm -rf {} \;
fi

需要更改 2,因为目录上的 ls -l 会列出文件在目录中。

  • Remove -type f
  • changing ls -l to ls -ld

Change 1 will list everything and not just files. This includes links as well. If you are not fine with listing/deleting anything other than files and directories then you need to separately list/delete files and directories as:

if test $1 == '-d'
then
    find $2 -mmin +60 -type f -exec ls -ld {} \;
    find $2 -mmin +60 -type d -exec ls -ld {} \;
elif test $1 == '-e'
then
    find $2 -mmin +60 -type f -exec rm -rf {} \;
    find $2 -mmin +60 -type d -exec rm -rf {} \;
fi

Change 2 is needed as ls -l on a directory will list the files in the directories.

撧情箌佬 2024-09-24 17:50:15
#!/bin/bash
if [ $# -lt 2 ]
then
    echo "$0 : Not enough argument supplied. 2 Arguments needed."
    echo "Argument 1: -d for debug (lists files it will remove) or -e for execution."
    echo "Followed by some path to remove files from. (path of where to look) "
    exit 1
fi

if test $1 == '-d'
then
    find $2 -mmin +60 -type d -exec ls -l {} \;
    find $2 -mmin +60 -type f -exec ls -l {} \;
elif test $1 == '-e'
then
    find $2 -mmin +60 -type d -exec rm -rf {} \;
    find $2 -mmin +60 -type f -exec rm -rf {} \;
fi

那应该对你有用。

#!/bin/bash
if [ $# -lt 2 ]
then
    echo "$0 : Not enough argument supplied. 2 Arguments needed."
    echo "Argument 1: -d for debug (lists files it will remove) or -e for execution."
    echo "Followed by some path to remove files from. (path of where to look) "
    exit 1
fi

if test $1 == '-d'
then
    find $2 -mmin +60 -type d -exec ls -l {} \;
    find $2 -mmin +60 -type f -exec ls -l {} \;
elif test $1 == '-e'
then
    find $2 -mmin +60 -type d -exec rm -rf {} \;
    find $2 -mmin +60 -type f -exec rm -rf {} \;
fi

That should work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文