如何使用 bash 脚本删除文件夹中的内容?
我想清除项目目录中的 /bin 文件夹。我该怎么做?
我尝试了 rm -rf ~/bin
但没有成功
I would like to clear out my /bin folder in my project directory. How can I do this?
I tried rm -rf ~/bin
but no luck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
~ 是当前用户主目录的简写。因此,除非它也是您的项目目录,否则您就做错了。除此之外,清除目录将是
,如果您还想清除隐藏文件
请确保您没有这样做,
特别是作为root,因为它也会尝试删除父目录。
UPD
为什么?由于通配符 (
*
) 被 shell 解释为零个或多个任何类型的字符,因此.*
也将匹配. (当前目录)和
..
(父目录)。~ 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
And if you also want to clear the hidden files
Make sure you are not doing
especially as root as it will also try to delete the parent directory.
UPD
Why? Since wildcard (
*
) is interpreted by shell as zero or more characters of any kind the.*
will also match.
(current directory) and..
(parent directory).您应该说“...我的 bin 文件夹”,而不是“我的 /bin 文件夹”。
/bin
是绝对路径,bin
是相对路径。rm -rf ~/bin
删除了$HOME/bin
,所以也不是你想要的。现在,这取决于您所在的位置:如果您在键入命令时位于项目目录中,则只需键入
rm -rf bin
。You should say "... my bin folder", not "my /bin folder".
/bin
is an absolute path,bin
is a relative path.rm -rf ~/bin
removes$HOME/bin
, so not what you want either.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/{*,.[^.]*}
将删除
~/bin/
中的所有文件和目录,包括隐藏的文件和目录(名称以.
),但不是父目录(即..
)。.[^.]*
匹配名称以点开头、第二个字符不是点且包含或不包含更多字符的所有隐藏文件和目录。rm -rf ~/bin/{*,.[^.]*}
would delete all files and directories in
~/bin/
, including hidden ones (name starts with.
), but not the parent directory (i.e...
).The
.[^.]*
matches all hidden files and directories whose name starts with a dot, the second char is NOT a dot, and with or without more chars.