ksh - walking back up a directory PATH


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers ksh - walking back up a directory PATH
# 1  
Old 12-01-2008
ksh - walking back up a directory PATH

Hi there,

I am putting a script together to apply a label to a directory structure but it can't be done down the directory structure, i must start at the end and work back and this is what i am struggling with.

for example, i will supply a path as an argument to a script, e.g

dir1/dir2/dir3/dir4/dir5/dir6

i then want to loop round but each time remove the end element of the path e.g

1st pass = dir1/dir2/dir3/dir4/dir5/dir6
2nd pass = dir1/dir2/dir3/dir4/dir5
3rd pass = dir1/dir2/dir3/dir4
4th pass = dir1/dir2/dir3
5th pass = dir1/dir2
6th pass = dir1

The path will not always be 6 entries long though, it could be anywhere in between 2 and 10 entries long.
Has anybody done this before, or give me pointers on which utility within ksh i can use, e.g awk, nawk etc.

many thanks in advance

Steve
# 2  
Old 12-01-2008
Read all the directories with find, print the length of the name before the name with awk, sort the line (numeric, reverse) and remove the numbers with cut:

Code:
find /dir1 -type d|awk '{print length, $0}'|sort -nr|cut -d " " -f2-  |
while read dir
do
  # do something with "$dir"
done

Regards
# 3  
Old 12-01-2008
Code:
#!/bin/ksh
START_DIR="$1"
if [ ! -d "${START_DIR}" ]
then
        echo "Directory missing: ${START_DIR}"
fi
cd "${START_DIR}"
#
while true
do
        CURRENT_DIR="`pwd`"
        if [ "${CURRENT_DIR}" = "/" ]
        then
                break
        fi
        echo "${CURRENT_DIR}"
        #
        # Actions per directory go here
        #
        NEXT_DIR="`dirname ${CURRENT_DIR}`"
        if [ "${NEXT_DIR}" = "${CURRENT_DIR}" ]
        then
                break
        fi
        cd "${NEXT_DIR}"
done

# 4  
Old 12-01-2008
Quote:
Originally Posted by surfbus78
dir1/dir2/dir3/dir4/dir5/dir6

i then want to loop round but each time remove the end element of the path
This can easily be done by shell variable expansion means: to cut everything after the last slash "/" in a variable var use: "${var%/*}". Therefore:

Code:
var="/a/b/c/d/e/f"

while [ -n "$var" ] ; do
     print - "before: $var"
     var="${var%/*}"
     print - "after : $var"
done

I hope this helps.

bakunin
# 5  
Old 12-03-2008
hi there,

thanks for the fantastic responsese. I've tried a few of the suggestions but what i need to avoid is the need to have to actually be in the directory structure.
Basically i want to provide the tree structure as a value and have this broken down into its constituent parts as described in the first post.

can anybody help?

cheers

Steve
# 6  
Old 12-03-2008
Hi.

Running the (ksh) script from bakunin produces:
Code:
before: /a/b/c/d/e/f
after : /a/b/c/d/e
before: /a/b/c/d/e
after : /a/b/c/d
before: /a/b/c/d
after : /a/b/c
before: /a/b/c
after : /a/b
before: /a/b
after : /a
before: /a
after :

Which seems to satisfy the requirement ... cheers, drl
# 7  
Old 12-03-2008
hmm, i'll give it another try then.

potentially it might be a problem with my ksh, which i forgot to mention is the solaris implementation, which i have experienced issues with previously when compared to ksh on say a linux box.

i'll keep you posted, thanks for the response BTW

Steve
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What is the difference ../directory path and ./directory path in ksh?

What is the difference ../directory path and ./directory path in ksh? (1 Reply)
Discussion started by: TestKing
1 Replies

2. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

3. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

4. UNIX for Dummies Questions & Answers

Move back to a directory

~/assign1b/assign1/york/> your current directory is york how do you go back to your home directory with using relative pathname? not using cd alone? :( (3 Replies)
Discussion started by: blackendstars
3 Replies

5. UNIX for Dummies Questions & Answers

alias to go back to previous directory?

Some time ago I was using a linux system and someone had set up and alias called "back" that got me back to the directory I was last in, which was very useful. I've now switched to using a mac, and don't have those aliases available. Can anyone tell me how I could make a an alias that would... (2 Replies)
Discussion started by: ac2011
2 Replies

6. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

7. Shell Programming and Scripting

Puzzling Problem: Going back to the previous directory

Hi, I am trying to figure out if there is a way to make linux take me back to the previous directory I was working in. For instance, I am in /home/username/directory1 Then if I cd into /home/username/directory1/temp1/temp2/temp3 I would like to immediately go back to the previous... (2 Replies)
Discussion started by: Legend986
2 Replies

8. Linux

how to recover from back up directory

Hey all, Is there any way out to get/restore the deleted file in Linux ? As we do have Recycle Bin in Windows OS. as i deleted some files.. it is available in .snapshot but its a large files.. i cont use "mv" command to copy back. it there any way .. such like in window we click restore... (1 Reply)
Discussion started by: mail2sant
1 Replies

9. UNIX for Dummies Questions & Answers

Path autocompletion in ksh

Say, I want to move into dir library from current dir, on prompt if I type in cd li if it is followed by pressing 'Tab' key then complete dir name appears. Would there anyone know, how we can make into effect this path autocompletion? I am using 'ksh'. (5 Replies)
Discussion started by: videsh77
5 Replies

10. UNIX for Dummies Questions & Answers

? question mark, how to get back to the root directory

hiyas I am trying to get back to the root directory: I went into MAIL directory and now I can't get back to the root directory. What are the commands... I have '?' coming up and I cannot proceed with this, HELP Cheers (1 Reply)
Discussion started by: etravels
1 Replies
Login or Register to Ask a Question