㈠ linux平台:使用lua語言遍歷某一文件夾下所有文件
你可以參考如下實例代碼:
functiongetFile(file_name)
localf=assert(io.open(file_name,'r'))
localstring=f:read("*all")
f:close()
returnstring
endfunctionwriteFile(file_name,string)
localf=assert(io.open(file_name,'w'))
f:write(string)
f:close()
end--從命令行獲取參數,如果有參數則遍歷指定目錄,沒有參數遍歷當前目錄ifarg[1]~=nilthen
cmd="ls"..arg[1]
else
cmd="ls"endprint("cmd",cmd)
--io.popen返回的是一個FILE,跟c裡面的popen一樣locals=io.popen(cmd)
localfileLists=s:read("*all")
print(fileLists)
whiletruedo--從文件列表裡一行一行的獲取文件名_,end_pos,line=string.find(fileLists,"([^
]+.txt)",start_pos)
ifnotend_posthenbreakend--print("wld",line)localstr=getFile(line)
--把每一行的末尾1,替換為0,localnew=string.gsub(str,"1,
","0,
");
--替換後的字元串寫入到文件。以前的內容會清空writeFile(line,new)
start_pos=end_pos+1end
㈡ 如何在linux中使用shell腳本遍歷指定目錄的文件,將創建時間大於指定時間的文件,復制到指定目錄下。
大於指定時間?最簡單的就是直接find裡面指定吧。例如,查找當前目錄及其子目錄所有mtime大於1天的文件:
find /path -type f -mtime +1 即可,/path 可以換成其他路徑,-mtime +1 表示時間大於1天。-1的話表示小於一天也就是1天之內的。
㈢ 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 C 中的文件夾遍歷dirent d_type表明該文件的類型 跪求~~~
enum
{
DT_UNKNOWN = 0,
# define DT_UNKNOWN DT_UNKNOWN
DT_FIFO = 1,
# define DT_FIFO DT_FIFO
DT_CHR = 2,
# define DT_CHR DT_CHR
DT_DIR = 4,
# define DT_DIR DT_DIR
DT_BLK = 6,
# define DT_BLK DT_BLK
DT_REG = 8,
# define DT_REG DT_REG
DT_LNK = 10,
# define DT_LNK DT_LNK
DT_SOCK = 12,
# define DT_SOCK DT_SOCK
DT_WHT = 14
# define DT_WHT DT_WHT
};
這是d_type的枚舉類型..........每個值表示一個類型..........4是目錄,0是未知,1是管道,2是字元設備,8表示文件...............6是塊設備..........其他的都如字面表示..........很容易區分.........
㈤ linux下遍歷讀取所有子目錄里的特定文件,並改名復制到別的目錄
find . -name a.txt -exec mv {} b.txt \; 其中find後面的"."表示從當前目錄開始查找(含子目錄),注意最後的「\;"是需要的。
㈥ 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下C語言:如何遍歷制定目錄及其子目錄下所有文件的文件名並將其按照最後修改時間排序呢
linux中有相關的API函數,可以讀取目錄中所有的文件名字,以及時間屬性信息,你把這些信息讀出來,利用各種排序演算法排序就可以了
㈨ 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!