LinuxQuestions.org
Help answer threads with 0 replies.
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 10-15-2016, 11:53 AM   #1
TheEngineer
LQ Newbie
 
Registered: Oct 2006
Location: UK
Distribution: MacOS , Redhat
Posts: 17

Rep: Reputation: 0
Unix shell script/command help needed...


Hi all.

I need some help with the following scenario...


I have this directory structure:

/Dir 1/Dir 2/Dir 3/Dir X/Dir 5/Dir 6/files

/Dir 1/Dir 2/Dir 3/Dir Y/Dir 5/Dir 6/files

/Dir 1/Dir 2/Dir 3/Dir Z/Dir 5/Dir 6/files


Is there a way to list all the files in each path without me having to type the whole path out into the terminal?


I know ls -1 /Dir 1/Dir 2/Dir 3 will list the directories Dir X, Dir Y and Dir Z on separate lines (with no recursion).


Is there a way to insert each separate item that is listed into a find command to search for a file of type *.txt for example?


eg: find /Dir 1/Dir 2/Dir 3/ITEM/Dir 5/Dir 6/ -name *.txt

where ITEM would be the output from the ls command.


All the directory paths have a common path up to Dir X and then the remainder of the path is also common too.


Thanks in advance.
 
Old 10-15-2016, 12:22 PM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,831
Blog Entries: 36

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
You already answered your own question. Use find command but modify it a little bit.

Code:
find "/your/path" -type f -name '*.txt'
In the above example,
  • Quote the "/your/path". If your directories have spaces in the name then you have to quote the spaces. I used double quotes in case you wanted to use variables. Single quotes take away special meaning from things like variables.
  • -type f because I only want to find files.
  • I single quoted '*.txt' because I didn't want the shell to take special meaning from the asterisk (*). I wanted to pass the literal pattern to the find command.

WITH THAT being said. It sounds like you're not asking your full question. What is the real problem you're trying to solve? Don't give us the trees. Give us the forest. If we know the whole problem you're trying to solve then it's possible we could give you a whole solution that is simpler than your line of thought.
 
Old 10-15-2016, 12:24 PM   #3
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 956
Blog Entries: 3

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
Quote:
Originally Posted by TheEngineer View Post
eg: find /Dir 1/Dir 2/Dir 3/ITEM/Dir 5/Dir 6/ -name *.txt
You're complicating it by thinking about trying to use output from "ls". The program "find" ought to be enough on its own. You just need to write the path properly, if the above is the real example. Look at the manual for "find" and check out -type f. If there are files in /Dir 1/Dir 2/Dir 3/ITEM or its subdirectories that you don't want, then consider also using -mindepth
 
1 members found this post helpful.
Old 10-15-2016, 12:39 PM   #4
TheEngineer
LQ Newbie
 
Registered: Oct 2006
Location: UK
Distribution: MacOS , Redhat
Posts: 17

Original Poster
Rep: Reputation: 0
There are a few hundred directories that are listed from the command: ls -1 /Dir 1/Dir 2/Dir 3

I gave only a simple example as there are 10 subdirectories below the few hundred directories from the output of the above ls command.

I don't want to have to type 100s of find commands out.

I only want to search the branch of subdirectories that I specify after the ITEM directory:

eg: /Dir 1/Dir 2/Dir 3/ITEM/Dir 5/Dir 6/

There are 1000s of other subdirectory branches that are not important.
 
Old 10-15-2016, 12:44 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,919

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by TheEngineer View Post
There are a few hundred directories that are listed from the command: ls -1 /Dir 1/Dir 2/Dir 3

I gave only a simple example as there are 10 subdirectories below the few hundred directories from the output of the above ls command. I don't want to have to type 100s of find commands out. I only want to search the branch of subdirectories that I specify after the ITEM directory:

eg: /Dir 1/Dir 2/Dir 3/ITEM/Dir 5/Dir 6/

There are 1000s of other subdirectory branches that are not important.
Great...so now that your duplicate thread has been reported (read the LQ Rules), did you read the "Question Guidelines"? We know what you WANT, so now you need to show us what you have actually DONE to accomplish this. We're happy to help, but we aren't going to write your scripts for you. There are THOUSANDS of easily-found bash scripting tutorials about finding directories which you can locate with a Google search. Start there.
 
1 members found this post helpful.
Old 10-15-2016, 12:45 PM   #6
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 956
Blog Entries: 3

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
With "find" you specify the starting path. Then everything else that follows is relative to that path. As sag47 warned about about quoting the file name to avoid globbing there, you can use globbing to your advantage with the path name.

Code:
find /Dir1/Dir2/Dir3/*/Dir5/Dir6/ -type f -name '*.txt' -print
If I understand your question, you want only the files in subdirectories ./Dir5/Dir6/ underneath /Dir1/Dir2/Dir3/*/
 
Old 10-15-2016, 12:57 PM   #7
TheEngineer
LQ Newbie
 
Registered: Oct 2006
Location: UK
Distribution: MacOS , Redhat
Posts: 17

Original Poster
Rep: Reputation: 0
Forgive me for duplicating the post as I'm not sure if posted in the right forum.

Yes, that's correct... /Dir1/Dir2/Dir3/*/Dir5/Dir6/

I wasn't sure about the wildcard used in the path.
 
Old 10-15-2016, 01:02 PM   #8
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 956
Blog Entries: 3

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
In the shell its globbing. If you leave off the quotes, the * and ? have special meaning. If they are wrapped in quotes, then they are interpreted literally.

Compare these two:

Code:
ls  /Dir1/Dir2/Dir3/*/Dir5/
ls '/Dir1/Dir2/Dir3/*/Dir5/'
 
Old 10-15-2016, 01:11 PM   #9
TheEngineer
LQ Newbie
 
Registered: Oct 2006
Location: UK
Distribution: MacOS , Redhat
Posts: 17

Original Poster
Rep: Reputation: 0
Now, I understand where I might have made a mistake. I think I used single quotes maybe because of spaces in a directory...

Thanks for the replies and help - I've learnt something new :-)
 
Old 10-15-2016, 01:15 PM   #10
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 956
Blog Entries: 3

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
You're welcome. If you have spaces in the directory names, you can use a backslash to escape them.

Code:
ls /Dir\ 1/Dir\ 2/Dir\ 3/*/Dir\ 5/Dir\ 6/
 
1 members found this post helpful.
  


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
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 10:12 PM
How to visit a url using a shell script or unix command Keerthi Shankar Pattabhi Linux - Newbie 5 08-02-2011 12:00 PM
UNIX shell script: split long command on multiple lines loopoo Linux - Newbie 2 10-23-2006 10:34 AM
Problem with shell script using a variable from a unix command. abefroman Programming 1 05-10-2006 03:14 PM
unix shell script cxy0481 Programming 9 11-20-2005 09:15 AM


All times are GMT -5. The time now is 12:13 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