-->

Previous | Table of Contents | Next

Page 72




# 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

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:


# rm -f file1

# cat file2

cat: file2: No such file or directory

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:


# ln /usr/local/games play

ln: /usr/local/games: hard link not allowed for directory

But you can use a symbolic link with


# ln -s /usr/local/games play

# ls -l play

lrwxrwxrwx   1 bball    bball          16 Nov 13 19:28 play -> /usr/local/games

Now, instead of typing a long command like


# cd /usr/local/games

you can use


# cd play

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.

Handling Files with the Midnight Commander Program

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:


# mc

This section does not cover all of the details of the mc command. But here are the highlights of its features:

Page 73

Figure 5.1.
The Midnight Commander
visual shell
displays a graphical
interface to Linux file
commands.



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.

Searching Files

Page 74

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.

What Are Regular Expressions?

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.

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:
* Matches any character
? or . Matches a single character
[xxx] or [x-x] Matches a character in a range of characters
\x Matches a character such as ? or \
^pattern Matches pattern to the beginning of a line
$pattern Matches pattern to the end of a line

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.

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


# ls *.txt

14days.txt    96hours.txt   datalog.txt   datebook.txt  day67.txt

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:


# 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

Previous | Table of Contents | Next