old-www/HOWTO/Bash-Prompt-HOWTO/x700.html

192 lines
3.0 KiB
HTML

<HTML
><HEAD
><TITLE
>Counting Files in the Current Directory</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Bash Prompt HOWTO"
HREF="index.html"><LINK
REL="UP"
TITLE="Prompt Code Snippets"
HREF="c679.html"><LINK
REL="PREVIOUS"
TITLE="Date and Time"
HREF="x690.html"><LINK
REL="NEXT"
TITLE="Total Bytes in the Current Directory"
HREF="x711.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Bash Prompt HOWTO: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x690.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 11. Prompt Code Snippets</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x711.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN700"
></A
>11.3. Counting Files in the Current Directory</H1
><P
>To determine how many files there are in the current directory, put in
<TT
CLASS="USERINPUT"
><B
>ls -1 | wc -l</B
></TT
>. This uses <TT
CLASS="USERINPUT"
><B
>wc</B
></TT
>
to do a count of the number of lines (-l) in the output of
<TT
CLASS="USERINPUT"
><B
>ls -1</B
></TT
>. It doesn't count dotfiles. Please note that
<TT
CLASS="USERINPUT"
><B
>ls -l</B
></TT
> (that's an "L" rather than a "1" as in the
previous examples) which I used in previous versions of this HOWTO will
actually give you a file count one greater than the actual count. Thanks
to Kam Nejad for this point.&#13;</P
><P
>If you want to count only files and NOT include symbolic links (just an
example of what else you could do), you could use <TT
CLASS="USERINPUT"
><B
>ls -l | grep
-v ^l | wc -l</B
></TT
> (that's an "L" not a "1" this time, we want a
"long" listing here). <TT
CLASS="USERINPUT"
><B
>grep</B
></TT
> checks for any line
beginning with "l" (indicating a link), and discards that line (-v).&#13;</P
><P
>Relative speed: "ls -1 /usr/bin/ | wc -l" takes about 1.03 seconds on an
unloaded 486SX25 (/usr/bin/ on this machine has 355 files). "ls -l
/usr/bin/ | grep -v ^l | wc -l" takes about 1.19 seconds.&#13;</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x690.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x711.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Date and Time</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c679.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Total Bytes in the Current Directory</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>