当前位置:首页 » 操作系统 » linuxshell遍历文件

linuxshell遍历文件

发布时间: 2022-05-05 07:47:03

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

⑵ 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如何编个shell去指定按照某个文件里列出的文件名来循环删除某个文件夹下的这些文件

可以的,我给你写出来吧,用shell ,+741345015

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

我想这个好像没有专门的命令的
一般都是通过编程来实现的,可以通过递归方式来遍历目录,然后得到文件数。

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

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

⑹ linux shell如何获取最新修改的文件或者文件夹

mmin过去几分钟前修改、创建的文件,(-n)为几分钟内

⑺ linux shell遍历当前文件夹中的txt文件并处理生成新的文件

先以a.txt为例:

awk-vRS=""'{
n=split($0,a,"《[^》]+》");
for(i=2;i<n;i+=2)
print"《keywords》"a[i]"《/keywords》"
}'a.txt>>./newfile/a.txt

这样就行了。

为了可读性,我将一条awk语句写成了多行。

实际测试结果如下:

解说:

RS=""

将awk的记录分隔符设置为空(默认是换行符),即将整个a.txt文本看做一条记录。

n = split($0,a,"《[^》]+》");

以正则"《[^》]+》"匹配的内容作为分隔符,对文本内容进行分割并将分割结果存入数组a,分割出的数目(数组大小)即为split函数的返回值n。这里暂且不对该正则做过多解释,否则喧宾夺主,有需要请追问,我再补充。

for(i=2;i<n;i+=2)
print "《keywords》"a[i]"《/keywords》"

打印数组下标为偶数的元素并在首尾分别加上关键字标记以还原。数组下标从1开始。

其他文件可作相同处理。如果文件较多,你可以搞个循环去做。这个应该不难。

⑻ 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!

⑼ bash脚本遍历目录指定后缀的文件,并执行操作

可以使用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

⑽ 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


这样应该可以吧,试试看

热点内容
文件夹两孔 发布:2024-10-06 23:48:53 浏览:351
ftp工具分析 发布:2024-10-06 23:48:51 浏览:157
服务器被游戏封机器码怎么办 发布:2024-10-06 23:46:10 浏览:160
股票java 发布:2024-10-06 23:38:46 浏览:290
安卓算法 发布:2024-10-06 23:14:20 浏览:183
域名访问404 发布:2024-10-06 23:08:52 浏览:708
访问笔录模版 发布:2024-10-06 22:36:54 浏览:140
多用途编程 发布:2024-10-06 22:35:58 浏览:617
msde2000数据库下载 发布:2024-10-06 22:33:35 浏览:44
什么配置可以算神机 发布:2024-10-06 21:52:09 浏览:422