如何使用 bash 脚本删除文件夹中的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8631990/
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
How can I delete contents in a folder using a bash script?
提问by Sheehan Alam
I would like to clear out my /bin folder in my project directory. How can I do this?
我想清除我的项目目录中的 /bin 文件夹。我怎样才能做到这一点?
I tried rm -rf ~/bin
but no luck
我试过了,rm -rf ~/bin
但没有运气
回答by dimir
~ is a shorthand to a current user home directory. So unless it's also your project directory you are doing something wrong. Other than that, clearing a directory would be
~ 是当前用户主目录的简写。所以除非它也是你的项目目录,否则你做错了什么。除此之外,清除目录将是
rm -rf ~/bin/*
And if you also want to clear the hidden files
如果您还想清除隐藏文件
rm -rf ~/bin/* ~/bin/.[a-zA-Z]*
Make sure you are not doing
确保你没有做
rm -rf ~/bin/.*
rm -rf ~/bin/.*
especially as root as it will try to clear out your entire system.
特别是作为 root 用户,它会尝试清除整个系统。
UPD
UPD
Why? Since wildcard (*
) is interpreted by shell as zero or more characters of any kindthe .*
will also match .
(current directory) and ..
(parent directory), which will result in going all the way up and then down, trying to remove each file in your filesystem tree.
为什么?由于通配符 ( *
) 被 shell 解释为零个或多个任何类型的字符,.*
因此也将匹配.
(当前目录)和..
(父目录),这将导致一路向上然后向下,试图删除您的文件中的每个文件文件系统树。
回答by fge
You should say "... my bin folder", not "my /bin folder". /bin
is an absolute path, bin
is a relative path.
您应该说“...我的 bin 文件夹”,而不是“我的 /bin 文件夹”。 /bin
是绝对路径,bin
是相对路径。
rm -rf ~/bin
removes $HOME/bin
, so not what you want either.
rm -rf ~/bin
删除$HOME/bin
,所以也不是你想要的。
Now, it depends on where you are: if you are in your project directory when you type the command, just type rm -rf bin
.
现在,这取决于您所在的位置:如果您在键入命令时在项目目录中,只需键入rm -rf bin
.