⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx04.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<HTML>

<HEAD>

<TITLE>UNIX Unleashed unx04.htm</TITLE>

<LINK REL="ToC" HREF="index.htm">

<LINK REL="Next" HREF="unx05.htm">

<LINK REL="Previous" HREF="unx03.htm"></HEAD>

<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>

<P><A HREF="unx03.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>

<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>

<A HREF="unx05.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>

<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>

</P><UL>

<LI>

<A HREF="#I1">4 &#151; Listing Files</A></LI>

<UL>

<UL>

<LI>

<A HREF="#I3">Listing Files and Directories: ls Revisited</A></LI>

<UL>

<LI>

<A HREF="#I4">ls The Short and Long of It</A></LI></UL>

<LI>

<A HREF="#I5">Other ls Options</A></LI>

<UL>

<LI>

<A HREF="#I6">Showing Hidden Files with -a</A></LI>

<LI>

<A HREF="#I7">Showing File Types with -F</A></LI>

<LI>

<A HREF="#I8">Listing Files Whose Names Contain Nonprintable Characters with -q</A></LI>

<LI>

<A HREF="#I9">Other Useful ls Options</A></LI></UL>

<LI>

<A HREF="#I10">Using Metacharacters When Referring to Filenames</A></LI>

<UL>

<LI>

<A HREF="#I11">Pattern Matching on a Single Character</A></LI>

<LI>

<A HREF="#I12">Pattern Matching on a Group of Characters</A></LI>

<LI>

<A HREF="#I13">Pattern Matching on Character Sets</A></LI></UL>

<LI>

<A HREF="#I14">How File Substitution Works</A></LI>

<LI>

<A HREF="#I15">The find Command</A></LI>

<UL>

<LI>

<A HREF="#I16">Search Criteria</A></LI>

<UL>

<LI>

<A HREF="#I17">Finding Files with a Specific Name: -name fname</A></LI>

<LI>

<A HREF="#I18">Locating Files of a Specific Size: -size n</A></LI>

<LI>

<A HREF="#I19">Locating Files of a Specific Size: -size n</A></LI>

<LI>

<A HREF="#I20">Displaying the Path Names of Found Files: -print</A></LI>

<LI>

<A HREF="#I21">Executing a UNIX Command on the Found Files: -exec cmd \;</A></LI>

<LI>

<A HREF="#I22">Executing a UNIX Command on Found Files, But Querying First: -ok cmd \;</A></LI>

<LI>

<A HREF="#I23">Writing Found Files to a Device: -cpio device</A></LI></UL>

<LI>

<A HREF="#I24">Search Qualifiers</A></LI>

<UL>

<LI>

<A HREF="#I25">Searching for Files on Only the Current File System: -mount</A></LI>

<LI>

<A HREF="#I26">Altering the Search Path with -depth</A></LI></UL>

<LI>

<A HREF="#I27">Combining Search Criteria</A></LI>

<UL>

<LI>

<A HREF="#I28">Negating Expressions to Find Files That Don't Meet Criteria</A></LI>

<LI>

<A HREF="#I29">Specifying More Than One Path to Search</A></LI></UL></UL>

<LI>

<A HREF="#I30">Controlling Input and Output</A></LI>

<UL>

<UL>

<LI>

<A HREF="#I31">Output Redirection</A></LI></UL>

<LI>

<A HREF="#I32">Input File Redirection</A></LI>

<LI>

<A HREF="#I33">Redirecting Error Messages</A></LI></UL>

<LI>

<A HREF="#I34">Using Pipes to Pass Files Between Programs</A></LI>

<LI>

<A HREF="#I35">Summary</A></LI></UL></UL></UL>



<H1 ALIGN="CENTER">

<CENTER><A ID="I1" NAME="I1">

<BR>

<FONT SIZE=5><A ID="I2" NAME="I2"></A><B>4 &#151; Listing Files</B>

<BR></FONT></A></CENTER></H1>

<P>This chapter covers some useful commands and constructs that help you better manage your data files. As a system matures, the file system becomes an eclectic collection of data files&#151;some old, some new, some borrowed, some blue. The file system 
maintains information about data files such as file ownership, the size of the file, and the access dates. All of this information is useful in helping to manage your data. You'll learn more about ls, the directory list command. In addition, you'll learn 
about the find command, which you can use to locate files even when you don't know the complete path name.

<BR></P>

<P>Sometimes you want to limit the scope of a command so that the output from the command is more focused. You accomplish this by using partial filenames and some special wildcard characters. This chapter discusses three ways of causing the system to make 

filename substitutions.

<BR></P>

<P>You'll also look at two of the most powerful features of UNIX&#151;redirection and piping&#151;which are methods for rerouting the input and output of most commands.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I3" NAME="I3">

<FONT SIZE=4><B>Listing Files and Directories: </B><B><I>ls</I></B><B> Revisited</B>

<BR></FONT></A></CENTER></H3>

<P>As you learned in Chapter 3, &quot;The UNIX File System: Go Climb a Tree,&quot; the ls command lists the names of files and directories. This section reviews the basics of ls and provides examples of its options.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I4" NAME="I4">

<FONT SIZE=3><B><I>ls</I></B><B> The Short and Long of It</B>

<BR></FONT></A></CENTER></H4>

<P>In its simplest form, the ls command without arguments displays the names of the files and directories in the current working directory in alphabetical order by name. For example,

<BR></P>

<PRE>$ <B>ls</B>

21x        LINES.dat    LINES.idx   PAGES.dat   PAGES.idx

acct.pds   marsha.pds   p11         t11         users</PRE>

<P>On some systems, the default output from ls is a single column of output. Most of the examples in this chapter use the columnar format to conserve space.

<BR></P>

<P>The ls command can also accept a filename as a command line parameter. For example,

<BR></P>

<PRE>$ ls marsha.pds

marsha.pds</PRE>

<P>If the command line parameter is a directory name, all the files in that directory are listed. For example,

<BR></P>

<PRE>$ ls users

dave     marsha     mike</PRE>

<P>Notice that the files are listed in order by collating sequence. That is, files beginning with numbers come first; files beginning with uppercase characters come next; and files beginning with lowercase characters come last. Also notice that although 
this format displays your filenames in a compact fashion, it doesn't give you much information about the files. You can get more detail about the files by requesting a long listing with the -l option. For example,

<BR></P>

<PRE>$ ls -l

-rwxr-xr&#151;   1 asm      adept       512 Dec 14 16:16 21x

-rw-rw-r&#151;   1 marsha   adept      1024 Jan 20 14:14 LINES.dat

-rw-rw-r&#151;   1 marsha   adept      3072 Jan 20 14:14 LINES.idx

-rw-rw-r&#151;   1 marsha   adept       256 Jan 20 14:14 PAGES.dat

-rw-rw-r&#151;   1 marsha   adept      3072 Jan 20 14:14 PAGES.idx

-rw-rw-r&#151;   1 marsha   acct        240 May  5  1992 acct.pds

-rw-rw-r&#151;   1 marsha   adept      1024 Nov 22 15:42 marsha.pds

-rwxrwxr&#151;   4 root     sys      243072 Aug 22  1991 p11

-rwxrwxr&#151;   4 root     sys      256041 Aug 22  1991 t11

drw-rw-r&#151;   1 marsha   adept      3072 Oct 12 11:42 users</PRE>

<P>A long listing displays seven columns of information about each file. In the first line of the listing,

<BR></P>

<TABLE BORDER>

<TR>

<TD>

<P>-rwxr-xr&#151; </P>

<TD>

<P>indicates the file's type and permissions</P>

<TR>

<TD>

<P>1</P>

<TD>

<P>indicates the number of links to the file</P>

<TR>

<TD>

<P>asm </P>

<TD>

<P>is the user ID of the file's owner</P>

<TR>

<TD>

<P>adept </P>

<TD>

<P>is the group ID of the group that the owner belongs to</P>

<TR>

<TD>

<P>512 </P>

<TD>

<P>is the size of the file in bytes</P>

<TR>

<TD>

<P>Dec 14 16:16 </P>

<TD>

<P>is the time stamp&#151;the date and time when the file was last modified</P>

<TR>

<TD>

<P>21x </P>

<TD>

<P>is the name of the file (refer to Figure 3.4 in Chapter 3)</P></TABLE>

<P>The first and second columns require a bit more explanation. The first column is a ten-character field that indicates the file's mode&#151;its type and its permissions. In the first line of the list, the file's mode is -rwxr-xr&#151;. The first 
character tells the file type, which is a hyphen (-) for regular files, and d for directories. In this example, the first nine items in the list are all ordinary files, and the last item is a directory.

<BR></P>

<P>The next nine characters of the entry are the file's permissions&#151;three sets of three characters that control which users may access a file and what they can do with it. The first set of three characters controls what the file's owner can do; the 
second set of three characters controls what others in the group can do; and the third set of three characters controls what all other users can do. Each set of three characters shows read (r), write (w), and execute (x) permission, in that order. A hyphen 

(-) means that the permission is denied.

<BR></P>

<P>The second column of the long listing is the number of links to this file. All the files except two&#151;p11 and t11&#151;are pointed to only from this directory. p11 and t11 have entries in three other directories, for a total of four links.

<BR></P>

<P>You should refer to the &quot;Keeping Secrets&#151;File and Directory Permissions&quot; section in Chapter 3 for a complete description of file types and for further details on file permissions. File links are covered in the &quot;Hard and Symbolic 
Links&quot; section of Chapter 3.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I5" NAME="I5">

<FONT SIZE=4><B>Other </B><B><I>ls </I></B><B>Options</B>

<BR></FONT></A></CENTER></H3>

<P>The ls command has several options. This section covers many of the ones more frequently used.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I6" NAME="I6">

<FONT SIZE=3><B>Showing Hidden Files with </B><B><I>-a</I></B>

<BR></FONT></A></CENTER></H4>

<P>The ls option doesn't normally list files that begin with a period. Suppose that the directory displayed in the previous section also contained a file named .profile. In that case, you would see

<BR></P>

<PRE>$ ls -a

.           ..          .profile    21x          LINES.dat

LINES.idx   PAGES.dat   PAGES.idx   acct.pds    marsha.pds

p11         t11         users</PRE>

<P>Note that the files . and .. represent the current and parent directories, respectively.

<BR></P>

<P>You can combine options, as in this example:

<BR></P>

<PRE>$ ls -al

-rw-r&#151;r&#151;   1 marsha   adept      2156 Jul 19 1991  .

-rw-r&#151;r&#151;   1 marsha   adept      2246 Jul 19 1991  ..

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -