LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-09-2013, 11:23 AM   #1
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Rep: Reputation: Disabled
find maxdepth and print only the maxdepth matches


Hi All,
I have directory structre as below
Code:
$ tree /tmp/OS/ -d
/tmp/OS/
|-- Linux
|   |-- Debian
|   |   `-- test
|   |-- Fedora
|   |-- RedHat
|   `-- Suse
|       `-- test1
|           `-- test2
`-- Windows
    |-- Vista
    `-- XP

11 directories
Now I wanted print only upto 3rd subdirectory inside /tmp. For example I wanted to get output as only below
Code:
/tmp/OS/Linux/Debian
/tmp/OS/Linux/Fedora
/tmp/OS/Linux/RedHat
/tmp/OS/Linux/Suse
/tmp/OS/Windows/Vista
/tmp/OS/Windows/XP
and wanted hide everything which are lesser than or greter than 4th directory. For that I ran
Code:
$ find /tmp/OS/ -maxdepth 2 -type d | sort
/tmp/OS/
/tmp/OS/Linux
/tmp/OS/Linux/Debian
/tmp/OS/Linux/Fedora
/tmp/OS/Linux/RedHat
/tmp/OS/Linux/Suse
/tmp/OS/Windows
/tmp/OS/Windows/Vista
/tmp/OS/Windows/XP
Since I am getting output as above how can I hide these below?
Code:
/tmp/OS/
/tmp/OS/Linux
/tmp/OS/Windows
Please help me.
 
Old 06-09-2013, 11:49 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
something like this would work for your example

Code:
find /tmp/OS/*/* -maxdepth 0 -type d
 
1 members found this post helpful.
Old 06-09-2013, 01:03 PM   #3
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
something like this would work for your example

Code:
find /tmp/OS/*/* -maxdepth 0 -type d
Thanks a lot.
 
Old 06-09-2013, 05:11 PM   #4
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 353

Rep: Reputation: 152Reputation: 152
Alternatively, you could use -mindepth:
Code:
find /tmp/OS/ -maxdepth 2 -mindepth 2 -type d
 
Old 06-11-2013, 12:36 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
This particular job could also be done with shell globbing.

Code:
printf '%s\n' /tmp/OS/*/*/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Search two text documents, eliminate matches, print third document with unmatched+. ramma Programming 15 07-12-2012 12:09 PM
help with grep... only print specific string with multiple matches? trey85stang Linux - General 2 05-11-2009 09:29 AM
fedora4+cbq+maxdepth problem sumonhere Linux - Networking 1 03-17-2007 03:45 AM
Regex Question: Only print part of line that matches TheMeteorPolice Programming 5 01-12-2006 01:21 PM
Simulating mindepth, maxdepth in find subu_s Programming 1 04-11-2005 05:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration