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 |
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.
|
 |
10-15-2016, 11:53 AM
|
#1
|
LQ Newbie
Registered: Oct 2006
Location: UK
Distribution: MacOS , Redhat
Posts: 17
Rep:
|
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.
|
|
|
10-15-2016, 12:22 PM
|
#2
|
Senior Member
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
|
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.
|
|
|
10-15-2016, 12:24 PM
|
#3
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,679
|
Quote:
Originally Posted by TheEngineer
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.
|
10-15-2016, 12:39 PM
|
#4
|
LQ Newbie
Registered: Oct 2006
Location: UK
Distribution: MacOS , Redhat
Posts: 17
Original Poster
Rep:
|
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.
|
|
|
10-15-2016, 12:44 PM
|
#5
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,467
|
Quote:
Originally Posted by TheEngineer
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.
|
10-15-2016, 12:45 PM
|
#6
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,679
|
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/*/
|
|
|
10-15-2016, 12:57 PM
|
#7
|
LQ Newbie
Registered: Oct 2006
Location: UK
Distribution: MacOS , Redhat
Posts: 17
Original Poster
Rep:
|
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.
|
|
|
10-15-2016, 01:02 PM
|
#8
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,679
|
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/'
|
|
|
10-15-2016, 01:11 PM
|
#9
|
LQ Newbie
Registered: Oct 2006
Location: UK
Distribution: MacOS , Redhat
Posts: 17
Original Poster
Rep:
|
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 :-)
|
|
|
10-15-2016, 01:15 PM
|
#10
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,679
|
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.
|
All times are GMT -5. The time now is 08:11 PM.
|
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
|
|