Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux for循环中的星号未按预期工作?_Linux_Bash_Shell_Ubuntu_Sh - Fatal编程技术网

Linux for循环中的星号未按预期工作?

Linux for循环中的星号未按预期工作?,linux,bash,shell,ubuntu,sh,Linux,Bash,Shell,Ubuntu,Sh,我不明白这为什么不起作用。我正在尝试循环浏览文件和文件夹,并根据名称删除其中一些文件和文件夹。在下面的示例中,删除除if语句中的文件夹以外的所有文件夹 代码如下: #!/bin/bash workdir=/var/www/ for dir in $workdir/custom/*; do if ! [ "$dir" == "$workdir/custom/somefolder" ]; then rm -rf $dir echo "remove $dir $?" fi echo

我不明白这为什么不起作用。我正在尝试循环浏览文件和文件夹,并根据名称删除其中一些文件和文件夹。在下面的示例中,删除除
if
语句中的文件夹以外的所有文件夹

代码如下:

#!/bin/bash

workdir=/var/www/
for dir in $workdir/custom/*; do
 if ! [ "$dir" == "$workdir/custom/somefolder" ]; then
   rm -rf $dir
   echo "remove $dir $?"
 fi
 echo "$dir"
done
问题是在
/custom/
中有几个文件和文件夹,但是
只输出一次
/var/www/custom/*
而不是遍历该目录中的每个文件和文件夹。我知道这意味着*什么都不匹配,但这是不可能的

文件夹存在,其中有多个文件和文件夹,路径正确,而且用户拥有rm文件的所有所需权限,我检查了两次。

我错过了什么

find /var/www/custom/ -name "*" -type d -mindepth 1 -exec rm -rf {} \;

这将删除自定义目录中的所有目录。如果这是必需的。

如果
$dir
/var/www/custom/*
,则表示通配符不匹配。你确定你的路径名是正确的吗?也许“全局搜索”已关闭?试试
set+f
为什么要使用循环而不是
rm-rf“$workdir/custom/”{somefolder,otherfolder,…}
?ls/var/www/custom/*
显示了什么?是要删除文件和目录,还是只删除目录中的文件。您可能需要使用find命令。find/var/www/custom/-name“*”-type d-exec-rm-rf{}\;遗憾的是,我正在尝试删除所有文件和文件夹,除了我的
if
语句中所述的文件和文件夹。请立即尝试使用它。。我将mindepth添加为1。你的答案的问题是,它实际上与我试图实现的目标相反。我添加了一个
到我的
如果在我的代码描述中,我忘了添加它。哦,找到了。您可以尝试在查找中使用-path/path/to/dirTouDontwantToDelete-prune-o。