Operating System - HP-UX
1753479 Members
4794 Online
108794 Solutions
New Discussion юеВ

Re: capturing directories of files

 
SOLVED
Go to solution
Henry Chua
Super Advisor

capturing directories of files

Hi guys,

If I have say :
DIR1/DIR2/DIR3/a.txt
DIR1/DIR2/b.txt
DIR1/DIR2/c.txt
DIR1/DIR2/DIR3/DIR4/d.txt
DIR1/DIR2/DIR3/e.txt
...
If the list goes on and on, how do i write a script to create a list of Directories where the files belong to..
i.e.
DIR1/DIR2/DIR3/
DIR1/DIR2/
DIR1/DIR2/
DIR1/DIR2/DIR3/DIR4/
DIR1/DIR2/DIR3/

any suggestion?

thanks!








5 REPLIES 5
Thierry Poels_1
Honored Contributor

Re: capturing directories of files

Hi,

have a look at "dirname". "dirname" and "basename" are used to extract directory name and file name from a full path.

regards,
Thierry Poels.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Thierry Poels_1
Honored Contributor

Re: capturing directories of files

or if you simply want to cut of the last field in a file:

sed "s/[^\/]*$//" yourfile

regards,
Thierry Poels.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Henry Chua
Super Advisor

Re: capturing directories of files

Hi Thierry,

The sed command was really very helpful, can u roughly explain how the command works?

more importantly, what if I want to output:

DIR1/DIR2/DIR3
DIR1/DIR2
DIR1/DIR2
DIR1/DIR2/DIR3/DIR4
DIR1/DIR2/DIR3

i.e. less the extra "/" at the back?

really hope u can help
THank u!!!
Thierry Poels_1
Honored Contributor
Solution

Re: capturing directories of files

hi,

sed "s/\/[^\/]*$//" yourfile
(just added another (escaped) / )
or
sed "s;/[^/]*$;;" yourfile
(with a different delimeter)


[^\/] = any character different from "/"
[^\/]* = contiguous group of characters different from "/"
[^\/]*$ = contiguous group of characters different from "/" at the end of the line
and simply replace it with an empty string.
See "man sed" for more info.

regards,
Thierry Poels.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Henry Chua
Super Advisor

Re: capturing directories of files

Thank u Thierry.. the commands work spendidly..

Many thanks!!