当前位置:首页 » 文件管理 » linuxc遍历文件夹

linuxc遍历文件夹

发布时间: 2025-05-05 04:07:42

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


这样应该可以吧,试试看

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:593
制作脚本网站 发布:2025-10-20 08:17:34 浏览:888
python中的init方法 发布:2025-10-20 08:17:33 浏览:581
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:765
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:684
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1012
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:255
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:113
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:806
python股票数据获取 发布:2025-10-20 07:39:44 浏览:712