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

linux遍歷目錄文件

發布時間: 2023-06-06 13:11:01

A. linux shell中的遍歷目錄並刪除目錄下與目錄名相同的文件

;do
if[-d/$i/$i];then
rm-rf/$i/$i
fi
done


代碼如上,但如樓上所說,!慎用 !

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

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

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

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

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

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

E. 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!

F. 怎麼使用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]));
}
}

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

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

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

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

熱點內容
scratch少兒編程課程 發布:2025-04-16 17:11:44 瀏覽:626
榮耀x10從哪裡設置密碼 發布:2025-04-16 17:11:43 瀏覽:356
java從入門到精通視頻 發布:2025-04-16 17:11:43 瀏覽:71
php微信介面教程 發布:2025-04-16 17:07:30 瀏覽:296
android實現陰影 發布:2025-04-16 16:50:08 瀏覽:787
粉筆直播課緩存 發布:2025-04-16 16:31:21 瀏覽:337
機頂盒都有什麼配置 發布:2025-04-16 16:24:37 瀏覽:202
編寫手游反編譯都需要學習什麼 發布:2025-04-16 16:19:36 瀏覽:798
proteus編譯文件位置 發布:2025-04-16 16:18:44 瀏覽:355
土壓縮的本質 發布:2025-04-16 16:13:21 瀏覽:582