网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

linux遍历文件的命令,遍历文件夹执行文件

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

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

专业领域包括网站建设、网站制作商城网站制作、微信营销、系统平台开发, 与其他网站设计及系统开发公司不同,创新互联建站的整合解决方案结合了帮做网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,为客户提供全网互联网整合方案。

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

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

如果是文件则答应文件名

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

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

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

linux必学的命令是什么?

1、作用at命令用来在指定时刻执行指定的命令序列。

2、格式at[-V][-qx][-ffile][-m]time。

3、主要参数

-V:显示标准错误输出。

-q:许多队列输出。

-f:从文件中读取作业。

-m:执行完作业后发送电子邮件到用户。

time:设定作业执行的时间。time格式有严格的要求,由小时、分钟、日期和时间的偏移量组成,其中日期的格式为MM。DD。YY,MM是分钟,DD是日期,YY是指年份。偏移量的格式为时间+偏移量,单位是minutes、hours和days。

扩展资料:

学习linux注意事项

1、Linux严格区分大小写。

2、Linux所有的存储设备都必须挂载之后用户才能使用,包括硬盘、U盘和光盘。

3、Windows下的程序不能直接在Linux中安装和运行。

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平台:使用lua语言遍历某一文件夹下所有文件

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

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 遍历文件夹 并将结果保存 到变量

#!/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遍历文件的命令,遍历文件夹执行文件
转载来于:http://bjjierui.cn/article/dssojid.html

其他资讯