当前位置:首页 » 编程软件 » shell脚本遍历文件

shell脚本遍历文件

发布时间: 2022-12-20 10:04:29

‘壹’ shell怎么遍历一个文件夹下所有文件

# find . -type f
./a
./normal/log-1
./normal/log-2
./normal/log-3

通过find找到文件
那么遍历就用循环
for i in `find . -type f`
do
echo $i

done

‘贰’ 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!

‘叁’ 如何用shell脚本遍历指定目录下的文件,并按后缀名分类

脚本1:
#!/bin/bash
#
#
cd /tmp/script
cfile=` find -name '*.c' `
hfile=` find -name '*.h' `
for fc in $cfile
do
cfname=`basename -s .c $fc`
cat $fc >/tmp/scripttest/$cfname.txt
done
for fh in $hfile
do
cp $fh /tmp/scripttest
done

‘肆’ 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


这样应该可以吧,试试看

‘伍’ 如何在linux中使用shell脚本遍历指定目录的文件,将创建时间大于指定时间的文件,复制到指定目录下。

大于指定时间?最简单的就是直接find里面指定吧。例如,查找当前目录及其子目录所有mtime大于1天的文件:
find
/path
-type
f
-mtime
+1
即可,/path
可以换成其他路径,-mtime
+1
表示时间大于1天。-1的话表示小于一天也就是1天之内的。

‘陆’ linux的shell脚本中,如何通过until循环实现 当本目录下存在restart文件夹的时候进行循环呢

可以使用ls或者find来完成对某个文件夹下所有文件的遍历
比如使用ls
可以简单地使用一个通配符来完成
ls 某个目录/*

也可以使用find来完成
比如
find 某个目录

自然的也可以写一个shell脚本来进行遍历
首先进行一个要遍历的文件夹
然后循环查看每个文件
如果该文件是一个文件夹的话则进入该文件夹做和上面相同的事件
这样就可以该整个文件夹内的所有文件进行遍历了
一个简单的代码如下
#!/bin/bash

function show()
{
cd $1

for i in `ls`
do
if [ -d "$i" ]
then
show "$i"
else
echo "$i"
fi
done

cd ..
}

show $1

exit 0

该程序不能遍历以.开头的隐藏文件
可以使用ls -a来进行遍历隐藏文件
遍历时需要注意.和..这两个特殊文件

下面是一个简单的代码
#!/bin/bash

function show()
{
cd $1

for i in `ls -a`
do
if [ "$i" == "." ] || [ "$i" == ".." ]
then
continue;
fi

if [ -d "$i" ]
then
show "$i"
else
echo "$i"
fi
done

cd ..
}

show $1

exit 0

热点内容
电脑配置低怎么变得不卡 发布:2025-07-15 05:34:08 浏览:844
ios火影忍者手游脚本 发布:2025-07-15 05:31:34 浏览:82
iphone支付密码忘了怎么办 发布:2025-07-15 05:30:55 浏览:775
c语言打开网页 发布:2025-07-15 05:21:33 浏览:640
如何制作我的世界模组服务器 发布:2025-07-15 05:21:33 浏览:903
phparray加 发布:2025-07-15 05:20:41 浏览:782
4000以内二手安卓机怎么选 发布:2025-07-15 05:11:25 浏览:644
静态编译修复器 发布:2025-07-15 05:11:24 浏览:506
iphonexr的存储空间 发布:2025-07-15 05:09:20 浏览:328
能缓存航海王 发布:2025-07-15 04:55:38 浏览:91