Listing files with their parent dir?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Listing files with their parent dir?
# 1  
Old 11-19-2017
Apple Listing files with their parent dir?

Hi and good day,

This string lets me find the html files:

Code:
ls -R /Volumes/LC3/Sites/chu | grep  "html"

But how to list the files with their parent dir 'attached' to them:

For example:
Code:
n/fofo.html
n/siso.html
m/…

/

Any assistance would be greatly appreciated!

With best regards,
Omar KN

Last edited by Scott; 11-19-2017 at 04:32 PM.. Reason: Additional code tags
# 2  
Old 11-19-2017
Your question is a little vague. Do you mean only to report the direct parent of the .html file?

Like:
Code:
$ find . -name "*.html" | awk -F/ '{print $(NF-1)"/"$NF}'

e.g. a test:
Code:
$ mkdir /Volumes/LC3/Sites/chu/a /Volumes/LC3/Sites/chu/a/b /Volumes/LC3/Sites/chu/b
$ touch /Volumes/LC3/Sites/chu/a/a.html \
           /Volumes/LC3/Sites/chu/a/b/ab.html \
           /Volumes/LC3/Sites/chu/b/b.html
$ find /Volumes/LC3/Sites/chu -name *.html
/Volumes/LC3/Sites/chu/a/a.html
/Volumes/LC3/Sites/chu/a/b/ab.html
/Volumes/LC3/Sites/chu/b/b.html
$ find /Volumes/LC3/Sites/chu -name "*.html" | awk -F/ '{print $(NF-1)"/"$NF}'
a/a.html
b/ab.html
b/b.html

# 3  
Old 11-19-2017
If one wanted to use ls instead of find, one might try something like:
Code:
BaseDir=/Volumes/LC3/Sites/chu
{       printf '%s:\n' "$BaseDir"
        ls -R "$BaseDir"
} | awk -F/ '
/:$/ {  Parent = substr($NF, 1, length($NF) - 1) "/"
        next
}
/\.html$/ {
        print Parent $NF
}'

If you want to try this (or Scott's suggestion) on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 4  
Old 11-20-2017
I might be missing the point, but would this do?:-
Code:
cd /Volumes/LC3/Sites/chu
ls -1 */*.html

There might be an issue of either there are many many files or if the files you seek are not just one directory down.

If this doesn't suit, how about this?:-
Code:
cd /Volumes/LC3/Sites/chufind . -name "*.html" | sort             # the list from find will be in a seemingly random order

The second option is not perfect because it would show a leading ./ on every line but does that get you near your need?



Robin
# 5  
Old 11-20-2017
Hey, Robin.

My understanding was to find all the .html files under some tree, regardless of where they are within that structure, and show all of them with their parent directory.

Neither of your solutions does quite that (the second one I presume to be a 'cut-and-paste' error, as there's no find in that Smilie).
# 6  
Old 11-21-2017
Oh yes, what a mess. The suggestion should have been:-
Code:
cd /Volumes/LC3/Sites/chu
find . -name "*.html" | sort             # the list from find will be in a seemingly random order

Of course, this may list several directories depth, I'm not sure if that is too much detail or not.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Assign read write permission to the user for specific dir and it's sub dir and files in AIX

I have searched this quite a long time but couldn't find the right method for me to use. I need to assign read write permission to the user for specific directories and it's sub directories and files. I do not want to use ACL. I do not want to assign user the same group of that directories too.... (0 Replies)
Discussion started by: blinkingdan
0 Replies

2. Shell Programming and Scripting

KSH - Find paths of multiple files in CC (dir and sub-dir))

Dear Members, I have a list of xml files like abc.xml.table prq.xml.table ... .. . in a txt file. Now I have to search the file(s) in all directories and sub-directories and print the full path of file in a output txt file. Please help me with the script or command to do so. ... (11 Replies)
Discussion started by: Yoodit
11 Replies

3. UNIX for Dummies Questions & Answers

How to list all files in dir and sub-dir's recursively along with file size?

I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In have to list all the files in directory and its sub directories along with file path and size of the file Please help me in this regard and many thanks in advance. (3 Replies)
Discussion started by: nmakkena
3 Replies

4. Windows & DOS: Issues & Discussions

DOS Dir - listing of full path and timestamp

Hi, (Apologies, I'm sure I'm not the first person to raise this question but so far in my searches haven't found a good answer). I would like to output a listing per line of filename (including full path) and 'last updated' timestamp. e.g: Z:\dir1\file1.txt 01/02/2010 10:43... (5 Replies)
Discussion started by: GM_AIX
5 Replies

5. UNIX for Dummies Questions & Answers

Listing files in a non-parent directory

Hi, Edit: The title should really read listing files in a non-parent directory, sorry! Im trying to get one of my Bash scripting assignments done for uni and now I'm stuck. This is probably going to be one of those kick yourself moments but, in my script I have a variable usrDir which... (2 Replies)
Discussion started by: Adzi
2 Replies

6. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies

7. Shell Programming and Scripting

Rename a file after the name of its parent dir

Started a thread in beginners but I thought someone from this forum may have an idea as well. Thanks! EDITED BY REBORG (3 Replies)
Discussion started by: robotsbite
3 Replies

8. Shell Programming and Scripting

full path of a file situated either in parent's dir. or parent's parent dir. so on...

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to... (1 Reply)
Discussion started by: yahoo!
1 Replies

9. UNIX for Dummies Questions & Answers

listing home dir of anothe user

Hi I am trying to display the home directory of another user. I dont have the permissions to change into their directory and wonder is it possible to display their directory eg I cant change directory to john but I am trying to display something like /home/John. If anyone can help I would... (3 Replies)
Discussion started by: mmg2711
3 Replies

10. Shell Programming and Scripting

advanced dir listing

Hi i know this Q might seem retarded... yet, i can't find a solution. i need a bash script that recursively prints like this: mydir/ myfile1 114 06 Aug 2006 myfile2 234 14 Jun 2006 mydir/mysubdir1/ myfile3 32 18 Feb 2006 mydir/mysubdir1/mysubsubdir1/ myfile4 5324 06 Aug 2006 ... (2 Replies)
Discussion started by: xenophobian
2 Replies
Login or Register to Ask a Question