linuxc遍历文件夹
㈠ linux C 中的文件夹遍历dirent d_type表明该文件的类型 跪求~~~
enum
{
DT_UNKNOWN = 0,
# define DT_UNKNOWN DT_UNKNOWN
DT_FIFO = 1,
# define DT_FIFO DT_FIFO
DT_CHR = 2,
# define DT_CHR DT_CHR
DT_DIR = 4,
# define DT_DIR DT_DIR
DT_BLK = 6,
# define DT_BLK DT_BLK
DT_REG = 8,
# define DT_REG DT_REG
DT_LNK = 10,
# define DT_LNK DT_LNK
DT_SOCK = 12,
# define DT_SOCK DT_SOCK
DT_WHT = 14
# define DT_WHT DT_WHT
};
这是d_type的枚举类型..........每个值表示一个类型..........4是目录,0是未知,1是管道,2是字符设备,8表示文件...............6是块设备..........其他的都如字面表示..........很容易区分.........
㈡ linux平台:使用lua语言遍历某一文件夹下所有文件
你可以参考如下实例代码:
functiongetFile(file_name)
localf=assert(io.open(file_name,'r'))
localstring=f:read("*all")
f:close()
returnstring
endfunctionwriteFile(file_name,string)
localf=assert(io.open(file_name,'w'))
f:write(string)
f:close()
end--从命令行获取参数,如果有参数则遍历指定目录,没有参数遍历当前目录ifarg[1]~=nilthen
cmd="ls"..arg[1]
else
cmd="ls"endprint("cmd",cmd)
--io.popen返回的是一个FILE,跟c里面的popen一样locals=io.popen(cmd)
localfileLists=s:read("*all")
print(fileLists)
whiletruedo--从文件列表里一行一行的获取文件名_,end_pos,line=string.find(fileLists,"([^ ]+.txt)",start_pos)
ifnotend_posthenbreakend--print("wld",line)localstr=getFile(line)
--把每一行的末尾1,替换为0,localnew=string.gsub(str,"1, ","0, ");
--替换后的字符串写入到文件。以前的内容会清空writeFile(line,new)
start_pos=end_pos+1end
㈢ linux shell 遍历文件夹 并将结果保存 到变量
#!/bin/bash
(($#<1))&&echo"paramiszero!"&&exit1
[!-d$1]&&echo"$1notpath"&&exit1
dir=$1
dir_p="$dirDirectory:"
cd$dir
dir=`pwd`
foriin`ls$dir`
do
if[-d$i];then
/tmp/sh/dir_file$i#我的脚本文件在/tmp/sh中,需要改一下这里
else
dir_p="$dir_pFile$i"
fi
done
cd..
echo$dir_p
实验结果:
[root@localhost sh]# ./dir_file /tmp/python/
python_2 Directory : File 1.log File 2.log
python_3 Directory : File 3.log
/tmp/python/ Directory : File p File t.py File y.py
这样应该可以吧,试试看