shell写了个递归遍历目录的脚本,本脚本实现递归遍历指定目录,打印目录下的文件名(全路径)。

#!/bin/sh   
    
function scandir() {   
    local cur_dir parent_dir workdir   
    workdir=$1   
    cd ${workdir}  
 
    if [ ${workdir} = "/" ]   
    then   
        cur_dir=""   
    else   
        cur_dir=$(pwd)   
    fi   
  
   for dirlist in $(ls ${cur_dir})   
   do   
           if test -d ${dirlist}
        then   
           cd ${dirlist}   
           scandir ${cur_dir}/${dirlist}   
           cd ..   
       else   
          echo ${cur_dir}/${dirlist} 
       fi   
   done   
}   
  
if test -d $1   
then   
   scandir $1   
elif test -f $1   
    then   
        echo "you input a file but not a directory,pls reinput and try again"   
           exit 1   
    else   
           echo "the Directory isn't exist which you input,pls input a new one!!"   
           exit 1   
fi

=================下面用php命令行执行php脚本============================

<?php
function traverse($path = '.') {
$current_dir = opendir($path); //opendir()返回一个目录句柄,失败返回false
while(($file = readdir($current_dir)) !== false) { //readdir()返回打开目录句柄中的一个条目
$sub_dir = $path . DIRECTORY_SEPARATOR . $file; //构建子目录路径
if($file == '.' || $file == '..') {
continue;
} else if(is_dir($sub_dir)) { //如果是目录,进行递归
echo 'Directory ' . $file . ':\r\n';
traverse($sub_dir);
} else { //如果是文件,直接输出
echo 'File in Directory ' . $path . ': ' . $file . '\r\n';
}
}
}
/*$argc 表示传入php参数个数,$agrv表示传入的参数数组
*类似于 array('xxxx.php','参数1','参数2');
*/
if($argc==2){
$path=$argv[1];
} else{
die('error msg!');
}
if(!is_dir($path)){
exit($path.' is not a directry!');
}
traverse($path); ?> 终端这样调用:[root@xiuran test]# php scan.php /mybak/ 这样就和上边的执行结果一样。【前提php命令要在当前目录下能够执行,可以做一个软连接: ln -s /usr/local/php/bin/php  /sbin/php 这样任何目录下都可访问了。】

PHP和shell脚本遍历目录及其下子目录的更多相关文章

  1. shell脚本中获取当前所在目录地址

    shell脚本中获取当前所在目录如下 #!/bin/bash work_path=$() cd ${work_path} work_path=$(pwd) cd ${work_path}/src

  2. Shell脚本:使用rsync备份文件/目录

    本文我们介绍一个shell脚本,用来使用rsync命令将你本地Linux机器上的文件/目录备份到远程Linux服务器上.使用该脚本会以交互的方式实施备份,你需要提供远程备份服务器的主机名/ip地址和文 ...

  3. shell 脚本中的当前工作目录等于执行脚本时所在的工作目录

    1. 测试脚本 在当前目录下创建 h1 h2两个文件夹 2. 在脚本所在目录执行脚本 在当前目录下创建了h1 h2两个目录 3. 在其他目录爱=下执行脚本 在其他目录下执行脚本时,在执行脚本所在的目录 ...

  4. 《Shell脚本学习指南》书籍目录

    摘要:Shell脚本与Windows/Dos下的批处理相似,也就是用各类命令预先放入到一个文件中,方便一次性执行的一个程序文件,主要是方便管理员进行设置或者管理用的.但是它比Windows下的批处理更 ...

  5. shell脚本遍历当前目录下以数字命名的目录,并打印

    #!/bin/bash single='' #定义以个位数为目录的集合double='' #定位十位数为目录的集合#按照需要可以根据实际情况再定义以百位数为目录的集合 for dir in `ls - ...

  6. 【操作系统作业—lab1】linux shell脚本 遍历目标文件夹和所有文件 | 包括特殊字符文件名的处理

    要求:写一个linux bash脚本来查看目标文件夹下所有的file和directory,并且打印出他们的绝对路径. 运行command:./myDir.sh  input_path  output_ ...

  7. shell脚本实现仅保留某目录下最新的两个文件

    #!/bin/sh export DS_DIR=/home/cxy/test if [ ! -d $DS_DIR ]; then mkdir $DS_DIR else echo "$DS_D ...

  8. shell脚本遍历子目录

    #!/bin/bashsource /etc/profile tool_path=/data/rsync_clientroot_path=/data/log ####yyyy-mm-dd¸ñʽdat ...

  9. 100个Shell脚本—【脚本6】拷贝目录

    [脚本6]拷贝目录 编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下: 一.脚本 #!/bin/bash cd /root list=(`ls`) for i i ...

随机推荐

  1. dvwa 源码分析(三) --- config.inc.php分析

    <?php # If you are having problems connecting to the MySQL database and all of the variables belo ...

  2. js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结【转】

    [摘要:之前常常正在代码中看到Math.round.parseInt.Math.floor战Math.ceil那四个函数,固然晓得效果皆能够返回一个整数,然则对他们四者的差别照样没有太清晰,本日便做一 ...

  3. C# Winform获取bin目录的路径

    //获取到bin目录的下层路径:bin\Debug\ string aa = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBa ...

  4. 在Window下安装解压版的mysql 5.7.11

    今天由于要在windows下学习Kettle,因此在Windows下安装了mysql 5.7.11,本来是没什么大问题的,但是在启动服务时还是出了点问题,服务老是启动不了: (一)解压到安装路径:   ...

  5. ubuntu多节点安装kubernetes

    在ubuntu上面多节点安装kubernetes,假设有两台机器 master:192.168.1.84 minion:192.168.1.83 You wil now need to configu ...

  6. hadoop上线和下线节点

    在运行中的ambari hadoop集中中动态添加或删除节点 1. 下线节点1) namenode节点上dfs.exclude文件,看配置文件怎么配置的,里每行添加一个服务器名,如我要下线server ...

  7. Redis Key 过期策略

    redis 官方提供的 conf https://raw.github.com/antirez/redis/2.2/redis.conf 中6中过期策略的具体方式.redis 中的默认的过期策略是vo ...

  8. java多线程有几种实现方法?线程之间如何同步

    java中多线程的实现方法有两种:1.直接继承thread类:2.实现runnable接口: 同步的实现方法有五种:1.同步方法:2.同步代码块:3.使用特殊域变量(volatile)实现线程同步:4 ...

  9. ansible debug模块学习笔记

    - name: Print debug infomation eg hosts: test2 gather_facts: F tasks: - name: Command run line shell ...

  10. C#中对 XML节点进行添加,删除,查找和删除操作

    从网上整理所得 XMLDocument来操作XML比较简单,虽然有时效率不是很高.代码如下 已知有一个XML文件(bookstore.xml)如下: <?xml version="1. ...