简体   繁体   English

如何在Unix Shell脚本中为任何目录结构构建文件夹路径

[英]how to build a folder path in Unix shell scripting for any directory structure

My Application has a folder structure like 我的应用程序具有类似的文件夹结构

/PNRPHD1/ex1/11.txt

/PNRPHD1/ex1/ex2/11.txt

/PNRPHD1/ex1/ex2/ex3/23.txt

/PNRPHD1/ex1/ex2/34.txt

/PNRPHD1/ex1/ex3/sp4/sp3/4334.txt,

but /PNRPHD1 is main folder , within that folder I need to iterate every time and go to the last level and check if the file exists or not,in that Directory or not, if File is the content of directory, then i need to build that path and come out of that loop. 但是/ PNRPHD1是主文件夹 ,在该文件夹中,我需要每次进行迭代并转到最后一级,并检查文件是否存在,在该目录中是否存在,如果File是目录的内容,那么我需要构建那条路,走出那个循环。 and I should have the above mentioned path as the output of the variable. 并且我应该将上述路径作为变量的输出。

Note: In the above directory structure, each directory can contain either a other Sub directory or a File, it wont have both 注意: 在以上目录结构中,每个目录可以包含另一个子目录或一个文件,但不会同时包含这两个目录

Can any one suggest I was trying to do that in a IF loop, But that is getting too much of code, I wanted to implement with minimum code 任何人都可以建议我尝试在IF循环中执行此操作吗,但是代码太多,我想用最少的代码实现

DIR_REG = /PNRPHD1;
    ls -p $DIR_REG | while read -r dir
    do
        echo "directory under is  -- " ${dir} >>${DEBUG_LOG_FILE}
        BUP="/cygdrive/d/psoft/${PSFT_SID}/${dir}";

        ls -p $BUP | while read -r dir1
        do 
            if [ -f "${BUP}/${dir1}" ]; then 
                mkdir -p "${BUP}backup"
                echo "level of directory  -- " ${BUP}/${dir1} >>${DEBUG_LOG_FILE}
            else
                echo "level of directory  -- " ${BUP}/${dir1} >>${DEBUG_LOG_FILE}
            fi

        done

    done

I am looking for a better shell script and concise one, which does the required task.. 我正在寻找一个更好的shell脚本,并简洁地完成所需的任务。

Updated : 更新 :
I am expecting the output of my loop should have a variable output in each iteration. 我期望循环的输出在每次迭代中都应该有一个可变的输出。 1st Case : /ex1/ 2nd case : /ex1/ex2 3rd Case : ex1/ex2/ex3/ 4th Case : /ex1/ex2/ex3/ 5th Case : /ex1/ex3/sp4/sp3/ 第一种情况:/ ex1 /第二种情况:/ ex1 / ex2第三种情况:ex1 / ex2 / ex3 /第四种情况:/ ex1 / ex2 / ex3 /第三种情况:/ ex1 / ex3 / sp4 / sp3 /

Why not create the directory tree you require once. 为什么不创建一次您需要的目录树。 Then use tar to create a tar file. 然后使用tar创建一个tar文件。 Any machine you require the same directory structure just unpack the tar file. 您需要相同目录结构的任何计算机都只需解压缩tar文件即可。

A manual page for tar can be found from here 可以在这里找到tar的手册页

I tried with the find command find . 我尝试使用find命令find。 -type f -print; 型f打印 to list all the type of files 列出所有文件类型

and

find . 找 。 -type d -print; 型d打印 to list all the type of directories 列出所有目录类型

once we get the list of files or directories, use DirName command to get the path 一旦我们获得文件或目录的列表,请使用DirName命令获取路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM