Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-06-2003, 04:05 PM
|
#1
|
LQ Newbie
Registered: Mar 2003
Location: Minneapolis MN
Distribution: RH 8.0
Posts: 12
Rep:
|
List only the parent directory
It is a simple issue, really, but cannot seem to find an answer.
I'd like to output the parent directory name of a file, e.g.;
# parentdir /home/foo/bar/filename
bar
That's all. Seems it can be done, but only with a script--not with an existing common command, like find or ls.
-sc
|
|
|
11-06-2003, 04:07 PM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
easy peasy:
ls /var/film/..
|
|
|
11-06-2003, 04:10 PM
|
#3
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
ok. and once i read the question correctly:
ls `dirname /var/film/blah`
|
|
|
11-06-2003, 04:22 PM
|
#4
|
Moderator
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047
Rep:
|
Maybe I'm missing something but do you not just want the name of the parent directory and not to list the files within it? If so then try:
pwd | awk -F"/" {'print $(NF-1)'}
|
|
|
11-06-2003, 04:28 PM
|
#5
|
LQ Newbie
Registered: Mar 2003
Location: Minneapolis MN
Distribution: RH 8.0
Posts: 12
Original Poster
Rep:
|
thanks for the help, but that returns the dir listing of blah's parent.
I've got three files in /home/foo/bar:
file1
file2
file3
I'd like a command on, for example, /home/foo/bar/file1 that will return its parent directory without all the lead directories, just:
bar
Thanks again
-sc
|
|
|
11-06-2003, 05:34 PM
|
#6
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
chris@kermit chris $ basename `dirname /var/film/films/the_core.avi`
films
|
|
|
11-07-2003, 09:10 AM
|
#7
|
LQ Newbie
Registered: Mar 2003
Location: Minneapolis MN
Distribution: RH 8.0
Posts: 12
Original Poster
Rep:
|
Thanks david_ross and acid_kewpie, both will do the job--although the basename `dirname` option is a little more straight forward, and works whether you feed it a directory with/without trailer
Now just to get it to work within an -exec option on find.
# find /home -user deleteduser -exec chown basename 'dirname {}` {} \;
Don't be concerned too much about what it does, it's appropriate for what I'm trying to do. (find files owned by a deleted user and assign ownership to a user with a name equal to basename `dirname`--the basename `dirname` user will exist).
-sc
|
|
|
All times are GMT -5. The time now is 02:58 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|