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.
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.
There are two dir/ls functions which I can't get to work the
way I want :
First is a listing of just the sub-directories in the current directory.
The description of "-d" would seem to be what I want but
all this does is to list the dot dir "." and " dir -d * " lists
all files.
Second is a recursive search of all sub-directories beneath the current location for a filename.
Using "-R" lists all files in all sub-directories but doesn't
seem to accept a specific filename.
I've been using kfind but I prefer the keyboard.
ls */ -d will list just the subdirectories and not the files. To list the hidden directories use:
ls .*/ -d.
ls */ .*/ -d will list both.
You might want to look at the output of: echo */ and echo .*/ to see what is actually happening. It will explain why ls -a */ doesn't list hidden directories.
ls */ -d will list just the subdirectories and not the files. To list the hidden directories use:
ls .*/ -d.
ls */ .*/ -d will list both.
You might want to look at the output of: echo */ and echo .*/ to see what is actually happening. It will explain why ls -a */ doesn't list hidden directories.
There's an even easier way to accomplish that.
ls -ad *
The idea with the '-a' is that it will list all file, including hidden ones (.files). Of course, either of the methods will work. More than not, there is usually many ways to accomplish the same thing in Linux.
To original poster:
reference the manual page for ls ('man ls') for an even larger listing of options and flags for the ls command.
The idea with the '-a' is that it will list all file, including hidden ones (.files). Of course, either of the methods will work. More than not, there is usually many ways to accomplish the same thing in Linux.
To original poster:
reference the manual page for ls ('man ls') for an even larger listing of options and flags for the ls command.
No, ls -ad * will also list files, and the original posting asked about directories. The -d option will cause ls to not list the contents of the directories and instead list the directories themselves.
ls -ad * also will not list hidden files or directories. Try "ls -ad * | grep kde" vs "ls -ad .*". Repeat my experiment without the trailing slash to see why. ( echo * and echo .* )
Thanks to both of you. That info is exactly what I'm
looking for.
To jschiwal :
The use of echo is intriguing. The man pages don't show the
*/ param.
Thanks again.
Austin
The slash at the end of a filename explitely refers to a directory.
For example, "mv ab de" will rename a file to "de" unless de is a directory that exists. However the command "mv ab de/" is unambiguous. If the directory "de" doesn't exist, the command will fail instead of doing the wrong thing.
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
Jschiwal, you are hiding the truth.
Mcamember, if you looked at 'man echo', you didn't guess. Unlike DOS, where each command had to expand wildcards on its own, shells used in GNU/Linux (or *BSD, or QNX) are expanding wildcards - so the right manual page to read is man bash. And then echo just gets as parameters a list of file names. Surely, it successfully outputs it. Caveat: if you have really lots of files in one dir, shell expansions will stop working, as shell allocates only limited amount of memory for arguments for each command run. But you can still use commands I suggested (and pipe them to xargs to, say, remove files; to move some files you will have to compose a group - read in man bash about {} and ()).
By the way,
ls -d "*/"
works better than
ls -d */
when you have directory name with space in it.
No, ls -d "*d" will try to list a file by the literal name '*d'.
If you use find instead then if the filename pattern contains any wildcards, you need to put them into double quotes to prevent the shell from expanding it itself. Single quotes would cause find to search for a file with a literal name of '*d'.
Quote:
shells used in GNU/Linux (or *BSD, or QNX) are expanding wildcards
And, yes, The use of: echo */ shows that the shell is expanding the wildcard and filling the filenames into the argument list. That's what my experiment points out.
You type in the command: ls -d .*/
and the command ls will run with an argument list like
./kde/ ./gnome/ ./ssh/
So it is the same as if you had typed in: ls -d ./kde ./gnome ./ssh
The ls command itself will print the same results regardless. Using 'echo .*/' you can see that the arguments will be.
About listing a file in a subdirectory tree, it is better to use the find command for that.
This might work:
Code:
$ ls */*/*/*/*/*/*/README
downloads/ndiswrapper/usr/share/doc/packages/ndiswrapper/README
if you know how the depth of the search but that is useless practically.
You could do: ls * -R | grep '/README$'
To list all the files in all the subdirectories but filter out all but README files. This will be very inefficient.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.