Linux - NewbieThis 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!
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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
You already answered your own question. Use find command but modify it a little bit.
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.
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
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.
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.
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/*/