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!
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.
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.
Free Guide: Linux from Scratch
Linux from Scratch describes the process of creating your own Linux system from scratch from an already installed Linux distribution, using nothing but the source code of software that you need.
This 318 page eBook provides readers with the background and instruction to design and build custom Linux systems. The resulting system will be compiled completely from the source code, and the user will be able to specify where, why, and how programs are installed. This eBook allows readers to fully customize Linux systems to their own needs and allows users more control over their system.
Click Here to receive the Linux from Scratch Guide absolutely free.
I know why it doesn't work, but am very short on ideas how to make it work. Bash (and other shells maybe too) will convert the * wildcard to the full dir listing of the current directory. (In most cases that is) ..
Ofcourse you can do ls */*/*/*.log etc, but then you have to know the deepness your looking for.. I mostly just use find for jobs like that.. find ./ -name "*.cpp"
Hi folks, totally basic and trivial question here.
1) How do I use ls to in conjunction with wildcards to list a particular file in the entire subdirectory tree? (not just the immediate subdirs)
For example, I'm looking for all files beginining with f in all subdirectories (not just the immediate ones)
My memory is maybe playing tricks but I'm sure
ls -r f* would have worked in my Sun and HP days
You are almost right. I think perhaps your memory is failing you! - Depending upon the HP system you were on, certainly Tru64, doesn't even have a recursive mode in ls!
You want this:
ls -R |grep f
although grepping for f is a bit useless, i'd grep some more, eg:
ls -R |grep -i file*
Quote:
Originally Posted by robgee1964
2) I've got a simular problem with grep, it doesn't seem to like working with recursive subdirectories either
For example I want to find the instances of "hopper.h" in all files ending .C
I thought the following should work :-
grep -r hopper.h *.c
But again, it doesn't seem to like my use of wildcard, and I get the message
grep: *.c: No such file or directory
Regards
Rob
As you want a literal search of 'hopper.h', you need to encapsulate it in quotes, eg:
grep -r "hopper.h" *.c
that's all!
PS: Just in case you were wondering, I don't know why the gnu people decicded that Recursive in ls is -R, but -r in grep, but that's the way it is!
Having been a professional Unix System Admin for both Solaris and HP-UX among dozens of variants I can tell you the ls syntax wouldn't have been any better on Unix than Linux.
Also recursive is upper case R not lower case (ls -R).
You could do "ls -R |grep ^f" it would give you all files that started with f but not the paths they were in. Doing "ls -R |egrep ":|^f" would give you all directory entries then all file names that started with f but they wouldn't be on the same line.
Therefore the BEST way to do it is find as mentioned by another poster.
grep matches on a pattern that may or may not be embedded in longer strings so wild cards are a little meaningless to it though I've seen some versions that allow for it I've never seen them on Unix.
Having been a professional Unix System Admin for both Solaris and HP-UX among dozens of variants I can tell you the ls syntax wouldn't have been any better on Unix than Linux.
Also recursive is upper case R not lower case (ls -R).
You could do "ls -R |grep ^f" it would give you all files that started with f but not the paths they were in. Doing "ls -R |egrep ":|^f" would give you all directory entries then all file names that started with f but they wouldn't be on the same line.
Therefore the BEST way to do it is find as mentioned by another poster.
grep matches on a pattern that may or may not be embedded in longer strings so wild cards are a little meaningless to it though I've seen some versions that allow for it I've never seen them on Unix.
Thanks for all your replies, I'll use find for the recursive directory thing.
As for grep, what I was meaning is how would I use grep to search recursively? Using the -r switch I thought? Incidentally I've got the wild card in the search string, not the filespec.
Putting the search string in quotes doesn't seem to help, I still can't get it to search recursively.
eg grep -r "hopper" *.c just comes up with
grep: *.c: no such file or directory.
You can also use xargs, search here on LQ if you are interested.
EDIT: I forgot to mention that you could use grep with an -l switch. If you do this you will get all files listed which contain the search string, but only the filename.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.