资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

linux命令遍历文件夹,遍历所有文件夹

linux平台:使用lua语言遍历某一文件夹下所有文件

你可以参考如下实例代码:function getFile(file_name) 

石台ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

local f = assert(io.open(file_name, 'r'))

local string = f:read("*all")

f:close()

return string

end function writeFile(file_name,string)

local f = assert(io.open(file_name, 'w'))

f:write(string)

f:close()

end --从命令行获取参数, 如果有参数则遍历指定目录,没有参数遍历当前目录 if arg[1] ~= nil then

cmd = "ls "..arg[1]

else

cmd = "ls" end print("cmd", cmd)

--io.popen 返回的是一个FILE,跟c里面的popen一样 local s = io.popen(cmd)

local fileLists = s:read("*all")

print(fileLists)

while true do --从文件列表里一行一行的获取文件名 _,end_pos, line = string.find(fileLists, "([^\n\r]+.txt)", start_pos)

if not end_pos then break end --    print ("wld", line) local str = getFile(line)

--把每一行的末尾 1, 替换为 0, local new =string.gsub(str, "1,\n", "0,\n");

--替换后的字符串写入到文件。以前的内容会清空     writeFile(line, new)

start_pos = end_pos + 1 end

linux shell 命令怎么遍历目录

先设定实验环境:

# 造 5 个 目录,每个目录下,造 3 个 文件和两个子目录如下:

cd $HOME/tmp

for i in d1 d2 d3 d4 d5

do

mkdir -p $i

touch $i/1.txt $i/2.txt $i/3.txt

mkdir -p $i/tmp1 $i/tmp2

done

# 检验测试环境:

$ ls -lR d1

total 0

-rw-r--r-- 1 wenlee comm 0 Dec 22 10:35 1.txt

-rw-r--r-- 1 wenlee comm 0 Dec 22 10:35 2.txt

-rw-r--r-- 1 wenlee comm 0 Dec 22 10:35 3.txt

drwxr-sr-x 2 wenlee comm 256 Dec 22 10:35 tmp1/

drwxr-sr-x 2 wenlee comm 256 Dec 22 10:35 tmp2/

# 利用下列脚本来实现你要做的:

cd $HOME/tmp

for i in */1.txt

do

echo "Found $i, save $i and remove everything else under $(dirname $i)/"

save_this_file=$(basename $i)

curr_dir=$(dirname $i)

# 把这个1.txt暂时存到/tmp里面去,为了避免已经有同样的档案名称在/tmp,加上$$ (i.e. PID)

mv $i /tmp/${save_this_file}.$$

rm -rf $curr_dir

mkdir -p $curr_dir

mv /tmp/${save_this_file}.$$ $curr_dir

done

# 屏幕执行输出如下:

Found d1/1.txt, save d1/1.txt and remove everything else under d1/

Found d2/1.txt, save d2/1.txt and remove everything else under d2/

Found d3/1.txt, save d3/1.txt and remove everything else under d3/

Found d4/1.txt, save d4/1.txt and remove everything else under d4/

Found d5/1.txt, save d5/1.txt and remove everything else under d5/

# 复验实验环境:

$ ls -l d?/*

-rw-r--r-- 1 wenlee comm 0 Dec 22 10:35 d1/1.txt

-rw-r--r-- 1 wenlee comm 0 Dec 22 10:35 d2/1.txt

-rw-r--r-- 1 wenlee comm 0 Dec 22 10:35 d3/1.txt

-rw-r--r-- 1 wenlee comm 0 Dec 22 10:35 d4/1.txt

-rw-r--r-- 1 wenlee comm 0 Dec 22 10:35 d5/1.txt

OK?

thanks!

在网上找到的linux下用C遍历目录的源代码 具体看补充。。

1、系统里面没有/usr/keygoe/ini文件夹或没有权限。可以修改main, 遍历命令行传入的文件夹;

2、filename设置为[256][256],如果文件比较多,程序会崩溃的;

3、遍历子文件夹时,只保存了文件名,没有添加上级路径。

linux下遍历读取所有子目录里的特定文件,并改名复制到别的目录

find . -name a.txt -exec mv {} b.txt \; 其中find后面的"."表示从当前目录开始查找(含子目录),注意最后的“\;"是需要的。

在linux shell(bash)编程中,如何通过递归方式遍历文件

写一个函数,函数的参数是目录路径字符串

函数内使用 ls -s dir_path , 然后for 遍历循环

如果是目录则继续调用自身

如果是文件则答应文件名

从执行优化的角度来讲,可以把判断目录还是文件的代码放在循环外层.

好久没写shell了 ,我这也没环境测试 , 只能给个思路,函数的具体写法自己找一下资料吧.

另外,find命令可以直接完成你要做的事.

linux shell 遍历文件夹 并将结果保存 到变量

#!/bin/bash

(( $#  1 ))  echo "param is zero!"  exit 1

[ ! -d $1 ]  echo "$1 not path"  exit 1

dir=$1

dir_p="$dir Directory :"

cd $dir

dir=`pwd`

for i in `ls $dir`

do

if [ -d $i ]; then

/tmp/sh/dir_file $i            #我的脚本文件在/tmp/sh中,需要改一下这里

else

dir_p="$dir_p File $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

这样应该可以吧,试试看


本文名称:linux命令遍历文件夹,遍历所有文件夹
当前URL:http://cdkjz.cn/article/hsjcsg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220