[Cialug] List file types in a directory

Tim Wilson tim_linux at wilson-home.com
Thu Mar 8 16:55:16 CST 2007


You can also do it this way, but I'd be a little more wary of this:

(for i in `ls -1`; do foo=${i%.*}; echo ${i#$foo}; done) | sort -u

Essentially, it takes $i and removes everything after (and including)
the last period, and puts that in the foo variable.  Then, it strips
foo from i, leaving you with just ".ext".  And this appears to work
with files with multiple periods, and not include files without
periods.

On 3/8/07, Daniel A. Ramaley <daniel.ramaley at drake.edu> wrote:
> Like this?
>
> $ ls | perl -ne 'print "$1\n" if /\.([^.]*?)$/;'
>
> That prints the file extension for files that have one. For files that
> don't have an extension, nothing is printed.
>
> On Thursday 08 March 2007 16:39, Kendall Bailey wrote:
> >Not simple enough for a single command line, but here's a python
> >script which will play the role of awk in the pipeline shown
> >
> >import sys,re
> >for i in sys.stdin :
> >   if re.match(".*\.([^.])+",i.strip()) : print
> > i.strip().split(".")[-1]
> >
> >I'm sure it would be a one liner in perl...
> >
> >On 3/8/07, David Champion <dchampion at visionary.com> wrote:
> >> Both of those methods will display files with no periods, i.e. put a
> >> file in there called "test" and it will echo out "test".
> >>
> >> -dc
> >>
> >> Tim Wilson wrote:
> >> > ls -1 | awk -F'.' '{print $NF}' | sort | uniq
> >> > or sort -u instead of sort | uniq
> >> >
> >> > I use awk because you could have foo.bar.jpg.  Using NF in awk
> >> > will guarantee you'll get jpg.
> >> >
> >> > On 3/8/07, Chris Freeman <cwfreeman at gmail.com> wrote:
> >> >> Assuming only one '.' per filename:
> >> >>
> >> >> ls -1 | cut -f2 -d. | sort -u
> >> >>
> >> >> Chris
> >> >>
> >> >> On 3/8/07, Daniel Juliano < dan at danandlaurajuliano.com> wrote:
> >> >> > So, how would I get a list of file types for all files in a
> >> >> > directory? Let's say I've got the following:
> >> >> > test1.txt
> >> >> > test2.txt
> >> >> > test3.jpg
> >> >> >
> >> >> > I'd like something similar to:
> >> >> > .txt
> >> >> > .jpg
> >> >> >
> >> >> > Any ideas?
> >> >> >
> >> >> >
> >> >> > _______________________________________________
> >> >> > Cialug mailing list
> >> >> > Cialug at cialug.org
> >> >> > http://cialug.org/mailman/listinfo/cialug
> >> >>
> >> >> _______________________________________________
> >> >> Cialug mailing list
> >> >> Cialug at cialug.org
> >> >> http://cialug.org/mailman/listinfo/cialug
> >>
> >> _______________________________________________
> >> Cialug mailing list
> >> Cialug at cialug.org
> >> http://cialug.org/mailman/listinfo/cialug
> >
> >_______________________________________________
> >Cialug mailing list
> >Cialug at cialug.org
> >http://cialug.org/mailman/listinfo/cialug
>
> --
> ------------------------------------------------------------------------
> Dan Ramaley                            Dial Center 118, Drake University
> Network Programmer/Analyst             2407 Carpenter Ave
> +1 515 271-4540                        Des Moines IA 50311 USA
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
>


-- 
Tim


More information about the Cialug mailing list