📄 0072-0074.html
字号:
<HTML>
<HEAD>
<TITLE>Sams Teach Yourself Linux in 24 Hours:Manipulation and Searching Commands:EarthWeb Inc.-</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311623 //-->
<!-- TITLE=Sams Teach Yourself Linux in 24 Hours//-->
<!-- AUTHOR=Bill Ball//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=05 //-->
<!-- PAGES=0063-0082 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0069-0071.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0075-0077.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-72"><P>Page 72</P></A>
<!-- CODE SNIP //-->
<PRE>
# ls -l file*
-rw-rw-r-- 1 bball bball 14 Nov 13 18:54 file1
lrwxrwxrwx 1 bball bball 5 Nov 13 19:04 file2 -> file1
</PRE>
<!-- END CODE SNIP //-->
<P>Note the arrow pointing from file2 to file2. This tells you that
file2 is a symbolic link to file1. Also note that
file2 is shorter than file1. Symbolic links are different from hard links in
that a symbolic link is merely a pathname, or alias, to the original file. Nothing happens to
the original file if you delete the symbolic link. However, if you delete the original file,
your symbolic link won't help you at all:
</P>
<!-- CODE SNIP //-->
<PRE>
# rm -f file1
# cat file2
cat: file2: No such file or directory
</PRE>
<!-- END CODE SNIP //-->
<P>Because the original file, file1, no longer exists, you can't access its contents through
the symbolic link, file2. However, symbolic links do have an advantage over hard links. You
can use a symbolic link to point to a directory on your file system. In the following
example, if you try to create a hard link to the
/usr/local/games directory, the ln command will
complain and quit:
</P>
<!-- CODE SNIP //-->
<PRE>
# ln /usr/local/games play
ln: /usr/local/games: hard link not allowed for directory
</PRE>
<!-- END CODE SNIP //-->
<P>But you can use a symbolic link with
</P>
<!-- CODE SNIP //-->
<PRE>
# ln -s /usr/local/games play
# ls -l play
lrwxrwxrwx 1 bball bball 16 Nov 13 19:28 play -> /usr/local/games
</PRE>
<!-- END CODE SNIP //-->
<P>Now, instead of typing a long command like
</P>
<!-- CODE SNIP //-->
<PRE>
# cd /usr/local/games
</PRE>
<!-- END CODE SNIP //-->
<P>you can use
</P>
<!-- CODE SNIP //-->
<PRE>
# cd play
</PRE>
<!-- END CODE SNIP //-->
<P>So far, you've learned about using the command line. If you're familiar with using
more graphical interfaces to manipulate files, you'll like the next program, the
mc command.
</P>
<H4><A NAME="ch05_ 12">
Handling Files with the Midnight Commander Program
</A></H4>
<P>The mc command, called Midnight Commander, is a graphical interface for
handling files (see Figure 5.1). It is a visual shell (you'll learn more about shells in the next hour). To
start mc, type the following at the command line:
</P>
<!-- CODE SNIP //-->
<PRE>
# mc
</PRE>
<!-- END CODE SNIP //-->
<P>This section does not cover all of the details of the
mc command. But here are the highlights of its features:
</P>
<UL>
<LI> Provides visual interface to two directories at a time, and directory
browsing
</UL>
<A NAME="PAGENUM-73"><P>Page 73</P></A>
<UL>
<DL>with mouse clicks</DL>
<LI> Allows menu-driven file operations with dialogs and mouse or keyboard
(and function key) support
<LI> Has an open command line to your shell
<LI> Runs commands through mouse clicks
<LI> Extensive, built-in hypertext help screens
<LI> Emulates and supports the ls, cp,
ln, mv, mkdir, rmdir, rm, cd, pwd, find, chown,
chgrp, and tree commands
</UL>
<P>Figure 5.1.<BR>
The Midnight Commander<BR>
visual shell<BR>
displays a graphical<BR>
interface to Linux file<BR>
commands.</P>
<a href="javascript:displayWindow('images/ch05fg01.jpg', 288, 216)"><img src="images/tn_ch05fg01.jpg"></a><BR>
<BR>
<UL>
<LI> Compares directory contents
<LI> Uses customized menus so you can build your own commands
<LI> Can use network links for telnet or FTP operations (see Hour 13,
"Internet Downloading and Browsing")
<LI> Offers mouse-click decompression of files (see
gzip)
<LI> Can undelete files (if your Linux filesystem is configured to support this)
</UL>
<P>Midnight Commander is a handy and convenient tool to use for file handling and
directory navigation. You will have to invest some time in learning how to use this program, but
if you've used similar file-management interfaces, you'll feel right at home.
</P>
<H3><A NAME="ch05_ 13">
Searching Files
</A></H3>
<A NAME="PAGENUM-74"><P>Page 74</P></A>
<P>This section introduces you to the use of sophisticated wildcards, or regular
expressions, along with short examples of file searches using the
grep family of programs. If you understand and use these expressions, you'll be able to create refined search
techniques you'll use again and again. You'll save time and effort during your work, and your
learning investment will pay big dividends throughout your Linux experience.
</P>
<H4><A NAME="ch05_ 14">
What Are Regular Expressions?
</A></H4>
<P>Regular expressions are patterns, using special syntax, that match strings (usually in
text in files, unless your search is for filenames).
There are also extended regular expressions, but the difference, important for syntax, should not deter you from the valuable lesson
of being able to construct patterns that will accurately match your desired search targets.
This is important if you're looking for text in files, and critical if you're performing
potentially dangerous tasks, such as multiple file deletions across your system.
</P>
<P>You can build an infinite number of regular expressions using only a small subset of
pattern characters. Here's a short list of some of these characters. You should be familiar with
at least one (the asterisk) from the previous examples:
<TABLE>
<TR><TD>
*
</TD><TD>
Matches any character
</TD></TR><TR><TD>
? or .
</TD><TD>
Matches a single character
</TD></TR><TR><TD>
[xxx] or [x-x]
</TD><TD>
Matches a character in a range of characters
</TD></TR><TR><TD>
\x
</TD><TD>
Matches a character such as ? or \
</TD></TR><TR><TD>
^pattern
</TD><TD>
Matches pattern to the beginning of a line
</TD></TR><TR><TD>
$pattern
</TD><TD>
Matches pattern to the end of a line
</TD></TR></TABLE>
<P>This is not a comprehensive list of pattern characters. For more details, you can read
the ed manual page (the ed command is discussed in Hour 14, "Text Processing"). For
now, try several simple examples using different patterns.
</P>
<P>You should know the asterisk, which is useful for finding matches to all characters.
For example, if you want to find all the text files in your directory with an extension of
.txt, you can use
</P>
<!-- CODE SNIP //-->
<PRE>
# ls *.txt
14days.txt 96hours.txt datalog.txt datebook.txt day67.txt
</PRE>
<!-- END CODE SNIP //-->
<P>But suppose you wanted a list of all files in your directory with numbers in the
filename? You can try to string multiple searches on the
ls command line, like this:
</P>
<!-- CODE //-->
<PRE>
# ls *0* *1* *2* *3* *4* *5* *6* *7* *8* *9*
08100097.db 14days.txt backup001.file phonelog.111597
08100097.db 32days.msg day67.txt phonelog.111597
08100097.db 32days.msg day67.txt phonelog.111597
08100097.db 96hours.txt message.76
08100097.db 96hours.txt message.76
14days.txt backup001.file phonelog.111597
</PRE>
<!-- END CODE //-->
<P><CENTER>
<a href="0069-0071.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0075-0077.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -