bash 使用shell脚本删除文件夹的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10483678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:12:49  来源:igfitidea点击:

Deleting content of folder with shell script

linuxmacosbashshellcommand

提问by Antonio MG

Im having problems trying to empty a folder in my script.

我在尝试清空脚本中的文件夹时遇到问题。

This is working in my command line:

这在我的命令行中有效:

rm -r Folder1/Folder2/*

But if in my script I do this:

但是如果在我的脚本中我这样做:

DIR="Folder1/Folder2/"
rm -r "$DIR*"

It says "rm: Folder1/Folder2/*: No such file or directory", where is the problem?

它说“rm:Folder1/Folder2/*:没有那个文件或目录”,问题出在哪里?

Im running the script in the same folder as I tried the command.

我在尝试命令时在同一文件夹中运行脚本。

回答by Mat

Glob expansion doesn't happen inside quotes.

全局扩展不会发生在引号内。

Try:

尝试:

rm -r -- "$DIR"*

(Just make really sure you don't put a space after the quotes.)

(只要确保不要在引号后放置空格。)

回答by egoroveo

rm -r $DIR*

That should work, without quotes

这应该有效,没有引号