使用shell命令遍历目录和子目录文件并输出到文本

“input_dir”代表当前目录,“output_file”代表输出文件,你可以自己根据情况修改,

 1 #!/bin/bash
 2 function getdir(){
 3     for element in `ls $1`
 4     do
 5         dir_or_file=$1"/"$element
 6         if [ -d $dir_or_file ]
 7         then
 8             getdir $dir_or_file
 9         else
10             echo $dir_or_file >> $output_file
11         fi
12     done
13 }
14 input_dir="."
15 output_file="out.txt"
16 getdir $input_dir

借鉴自此:http://www.cnblogs.com/xiaopipi/p/6214673.html   发博仅为方便以后查看

时间: 2024-09-01 21:47:48

使用shell命令遍历目录和子目录文件并输出到文本的相关文章

Makefile 递归遍历目录(含子目录) 编译动态库

这里推荐一本书,Makefile手册,本人正在学习,多交流~ 一.统一编译所有子目录的文件 直接上Makefile内容了, AR=arLD=ldCC=gcc CFLAGS = -O2 -Wall  -I./Test \                -I./Test/Test1 \ #注:"\"后面不能有空格,并且该句写完后最好有个换行 #注释部分推荐在单独的一行编写 #动态库需要 -fPIC  -shared SOFLAGS = -O2 -fPIC -sharedTARGET = .

Python递归遍历目录下所有文件

#自定义函数: import ospath="D:\\Temp_del\\a" def gci (path): parents = os.listdir(path) for parent in parents: child = os.path.join(path,parent) #print(child) if os.path.isdir(child): gci(child) # print(child) else: print(child) gci(path) #使用os.walk方

(实用篇)PHP不用递归遍历目录下所有文件的代码

<?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ function scanfiles($dir) { if (! is_dir ( $dir )) return array (); // 兼容各操作系统 $dir = rtrim ( str_replace ( '\\', '/', $dir ), '/' ) . '/'; // 栈,默认值为传入的目录 $

遍历目录中所有文件并统计信息

遍历目录中所有文件,并且统计文件类型. #!/bin/bash #filename: filestat.sh #set -x if [ $# -ne 1 ]; then     echo $0 basepath;    echo fi path=$1 declare -A statarray; while read line; do   ftype=`file -b "$line"`   let statarray["$ftype"]++; done< <

shell脚本遍历目录及其下子目录

用shell写了个递归遍历目录的脚本,本脚本实现递归遍历指定目录,打印目录下的文件名(全路径). #!/bin/sh       function scandir() {       local cur_dir parent_dir workdir       workdir=$1       cd ${workdir}       if [ ${workdir} = "/" ]       then           cur_dir=""       else 

Linux和Windows的遍历目录下所有文件的方法对比

首先两者读取所有文件的方法都是采用迭代的方式,首先用函数A的返回值判断目录下是否有文件,然后返回值合法则在循环中用函数B直到函数B的返回值不合法为止.最后用函数C释放资源. 1.打开目录 #include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); 先看Linux的,返回的是DIR*,因此出错时返回NULL(0).而这里不用关心DIR结构具体定义,只需要知道是对它进行操作(注意:DIR不是保存文

遍历目录及子目录

遍历目录及子目录 unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrls; type   TForm1 = class(TForm)     Memo1: TMemo;     Button1: TButton;     Button2: TButton;     procedure Button1Clic

java递归遍历目录获取所有文件及目录方案

本文提供一份递归遍历目录获取所有文件及目录的源代码: import java.io.File; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2019/2/10. */ public class TestWalkDir { static class FileComponent { File curFile; List<FileComponent> fileComponen

Python os模块实例之遍历目录及子目录指定扩展名的文件

需求:在该目录下有很多子目录(如下图,截图了部分),现要从该目录和所有子目录下找到所有扩展名为.meta的文件,并获取文件中第二行guid的值(': '后面的),然后将所有guid的值输出到另一文件中 .meta文件截图: 思路: 遍历当前目录的子目录,如果是文件并且扩展名为.meta,获取guid,写入到guid.txt:如果是子目录,则遍历子目录下的文件. 方法一: #!usr/bin/env python # -*- coding: utf-8 -*- import os outfile