Unix administration - Unix: List only File names and not directory

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > May 2004 > Unix: List only File names and not directory





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Unix: List only File names and not directory
Srinath M.K

2004-05-14, 12:49 pm

Hi all,

I am working on Unix (Solaris). I want to create a list file from the
files in my directory.

Say my directory /home/sri contains the folloiwng files & folders:
sria --> Folder
srib --> Folder
sri_f1.dat --> File
sri_f2.dat --> File
sriv3.dat --> File
am.dat --> File
blah.dat --> File

Now I want to create a new file which contains the file (not
direcotry) names starting from sri. That is, I want a list file called
sri.lst which contains below data:

sri.lst
========
sri_f1.dat
sri_f2.dat
sriv3.dat


I tried with the following command (ls -one) but this was not useful
since it also lists the directories starting from sri

ls -1 sri*


Now how can i specify Unix to consider only files and not
directories/files in subdirectories?

Any help much appriciated.

Thanks and Regards,
Srinath M. K
Tristram Scott

2004-05-14, 12:49 pm

"Srinath M.K" wrote:
>
> Hi all,
>
> I am working on Unix (Solaris). I want to create a list file from the
> files in my directory.
>
> Say my directory /home/sri contains the folloiwng files & folders:
> sria --> Folder
> srib --> Folder
> sri_f1.dat --> File
> sri_f2.dat --> File
> sriv3.dat --> File
> am.dat --> File
> blah.dat --> File
>
> Now I want to create a new file which contains the file (not
> direcotry) names starting from sri. That is, I want a list file called
> sri.lst which contains below data:
>
> sri.lst
> ========
> sri_f1.dat
> sri_f2.dat
> sriv3.dat
>
> I tried with the following command (ls -one) but this was not useful
> since it also lists the directories starting from sri
>
> ls -1 sri*
>
> Now how can i specify Unix to consider only files and not
> directories/files in subdirectories?
>
> Any help much appriciated.
>
> Thanks and Regards,
> Srinath M. K


There are doubtless many ways, but the following works:

ls -p | grep -v /

--
Dr Tristram J. Scott
Energy Consultant
Marcin Dobrucki

2004-05-14, 12:49 pm

Srinath M.K wrote:

> Now how can i specify Unix to consider only files and not
> directories/files in subdirectories?
>
> Any help much appriciated.


$ find . -type f

/Marcin
Chris F.A. Johnson

2004-05-14, 3:34 pm

On 2004-05-14, Srinath M.K wrote:
> Hi all,
>
> I am working on Unix (Solaris). I want to create a list file from the
> files in my directory.
>
> Say my directory /home/sri contains the folloiwng files & folders:
> sria --> Folder
> srib --> Folder
> sri_f1.dat --> File
> sri_f2.dat --> File
> sriv3.dat --> File
> am.dat --> File
> blah.dat --> File
>
> Now I want to create a new file which contains the file (not
> direcotry) names starting from sri. That is, I want a list file called
> sri.lst which contains below data:
>
> sri.lst
>========
> sri_f1.dat
> sri_f2.dat
> sriv3.dat
>
>
> I tried with the following command (ls -one) but this was not useful
> since it also lists the directories starting from sri
>
> ls -1 sri*
>
>
> Now how can i specify Unix to consider only files and not
> directories/files in subdirectories?


In the specific case you describe:

ls sri*.dat

More generally (e.g., if there are files that do not end in .dat):

ls -dF sri* | grep -v '/$'

--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
those who know me have no need of my name

2004-05-14, 4:34 pm

[fu-t set]

in comp.unix.admin i read:

> More generally (e.g., if there are files that do not end in .dat):
>
>ls -dF sri* | grep -v '/$'


annoyingly most ls' don't append a space after regular files, so any file
whose name ends with a slash would be mistaken for a directory. perhaps
that's more a lesson in sensible file naming. and indeed the likelihood
of such filenames is low enough that it might even be worth ignoring.

--
a signature
Alan Coopersmith

2004-05-14, 10:34 pm

those who know me have no need of my name <not-a-real-address@usa.net> writes in comp.sys.sun.admin:
|annoyingly most ls' don't append a space after regular files, so any file
|whose name ends with a slash would be mistaken for a directory. perhaps
|that's more a lesson in sensible file naming. and indeed the likelihood
|of such filenames is low enough that it might even be worth ignoring.

On a true Unix system, the likelihood of such names is exactly zero,
as they are forbidden. Perhaps if you were using CygWin or something
similar there's an issue, but thats not what the poster seeme to be
asking about.

--
________________________________________
________________________________
Alan Coopersmith alanc@alum.calberkeley.org
http://www.CSUA.Berkeley.EDU/~alanc/ aka: Alan.Coopersmith@Sun.COM
Working for, but definitely not speaking for, Sun Microsystems, Inc.
Adam Price

2004-05-15, 1:34 am

On Sat, 15 May 2004 01:52:45 +0000 (UTC), Alan Coopersmith wrote:

> those who know me have no need of my name <not-a-real-address@usa.net> writes in comp.sys.sun.admin:
>|annoyingly most ls' don't append a space after regular files, so any file
>|whose name ends with a slash would be mistaken for a directory. perhaps
>|that's more a lesson in sensible file naming. and indeed the likelihood
>|of such filenames is low enough that it might even be worth ignoring.
>
> On a true Unix system, the likelihood of such names is exactly zero,
> as they are forbidden. Perhaps if you were using CygWin or something
> similar there's an issue, but thats not what the poster seeme to be
> asking about.


I think that M$ filesystems enforce a similar rule about '/' in a filename.
In fact I think it is only on versions of MacOS that this could be a
problem.
Don't know about modern versions but I have a vague recall of one of those
interminable 'how do I remove...' threads from a long time ago where
someone mentioned a buggy network file protocol driver and an apple-mac
managing to create a file with '/' in its name, and then being unable to
remove it.
Adam
PS Followups-set, please don't x-post this widely
Barry Margolin

2004-05-15, 2:34 am

In article <1oe83iefylvnf.dlg@pappnase.co.uk>,
Adam Price <adam+usenet@pappnase.co.uk> wrote:

> Don't know about modern versions but I have a vague recall of one of those
> interminable 'how do I remove...' threads from a long time ago where
> someone mentioned a buggy network file protocol driver and an apple-mac
> managing to create a file with '/' in its name, and then being unable to
> remove it.
> Adam
> PS Followups-set, please don't x-post this widely


I remember that. The SunOS 4.x NFS server didn't enforce the '/'
restriction (the NFS protocol doesn't send full pathnames, so the NFS
server never needs to parse directory delimiters), and it could get away
with this because it was in the kernel. There was an Appleshare<->NFS
gateway, and it was pretty common to have '/' in Mac file names (for
example, people liked to put dates in file names).

The problem was fixed in the NFS server over a decade ago. And I think
the gateway was also updated to translate '/' to ':' (the latter is the
MacOS directory delimiter, so it can't appear in filenames coming from
the client).

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
rakesh sharma

2004-05-15, 2:34 am

mksrinath@indiatimes.com (Srinath M.K) wrote in message news:

>
> I am working on Unix (Solaris). I want to create a list file from the
> files in my directory.
>
> Say my directory /home/sri contains the folloiwng files & folders:
> sria --> Folder
> srib --> Folder
> sri_f1.dat --> File
> sri_f2.dat --> File
> sriv3.dat --> File
> am.dat --> File
> blah.dat --> File
>
> Now I want to create a new file which contains the file (not
> direcotry) names starting from sri. That is, I want a list file called
> sri.lst which contains below data:
>
> sri.lst
> ========
> sri_f1.dat
> sri_f2.dat
> sriv3.dat
>
>
> I tried with the following command (ls -one) but this was not useful
> since it also lists the directories starting from sri
>
> ls -1 sri*
>
>
> Now how can i specify Unix to consider only files and not
> directories/files in subdirectories?
>


There are many roundabout ways (no option in 'ls' will do it) to do this:

a)
/bin/ls -1dp | grep -v '/$' | grep '^sri' > /tmp/sri.list && \
mv -f /tmp/sri.list .

Note: filenames containing newlines (\n) will not handled properly.

b)
touch /tmp/sri.list && \
for _file in sri*;do
[ -f "${_file}" ] || continue
printf '%s\n' "${_file}" >> /tmp/sri.list
done
mv -f /tmp/sri.list .

Note: when a large number of files/dirs/links starting with sri are
present in your current dir., then the for loop might overflow.

c)
/bin/find . -type d ! -name . -prune -o -type f -name 'sri*' \
-print > /tmp/sri.list
mv -f /tmp/sri.list .
Frank da Cruz

2004-05-15, 2:34 pm

On 2004-05-15, rakesh sharma <sharma__r@hotmail.com> wrote:
: mksrinath@indiatimes.com (Srinath M.K) wrote in message news:
:> I am working on Unix (Solaris). I want to create a list file from the
:> files in my directory.
:>
:> Say my directory /home/sri contains the folloiwng files & folders:
:> sria --> Folder
:> srib --> Folder
:> sri_f1.dat --> File
:> sri_f2.dat --> File
:> sriv3.dat --> File
:> am.dat --> File
:> blah.dat --> File
:>
:> Now I want to create a new file which contains the file (not
:> direcotry) names starting from sri. That is, I want a list file called
:> sri.lst which contains below data:
:>
:> sri.lst
:> ========
:> sri_f1.dat
:> sri_f2.dat
:> sriv3.dat
:>
:> I tried with the following command (ls -one) but this was not useful
:> since it also lists the directories starting from sri
:>
:> ls -1 sri*
:>
:> Now how can i specify Unix to consider only files and not
:> directories/files in subdirectories?
:
: There are many roundabout ways (no option in 'ls' will do it) to do this:
:
Also a straightforward way if you have C-Kermit installed:

http://www.columbia.edu/kermit/ckermit.html

At the C-Kermit prompt, type:

directory /files /brief /output:sri.lst sri*

where:

/FILES = List only regular files
/BRIEF = List the name only
/OUTPUT:sri.lst = Put the listing in given file

You can do tons of fancy stuff with Kermit's DIRECTORY command. To give
you an idea:

C-Kermit>dir ? Enter or Return to confirm the command, or
file specification, or switch, one of the following:
/after: /dotfiles /message: /nosort /smaller-than:
/all /englishdate /nobackupfiles /not-after: /sort:
/array: /except: /nodotfiles /not-before: /summary
/ascending /files /nofollowlinks /noxfermode /type:
/backup /heading /noheading /output: /xfermode
/before: /isodate /nomessage /page /verbose
/brief /larger-than: /nopage /recursive
/directories /followlinks /norecursive /reverse
C-Kermit>

Type "help directory" at the C-Kermit> prompt for details. Prebuilt C-Kermit
binaries for Solaris can be found here:

http://www.columbia.edu/kermit/ck80binaries.html#sun

- Frank
phn@icke-reklam.ipsec.nu

2004-05-15, 4:35 pm

In comp.unix.admin Adam Price <adam+usenet@pappnase.co.uk> wrote:
> On Sat, 15 May 2004 01:52:45 +0000 (UTC), Alan Coopersmith wrote:


[vbcol=seagreen]
> I think that M$ filesystems enforce a similar rule about '/' in a filename.
> In fact I think it is only on versions of MacOS that this could be a
> problem.
> Don't know about modern versions but I have a vague recall of one of those
> interminable 'how do I remove...' threads from a long time ago where
> someone mentioned a buggy network file protocol driver and an apple-mac
> managing to create a file with '/' in its name, and then being unable to
> remove it.


I did this! Using a "gatorbox" which is a nfs-interface for apple boxes.

One of the mac's created a file containing a '/' and hp-ux ( as nfs-server)
happily put it in a filename. Think it was hp-ux 9 something.

As i remember we had to newfs + restore backup to clear this mess.


> Adam
> PS Followups-set, please don't x-post this widely


--
Peter Håkanson
IPSec Sverige ( At Gothenburg Riverside )
Sorry about my e-mail address, but i'm trying to keep spam out,
remove "icke-reklam" if you feel for mailing me. Thanx.
Mark Rafn

2004-05-16, 2:33 am

>Srinath M.K wrote:

Marcin Dobrucki <Marcin.Dobrucki@TAKETHISAWAY.nokia.com> wrote:[vbcol=seagreen]
> $ find . -type f


Add -maxdepth 1 if you want to avoid subdirectories.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
jpd

2004-05-16, 4:34 am

["Followup-To:" header set to comp.unix.admin.]
On 2004-05-16, Mark Rafn <dagon@dagon.net> wrote:
> Marcin Dobrucki <Marcin.Dobrucki@TAKETHISAWAY.nokia.com> wrote:
>
> Add -maxdepth 1 if you want to avoid subdirectories.


Except that it's a gn00 extention. Didn't OP specify `solaris'?
-prune would then be more useful, it seems.


--
j p d (at) d s b (dot) t u d e l f t (dot) n l .
Robert E A Harvey

2004-05-16, 1:34 pm

mksrinath@indiatimes.com (Srinath M.K) wrote in message news:<a2fb966f.0405140210.53e28fd4@posting.google.com>...
> Hi all,
>
> I am working on Unix (Solaris). I want to create a list file from the
> files in my directory.


Well, the classic form is:
ls -l | grep -v '^d'
which will return a long directory listing omitting the directories.

but you want just the file names, so you need to remove the other
columns.
ls -l | grep -v '^d' | awk '{print $9}'
will do just fine. You can redirect that into a file if required.
if you want to exclude links, just add another filter.
ls -l | grep -v '^d' | grep -v '^l' | awk '{print $9}'

The whole thing can be put in your .cshrc file as an alias:
alias lf "ls -l | grep -v '^d' | grep -v '^l' | awk '{print $9}'"

It gets a bit more difficult if you want to pass a parameter back to
the ls part of the string, such as a partially qualified file name -
but it can be done, see the man pages for your shell for details.

Bob Harvey
Cameron Simpson

2004-05-16, 2:34 pm

On 03:10 14 May 2004, Srinath M.K <mksrinath@indiatimes.com> wrote:
| I am working on Unix (Solaris). I want to create a list file from the
| files in my directory.
|
| Say my directory /home/sri contains the folloiwng files & folders:
| sria --> Folder
| srib --> Folder
| sri_f1.dat --> File
| sri_f2.dat --> File
| sriv3.dat --> File
| am.dat --> File
| blah.dat --> File
|
| Now I want to create a new file which contains the file (not
| direcotry) names starting from sri. That is, I want a list file called
| sri.lst which contains below data:
|
| sri.lst
| ========
| sri_f1.dat
| sri_f2.dat
| sriv3.dat
|
|
| I tried with the following command (ls -one) but this was not useful
| since it also lists the directories starting from sri
|
| ls -1 sri*

I'm rather fold of this:

ls -ld sri* | sed -n 's/^-.* //'

BTW, you only need -1 when you want one column sent to a terminal.
When ls is writing to a pipe or file it sends one column anyway.
(And if you think you have a counter example, make sure you haven't got an
alias getting in the way first.)

Cheers,
--
Cameron Simpson <cs@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

My initial work-around is to rebuild history.
- gary@sci34hub.sci.com (Gary Heston)
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com