简体   繁体   中英

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

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

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/

Why not create the directory tree you require once. Then use tar to create a tar file. Any machine you require the same directory structure just unpack the tar file.

A manual page for tar can be found from here

I tried with the find command find . -type f -print; to list all the type of files

and

find . -type d -print; to list all the type of directories

once we get the list of files or directories, use DirName command to get the path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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