當前位置:首頁 » 編程軟體 » 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

熱點內容
ftp上傳網頁 發布:2025-07-15 01:13:09 瀏覽:181
音樂文件夾圖標 發布:2025-07-15 01:03:41 瀏覽:494
安卓機怎麼反向充電 發布:2025-07-15 01:03:40 瀏覽:500
電腦使用華為雲伺服器 發布:2025-07-15 00:48:10 瀏覽:533
中考應該如何排解壓力 發布:2025-07-15 00:17:54 瀏覽:362
安卓第三方應用軟體是什麼 發布:2025-07-15 00:12:06 瀏覽:149
程序業務配置存儲 發布:2025-07-14 23:52:16 瀏覽:685
csdn編程挑戰 發布:2025-07-14 23:52:08 瀏覽:791
國外乘法演算法 發布:2025-07-14 23:51:14 瀏覽:11
phpexplodet 發布:2025-07-14 23:46:44 瀏覽:566