LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-21-2006, 06:04 PM   #1
mcamember
LQ Newbie
 
Registered: Dec 2006
Location: Lowell, Mass. U.S.A.
Distribution: xandros
Posts: 14

Rep: Reputation: 0
DIR parameter


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.

Thanks in advance.
 
Old 12-21-2006, 06:20 PM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Maybe you are trying to make these utilities do find's job?

find . -type d -maxdepth 1
find . -name file
 
Old 12-21-2006, 08:13 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 12-21-2006 at 08:19 PM.
 
Old 12-21-2006, 09:59 PM   #4
mcamember
LQ Newbie
 
Registered: Dec 2006
Location: Lowell, Mass. U.S.A.
Distribution: xandros
Posts: 14

Original Poster
Rep: Reputation: 0
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
 
Old 12-21-2006, 10:19 PM   #5
PingFloyd
Member
 
Registered: Jun 2006
Posts: 94

Rep: Reputation: 16
Quote:
Originally Posted by jschiwal
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.
 
Old 12-21-2006, 11:38 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by PingFloyd
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.
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 .* )

Last edited by jschiwal; 12-21-2006 at 11:43 PM.
 
Old 12-21-2006, 11:47 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by mcamember
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.
 
Old 12-22-2006, 12:48 AM   #8
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
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.
 
Old 12-22-2006, 11:49 PM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 12-22-2006 at 11:54 PM.
 
  


Reply



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
Compress dir keep file/dir permissions powadha Linux - General 1 11-14-2006 07:07 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
Q. moving /bin dir files to /dev dir?? Texas_student Linux - Software 2 03-26-2006 11:42 PM
Share configuratin dir (.dir) for 3 accounts on local computer LiNuXkOlOnIe Linux - Software 5 01-08-2006 03:36 AM
howto make a dir shared that is not in my home dir Schmurff Linux - Newbie 2 06-19-2004 07:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:58 AM.

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
Open Source Consulting | Domain Registration