修改shell脚本以删除文件夹和文件
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-type f
ls -l
更改为ls -ld
更改 1 将列出所有内容,而不仅仅是文件。这也包括链接。如果您不喜欢列出/删除文件和目录以外的任何内容,那么您需要单独列出/删除文件和目录,如下所示:
需要更改 2,因为目录上的 ls -l 会列出文件在目录中。
-type f
ls -l
tols -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:
Change 2 is needed as
ls -l
on a directory will list the files in the directories.那应该对你有用。
That should work for you.