當前位置:首頁 » 操作系統 » linux遍歷目錄

linux遍歷目錄

發布時間: 2023-02-01 06:07:40

① 如何在linux中使用shell腳本遍歷指定目錄的文件,將創建時間大於指定時間的文件,復制到指定目錄下。

大於指定時間?最簡單的就是直接find裡面指定吧。例如,查找當前目錄及其子目錄所有mtime大於1天的文件:
find /path -type f -mtime +1 即可,/path 可以換成其他路徑,-mtime +1 表示時間大於1天。-1的話表示小於一天也就是1天之內的。

② Linux下C語言:如何遍歷制定目錄及其子目錄下所有文件的文件名並將其按照最後修改時間排序呢

linux中有相關的API函數,可以讀取目錄中所有的文件名字,以及時間屬性信息,你把這些信息讀出來,利用各種排序演算法排序就可以了

③ 一個linux下目錄遍歷函數,有個問題,chdir(dir);chdir("..")這兩句為什麼變更目錄

因為為了讓程序變得准確可行。
1、chdir("..");是為了在遞歸完某一子目錄後,退回到其父目錄繼續遍歷後續的普通文件或其他子目錄;如果缺少這一語句,那麼while循環中的遞歸printdir將會把父目錄中後續的普通文件當作目錄來操作,從而造成「無法打開目錄」這種錯誤。
2、chdir(dir);是為了在程序剛運行時進入指定的目錄,以及接下來遞歸時進入相應子目錄;

用`pwd`提取的絕對路徑。
[gag@genomic-server tmp]$ more test1
#!/usr/bin/perl -w
# script name is test
use strict;
my $d="/home/gag";
my $now=`pwd`;
print $now,"\n";
chdir $d;
print `pwd`;print `ls`;
`touch iamhere`;
print "#######################\n";
chdir $now;print `pwd`;
`touch iamherethen`;
[gag@genomic-server tmp]$ perl test1
/home/gag/perl/tmp
/home/gag
c
cpp1
cpp2
java
perl
shell
tools
#######################
/home/gag
[gag@genomic-server tmp]$ ls
test1
[gag@genomic-server tmp]$ ls ../../
c cpp1 cpp2 iamhere iamherethen java perl shell tools

④ linux下面如何遍歷目錄獲取文件

遍歷啥意思,獲取啥意思,就是全弄走么,
這個可以find 和tar組合,都提取的話,tar -czvf 備份.tgz 要提取的文件,
要只找幾個文件,可以先find,然後加入打包,注意壓縮的話就不能追加入包了。
find 位置 找啥 --exec tar -rvf 備份.tar {} \;

⑤ 如何在linux中使用shell腳本遍歷指定目錄的文件,將創建時間大於指定時間的文件,復制到指定目錄下。

大於指定時間?最簡單的就是直接find裡面指定吧。例如,查找當前目錄及其子目錄所有mtime大於1天的文件:
find
/path
-type
f
-mtime
+1
即可,/path
可以換成其他路徑,-mtime
+1
表示時間大於1天。-1的話表示小於一天也就是1天之內的。

⑥ Linux下怎樣遍歷整個目錄文件

linux中有相關的API函數,可以讀取目錄中所有的文件名字,以及時間屬性信息,你把這些信息讀出來,利用各種排序演算法排序就可以了

⑦ linux下遍歷讀取所有子目錄里的特定文件,並改名復制到別的目錄

find . -name a.txt -exec mv {} b.txt \; 其中find後面的"."表示從當前目錄開始查找(含子目錄),注意最後的「\;"是需要的。

⑧ 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中的遍歷目錄並刪除目錄下與目錄名相同的文件

先設定實驗環境:
#

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!

⑩ 怎麼使用java編程實現linux下所有文件目錄的遍歷

為了避免目錄列舉消耗時間過長,請指定一個目錄來模擬,命令行參數:代表路徑的字元串.
如果認可代碼,請加分50,謝謝

----

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.io.*;
final public class FileTree extends JFrame {
public FileTree(File dir) throws HeadlessException {
super("File Tree");
JTree tree;
add(new JScrollPane(tree =new JTree(buildTreeModel(dir))));
tree.setCellRenderer(new FileTreeRenderer());
setSize(400,600);
setVisible(true);
}

private TreeModel buildTreeModel(File dir){
DefaultMutableTreeNode root = new DefaultMutableTreeNode(dir);
walkthrough(dir,root);
return new DefaultTreeModel(root);
}

private static void walkthrough(File f,DefaultMutableTreeNode node){
for (File fle : f.listFiles()) {
DefaultMutableTreeNode n = new DefaultMutableTreeNode(fle);
node.add(n);
if (fle.isDirectory()){
walkthrough(fle, n);
}
}
}

private class FileTreeRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
JLabel cmp = (JLabel)super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode)value;
Object obj = n.getUserObject();
if (obj instanceof File) {
File f = (File)obj;
cmp.setText(f.getName());
cmp.setForeground(f.isDirectory()?Color.BLUE:Color.BLACK);
}
}
return cmp;
}
}

public static void main(String[] args) {
new FileTree(new File(args[0]));
}
}

熱點內容
醫院新冠肺炎疫情防控演練腳本 發布:2024-04-27 04:04:45 瀏覽:652
天津智慧網關伺服器雲伺服器 發布:2024-04-27 03:56:51 瀏覽:422
移門製作下料尺寸演算法 發布:2024-04-27 03:15:02 瀏覽:641
c語言5常量 發布:2024-04-27 02:38:49 瀏覽:991
源碼怎麼搭建 發布:2024-04-27 02:33:44 瀏覽:97
java獲取參數 發布:2024-04-27 02:22:21 瀏覽:501
unixlinuxwindows 發布:2024-04-27 02:10:55 瀏覽:445
nginx禁止ip訪問網站 發布:2024-04-27 02:05:43 瀏覽:845
webrtc伺服器搭建哪家價格低 發布:2024-04-27 01:30:08 瀏覽:141
oracle資料庫無法啟動 發布:2024-04-27 01:29:20 瀏覽:613