Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
Loops 在UNIX中循环列表文件_Loops_Unix - Fatal编程技术网

Loops 在UNIX中循环列表文件

Loops 在UNIX中循环列表文件,loops,unix,Loops,Unix,我发出命令 ls -lrt 列出的文件和目录如下所示: drwxr-xr-x 4 root root 4096 Feb 2 2014 abc drwxr-xr-x 4 root root 4096 Feb 2 2014 cde drwxr-xr-x 4 root root 4096 Feb 2 20014 efg -rwxr-xr-x 4 root root 4096 Feb 2 20014 aaa.txt 现在,我想通过使用循环来确定

我发出命令

ls -lrt
列出的文件和目录如下所示:

drwxr-xr-x   4 root root    4096 Feb  2  2014 abc
drwxr-xr-x   4 root root    4096 Feb  2  2014 cde
drwxr-xr-x   4 root root    4096 Feb  2  20014 efg
-rwxr-xr-x   4 root root    4096 Feb  2  20014 aaa.txt
现在,我想通过使用循环来确定列出的输出文件是否为目录


提前谢谢。

这听起来像是一个X/Y问题,你要求X,但实际上想要Y

您正在尝试获取所有目录的列表吗?使用
find
命令

find . -type d

你是说像这样的事吗?在bash中:

ls |同时读取f;do test-d“$f”&&echo$f是一个目录| | echo$f不是一个目录;完成

这将列出当前目录中的所有文件,并检查每个文件是否为目录。要测试子目录中的目录,请将初始的“ls”替换为“find”

另一个选项是检查权限中的第一个选项。“d”目录。“-”文件

编辑:

for l in `ls -l|grep -v total |awk '{print $1}'|cut -b1`
do
if [ $l == "-" ]
then
    echo "File"
else
if [ $l == "d" ]
then
    echo "Directory"
fi
fi
done

使用循环是什么意思?我想在for循环或while循环中运行所有文件以测试文件是否为目录。我也可以使用
ls-ld
列出目录。但是我想在一个循环中检查(for或while)。我想在一个循环中运行所有文件以检查它是否是目录。如果任何文件名包含空格或奇怪的字符,这将不起作用。ls(1)将转换为问号,例如U+007F(DELETE)或非UTF-8字符。第二个脚本是什么?
for l in `ls -l|grep -v total |awk '{print $1}'|cut -b1`
do
if [ $l == "-" ]
then
    echo "File"
else
if [ $l == "d" ]
then
    echo "Directory"
fi
fi
done