rhl08.htm

来自「linux的初学电子书」· HTM 代码 · 共 2,355 行 · 第 1/5 页

HTM
2,355
字号

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>How Directories Are Named</B></FONT></CENTER></H4>

<BR>

<P>Directories are named just like files, and they can contain upper- and lowercase letters, numbers, and characters such as -, ., and _.

<BR>

<P>The slash (/) character is used to show files or directories within other directories. For instance, usr/bin means that bin is found in the usr directory. Note that you can't tell, from this example, whether bin is a file or a directory, although you 
know that usr must be a directory because it holds another item&#151;namely, bin. When you see usr/bin/grep, you know that both usr and bin must be directories, but again, you can't be sure about grep. The ls program shows directories with a following 
/&#151;for example, fido/. This notation implies that you could have, for instance, fido/file; therefore, fido must be a directory.

<BR>

<P>The root directory is shown simply by the symbol / rather than mentioned by name. It's very easy to tell when / is used to separate directories and when it's used to signify the root directory. If / has no name before it, it stands for the root 
directory. For example, /usr means that the usr subdirectory is found in the root directory, and /usr/bin means that bin is found in the usr directory and that usr is a subdirectory of the root directory. Remember, by definition the root directory can't be 
a subdirectory.

<BR>

<BR>

<A NAME="E69E52"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>The Home Directory</B></FONT></CENTER></H4>

<BR>

<P>Linux provides each user with his or her own directory, called the home directory. Within this home directory, users can store their own files and create subdirectories. Users generally have complete control over what's found in their home directories. 
Because there are usually no Linux system files or files belonging to other users in your home directory, you can create, name, move, and delete files and directories as you see fit.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Your home directory does not provide privacy! Normally, any user can go into another's home directory and read (and copy!) the files stored there (although he can't delete or change the files). When Linux creates your home directory, it in effect 
provides you with an open office cubicle whose desk and filing cabinet drawers are unlocked.

<BR>You must lock up everything you want to keep private. (This topic is covered in the section &quot;File Permissions and Ownership.&quot;) It is generally considered rude or nosy to poke around in someone else's home directory, just as it's rude or nosy 
to poke around in someone's office while they're away from their desk, but the world is full of nosy and rude people, so you must take precautions!

<BR>Note that anyone logged in as root can read and manipulate all the files on the system, including files that users have locked up. If you can't trust the system administrator (who usually has the root password), don't use the system!</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>The location of a user's home directory is specified by Linux and can't be changed by the user. This is both to keep things tidy and to preserve system security.

<BR>

<BR>

<A NAME="E68E54"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Navigating the Linux File System</B></FONT></CENTER></H3>

<BR>

<P>Fortunately, navigating the Linux file system is simple. There are only two commands to be learned, and one of them has absolutely no options or parameters!

<BR>

<BR>

<A NAME="E69E53"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>The pwd Command Where Am I?</B></FONT></CENTER></H4>

<BR>

<P>Type pwd at the Linux command prompt. You see

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ pwd

/home/fido

darkstar:~$</FONT></PRE>

<P>This tells you that you're currently in the directory /home/fido. (If you are logged in under a different user name, you will see that name in place of fido.) This is your home directory. When you log in, Linux always places you in your home directory.

<BR>

<P>The letters &quot;pwd&quot; stand for &quot;print working directory.&quot; Again, a command's name or function has been cut down to a few easy-to-type characters. (You will often see the term current directory used in place of working directory.)

<BR>

<P>You might be wondering what &quot;working directory&quot; or &quot;being in a directory&quot; really means. It simply means that Linux commands, by default, perform their actions in your working directory. For instance, when you run ls, you are shown 
only the files in your working directory. If you want to create or remove files, they will be created or removed in your working directory.

<BR>

<BR>

<A NAME="E69E54"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Absolute and Relative Filenames</B></FONT></CENTER></H4>

<BR>

<P>If you specify only the name of a file, Linux looks for that file in your working directory. For example, more myfile lets you read the contents of the file myfile. But myfile must be in your current working directory, or the more command won't find it.


<BR>

<P>Sometimes you want to specify a file that isn't in your current directory. You would then specify the name of the directory the file is in, as well as the name of the file itself.

<BR>

<P>If, for instance, your current directory has a subdirectory called novel, which contains a file called chapter_1, you could type more novel/chapter_1, which tells more that it should look in the subdirectory novel for the file chapter_1. This is called 
a relative filename. You are specifying the location of chapter_1 relative to where you are now, in the subdirectory novel, which is found in your current directory. If you changed your working directory, the relative filename would no longer work.

<BR>

<P>Two special directory specifications are &quot;.&quot; and &quot;..&quot;.The specification &quot;.&quot; always stands for the directory you are currently in, and &quot;..&quot; stands for the parent directory of your current directory. (You see how 
&quot;.&quot; and &quot;..&quot; are used later in this chapter.) Any filename that includes &quot;.&quot; or &quot;..&quot; is, by definition, a relative filename.

<BR>

<P>A filename that is valid from any location is called an absolute filename. Absolute filenames always begin with /, signifying root. So if you specify a filename as /home/fido/novel/chapter_1, there is no doubt as to where the file is located. Every file 
on your system has a unique absolute filename.

<BR>

<P>Someone else on the system might also have a directory called novel in his or her home directory. Perhaps it even contains a file called chapter_1. In this case, you can't distinguish the two files by using the relative filename novel/chapter_1. 
However, the absolute filenames will be different&#151;for instance, /home/fido/novel/chapter_1 as opposed to /home/mary/novel/chapter_1. The novel subdirectory in /home/fido is not the same directory as the novel directory in /home/mary! The two are in 
quite separate locations, and only coincidentally do they share the same name.

<BR>

<BR>

<A NAME="E69E55"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Going Places The cd Command</B></FONT></CENTER></H4>

<BR>

<P>The cd (change directory) command lets you change your working directory. You can think of it as moving to another directory.

<BR>

<P>The syntax of the cd command is

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">cd &lt;directory specification&gt;</FONT></PRE>

<P>There must be a space between cd and the directory specification.

<BR>

<P>The directory specification can be an absolute or relative one. For instance, type cd .. followed by pwd:

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ cd ..

darkstar:/home$ pwd

/home

darkstar:/home$ cd ..

darkstar:/$ pwd

/

darkstar:/$ cd ..

darkstar:/$ pwd

/</FONT></PRE>

<P>There is no parent directory for the root directory, so typing cd .. when in the root directory simply leaves you in the root directory.

<BR>

<P>Note that the Linux command prompt shows you which directory you are currently in, so you don't have to type pwd all the time. (I will continue to use pwd for clarity.)

<BR>

<P>You can also use absolute directory names.

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:/$ cd /usr/bin

darkstar:/usr/bin$ pwd

/usr/bin</FONT></PRE>

<P>When you type an absolute directory name, you go to that directory, no matter where you started from. When you type cd .., where you end up depends on where you started.

<BR>

<P>To see the effect of changing your working directory, type ls. The list of files is so long that the first part scrolls off your screen. The ls command shows you the contents of your current directory (as always), but now your current directory is 
/usr/bin, which contains many more files than your home directory.

<BR>

<BR>

<A NAME="E69E56"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>There's No Place Like Home</B></FONT></CENTER></H4>

<BR>

<P>Type cd without any directory specification:

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:/usr/bin$ cd

darkstar:~$ pwd

/home/fido</FONT></PRE>

<P>Typing cd by itself always returns you to your home directory. When exploring the file system, you sometimes wind up deep in a blind alley of subdirectories. Type cd to quickly return home, or type cd / to return to the root directory.

<BR>

<P>The ~ in your prompt is another special character. It stands for your home directory. There's no reason to type cd ~ when cd works just as well, and is much easier to type! However, try this:

<BR>

<P>When you type cd ~&lt;user&gt;, you move to that user's home directory. This is a very useful trick, especially on large systems with many users and more complicated directory structures than the simple /home/&lt;user&gt; on your Linux system.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>When you're changing to a distant directory, it's often a good idea to take several steps. If you mistype a very long directory specification, you will have to retype the entire specification. Sometimes it might not even be clear why cd gave you an 
error! Taking a number of shorter steps means less retyping in case of an error. Consider this example:

<BR>darkstar:~$ cd /usr/docs/faq/unix

<BR>bash: /usr/docs/faq/unix: No such file or directory

<BR>You're pretty sure that this path is correct. Let's change directories one step at a time:

<BR>darkstar:~$ cd /usr

<BR>darkstar:/usr$ cd docs

<BR>bash: docs: No such file or directory

<BR>Aha! There's a problem with docs. The directory is actually named doc:

<BR>darkstar:/usr$ ls

<BR>bin/ doc/ games/ info/ man/ sbin/ spool/

<BR>darkstar:/usr$ cd doc

<BR>darkstar:/usr/doc$ cd faq/unix

<BR>darkstar:/usr/doc/faq/unix$ pwd

<BR>/usr/doc/faq/unix</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E68E55"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Creating and Deleting Files</B></FONT></CENTER></H3>

<BR>

<P>Linux has many ways to create and delete files. In fact, some of the ways are so easy to perform that you have to be careful not to accidentally overwrite or erase files!

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>Go through the following sections very carefully. You should be logged in as your &quot;ordinary&quot; username, not as root! Only when you're sure you understand these sections thoroughly should you use these commands while logged in as root.

<BR>There is no &quot;unerase&quot; command in Linux! Be sure you know what you're doing!</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>Return to your home directory by typing cd. Make sure you're in your /home/&lt;user&gt; directory by running pwd.

<BR>

<P>In the previous chapter, you created a file by typing ls -l /bin &gt; test. Remember, the &gt; symbol means &quot;redirect all output to the following filename.&quot; Note that the file test didn't exist before you typed this command. When you redirect 
to a file, Linux automatically creates the file if it doesn't already exist.

<BR>

<P>What if you want to type text into a file, rather than some command's output? The quick and dirty way is to use the command cat.

<BR>

<BR>

<A NAME="E69E57"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>cat That Useful Feline</B></FONT></CENTER></H4>

<BR>

<P>The cat command is one of the simplest, yet most useful, commands in Linux. It certainly does more than any living feline!

<BR>

<P>The cat command basically takes all its input and outputs it. By default, cat takes its input from the keyboard and outputs it to the screen. Type cat at the command line:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ cat</FONT></PRE>

<P>The cursor moves down to the next line, but nothing else seems to happen. Now cat is waiting for some input:

<BR>

<PRE>

<FONT COLOR="#000080">hello

hello

what

what

asdf

asdf</FONT></PRE>

<P>Everything you type is repeated on-screen as soon as you press Enter!

<BR>

<P>How do you get out of this? At the start of a line, type ^D (Ctrl-D). (In other words, hold down the Ctrl key and press D.) If you're not at the beginning of a line, you have to type ^D twice. ^D is the Linux &quot;end of file&quot; character. When a 
program such as cat encounters a ^D, it assumes that it has finished with the current file, and it goes on to the next one. In this case, if you type ^D by itself on an empty line, there is no next file to go on to, and cat exits.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>When you say that a program exits, you mean that it has finished running and that you are back at the Linux command prompt. It might seem odd to talk about the program exiting when, from your point of view as a user, you have exited the program. This 
turn of phrase goes back to the early days of UNIX, when it was coined by the people who were programming the system. They looked at things from the program's point of view, not the user's!</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>So how do you use cat to create a file? Simple! You redirect the output from cat to the desired filename:

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ cat &gt; newfile

Hello world

Here's some text</FONT></PRE>

<P>You can type as much as you want. When you are finished, press ^D by itself on a line; you will be back at the Linux prompt.

<BR>

<P>Now you want to look at the contents of newfile. You could use the more or less commands, but instead, let's use cat. Yes, you can use cat to look at files simply by providing it with a filename:

<BR>

<PRE>

<FONT COLOR="#000080">darkstar:~$ cat newfile

Hello world

Here's some text

darkstar:~$</FONT></PRE>

⌨️ 快捷键说明

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