LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 11-06-2003, 04:05 PM   #1
scottrell
LQ Newbie
 
Registered: Mar 2003
Location: Minneapolis MN
Distribution: RH 8.0
Posts: 12

Rep: Reputation: 0
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
 
Old 11-06-2003, 04:07 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
easy peasy:

ls /var/film/..
 
Old 11-06-2003, 04:10 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
ok. and once i read the question correctly:

ls `dirname /var/film/blah`
 
Old 11-06-2003, 04:22 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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)'}
 
Old 11-06-2003, 04:28 PM   #5
scottrell
LQ Newbie
 
Registered: Mar 2003
Location: Minneapolis MN
Distribution: RH 8.0
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 11-06-2003, 05:34 PM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
chris@kermit chris $ basename `dirname /var/film/films/the_core.avi`
films
 
Old 11-07-2003, 09:10 AM   #7
scottrell
LQ Newbie
 
Registered: Mar 2003
Location: Minneapolis MN
Distribution: RH 8.0
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check if a parent directory is a symbolic link compnovice Linux - Newbie 11 10-06-2005 05:59 PM
.htaccess restricting parent directory Braytac Linux - Networking 2 05-26-2005 05:27 PM
'cp -rv .*' recurses into the parent directory??? stefanlasiewski SUSE / openSUSE 5 02-02-2005 05:36 PM
java - parent directory problem ludeKing Programming 8 10-07-2004 07:25 PM
mv the contents of one directory to the parent directory warkrime Linux - Newbie 4 07-14-2003 07:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration