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

linux遍历文件

发布时间: 2022-02-16 21:21:15

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平台:使用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中的遍历目录并删除目录下与目录名相同的文件

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


代码如上,但如楼上所说,!慎用 !

㈣ linux用for遍历目标文件怎么做

你这是Windows的批处理脚本,不能在linux下用的

㈤ 怎么使用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]));
}
}

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

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

热点内容
上传文件文件夹找不到 发布:2024-09-20 00:26:32 浏览:914
承台箍筋加密区 发布:2024-09-20 00:26:31 浏览:227
笔记本什么配置能流畅运行cf 发布:2024-09-20 00:14:19 浏览:951
实测华为编译器 发布:2024-09-19 23:50:52 浏览:821
linux汇总 发布:2024-09-19 23:46:39 浏览:452
阿里云服务器环境搭建教程 发布:2024-09-19 23:21:58 浏览:837
黄色文件夹图标 发布:2024-09-19 23:19:22 浏览:684
mysql数据库导出导入 发布:2024-09-19 23:00:47 浏览:183
lua脚本精灵 发布:2024-09-19 23:00:41 浏览:659
任务栏文件夹图标 发布:2024-09-19 22:54:25 浏览:101