Tech Support > Operating Systems > Zenix > Unix: List only File names and not directory
Unix: List only File names and not directory
Posted by Srinath M.K on May 14th, 2004


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

Posted by Tristram Scott on May 14th, 2004


"Srinath M.K" wrote:
There are doubtless many ways, but the following works:

ls -p | grep -v /

--
Dr Tristram J. Scott
Energy Consultant

Posted by Marcin Dobrucki on May 14th, 2004


Srinath M.K wrote:

$ find . -type f

/Marcin

Posted by UNIX admin on May 14th, 2004


`find ./ -type f -print | tee /tmp/sri.lst`

Optionally, you can do:

`find ./ -type f -print | awk -F'/' '{print $NF}' | tee /tmp/sri.lst`



Posted by Chris F.A. Johnson on May 14th, 2004


On 2004-05-14, Srinath M.K wrote:
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

Posted by those who know me have no need of my name on May 14th, 2004


[fu-t set]

in comp.unix.admin i read:

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

Posted by rakesh sharma on May 15th, 2004


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

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 .

Posted by Frank da Cruz on May 15th, 2004


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

Posted by Mark Rafn on May 16th, 2004


Marcin Dobrucki <Marcin.Dobrucki@TAKETHISAWAY.nokia.com> wrote:
Add -maxdepth 1 if you want to avoid subdirectories.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>

Posted by Cameron Simpson on May 16th, 2004


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)

Posted by jpd on May 16th, 2004


["Followup-To:" header set to comp.unix.admin.]
On 2004-05-16, Mark Rafn <dagon@dagon.net> wrote:
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 .

Posted by Robert E A Harvey on May 16th, 2004


mksrinath@indiatimes.com (Srinath M.K) wrote in message news:<a2fb966f.0405140210.53e28fd4@posting.google. com>...
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


Similar Posts