RedHat Enterprise Linux Computing essenessenview the File System

Source: Internet
Author: User
Tags gpg

 

 

3. view the File System

Main Concepts

  • The basis of the Linux directory tree is a directory called/, called "root directory )".
  • Absolute (absolute) file reference, such as/home/elvis/lsout.txt, refers to the file relative to the root directory.
  • Relative (relativeworkflow file reference, such as lsout.txt, refers to the file relative to the current working directory of the process.
  • The ls command is used to list directory content.
  • The cat command is used to check the file content.
  • You can use the shell command line> character to redirect the command output from the terminal to the file.
  • The user's home directory is one of the locations where the user can create new files.

Discussion

 

File and directory naming
In Linux, information and programs are stored as files on disks. Files are classified into directories that contain files and other directories. (Other operating systems often refer to directories as "Folders ".) This directory structure, including multi-level directories, is often referred to as "directory tree )".

The root of the directory tree is a directory named/, which is called "root directory )". The file in the root directory is called/filename. In Red Hat Enterprise Linux, the root directory is mainly other directories, such as/etc. The files in these directories are called/etc/filename. Each layer of the directory tree is named like this. For example, if the file network is located in the sysconfig Directory, which is located in the etc directory and the etc directory is located in the/directory, the file reference is/etc/sysconfig/network.

Obviously, it is very troublesome to reference files from the root directory every time. Fortunately, Linux provides a simpler method. Each process, including the user's shell, uses the current working directory as the environment. Files in the current working directory of a process can be directly referenced as filename without a diagonal line. The file in the directory of the current working directory can be referenced as dirname/filename without a slash. For example, if the current working directory of a process is/etc, the file network mentioned above can be referenced as sysconfig/network. If the working directory is/etc/sysconfig, the file can be referenced as a network.

In short, there are two ways to reference a file. The file reference of the root directory always starts with/. This reference method is called absolute reference. The file reference of the current working directory does not start with/. This reference method is called relative reference.

Use ls to list directory content
The user uses the ls command from the shell to list the contents of the directory. (Ls is the abbreviation of the verb "list .) In the following example, elvis wants to list the contents of the/etc/sysconfig/rhn directory.

Source code

 

[Elvis @ station elvis] $ ls/etc/sysconfig/rhn

Rhn-applet systemid up2date-keyring.gpg up2date-uuid

Rhnsd up2date up2date. rpmnew

 

When the ls command is called without parameters (that is, the directory is not specified), this command lists the contents of the current shell working directory. If a color terminal is used, ls will display the file names in different colors to help distinguish regular files (white) from directories (blue ).

LsIt is a very flexible command that can provide a large amount of different information, which will be discussed in detail in future courses.

UseCatView File ContentLsCommand to display the files contained in a given directory, but the contents of these files are not displayed. There are several commands that can be used to browse the file content. The simplest isCatCommand. Run the following command to list the files that are stored in concatenates and output them to the terminal. If only one file name is provided, the file content is displayed as the output.

In the following example, elvis wants to browse the configuration file/etc/hosts.

Source code

 

[Elvis @ station elvis] $ cat/etc/hosts

# Do not remove the following line, or various programs

# That require network functionality will fail.

127.0.0.1 localhost. localdomain localhost

192.168.0.254 server1.example.com server1

192.168.0.1 station1.example.com station1

 

Now you don't have to worry about what the file content is, just realize that the command cat can display all the five elements of the file.

If you instructCatDisplays a very long file or binary (non-text) file,CatIt will also be executed as usual. More advanced commands for browsing large files can be viewed on pages one by one, which will be introduced later.

Redirect command output to file
When executedLsAndCatCommand output is displayed on the terminal. In Linux, most of the commands that generate text output use a common Unix concept called the "standard out" stream. By default, this data stream is connected to the terminal. Bash shell allows users to "redirect" (redirect) the standard output stream to another location. Now we can only learn the simplest method. Use> to redirect the standard output to a file.

In the following example, the user elvis will list the contents of the/etc/sysconfig/rhn directory again, but this time he redirects the output to a new file.

Source code

 

[Elvis @ station elvis] $ ls/etc/sysconfig/rhn> lsout.txt

[Elvis @ station elvis] $ ls

Lsout.txt

[Elvis @ station elvis] $ cat lsout.txt

Rhn-applet

Rhnsd

Systemid

Up2date

Up2date-keyring.gpg

Up2date. rpmnew

Up2date-uuid

 

CommandLs/etc/sysconfig/rhnIs saved to a new file named lsout.txt. Elvis then uses ls in the current working directory to view the new file, and then uses the command cat to browse the file content. In Unix, elvis "redirects the output of the command ls to the file lsout.txt ."

Permission and the user's home directory.
Note what happens if elvis tries to redirect the output to a file other than the current working directory of the shell.

Source code

 

[Elvis @ station elvis] $ ls/etc/sysconfig/rhn>/etc/lsout.txt

-Bash:/etc/lsout.txt: No such file or directory

 

Elvis has another common Linux concept: File Ownership and permissions. Elvis tries to create a new file/etc/lsout.txt, but he does not have the permission to create a file in the directory/etc.

By default, Red Hat Enterprise Linux does not allow users to create files anywhere. In fact, there are only a few locations for creating new files. Each user has a home directory where they can create new files (and sub-Directories ). Fortunately, when a user logs on to a Linux session, shell uses the user's home directory as its current working directory. By default, in Red Hat Enterprise Linux, the user's home directory is named/home/username, and username is replaced by the user name.

The file system and permissions will be discussed in detail in future tutorials. Now you only need to know that you can only create files in your home directory.

Examples
Redirects command output to a file.

The user prince wants to use the cal command to save the calendar of the current month to the calendar.txt file.

Source code

 

[Prince @ station prince] $ cal 1

May 1, 2003

SuMoTu We Th Fr Sa

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

 

[Prince @ station prince] $ cal> calendar.txt 2

[Prince @ station prince] $ ls 3

Calendar.txt

[Prince @ station prince] $ cat calendar.txt 4

May 1, 2003

SuMoTu We Th Fr Sa

1 2 3

 

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

 

  1. Prince browsed the output of command cal directly.
  2. Prince runs the cal command again to redirect the output to the calendar.txt file.
  3. Prince lists the contents of your current working directory (the main directory here) and confirms that the new file has been created.
  4. Prince checks the content of the new file and determines that it does contain the output of the cal command.

Exercise
LsCommand
[Warning]Computer exercises

Source code

Purpose:Redirects the command output to the new file.

 

Estimated Time: 5 mins.

Operations
UseLsCommand to list the content of the/etc directory, and redirect the command output to the file named lsetc.txt in your main directory.

Result
A title
Question 1

  1. Your main directory contains the file lsetc.txt, which contains the output of the command ls/etc.

Reference answer
The following command is the reference answer for this exercise.

[Student @ station student] $ ls/etc> lsetc.txt

 

Problem
Multiple choice questions
A title

Question 1
Assume that the current working directory of shell is/home/elvis. Which of the following is a reference to the file/home/elvis/Mail/sent?

  1. A. Mail/sent
  2. B./Mail/sent
  3. C. sent
  4. D./sent

Question 2
The current working directory of the user elvis shell is/home/elvis. Which of the following is the reference of the file named Ed? This file is located in the Mail directory in the main directory.

  1. A./home/elvis/Mail/received
  2. B. Mail/received
  3. C. received
  4. D. both A and B.

Question 3
Which of the following commands will list files in the/usr/lib directory?

  1. A. cat/usr/lib
  2. B. lsdir/usr/lib
  3. C./usr/lib list
  4. D. ls/usr/lib

Question 4
Which of the following commands will successfully redirect the output of the command cal to the file lsout.txt?

  1. A.lsout.txt> cal
  2. B. cal ==> lsout.txt
  3. C. cal> lsout.txt
  4. D. cal} lsout.txt

Question 5
The main directory of elvis is/home/elvis. Which of the following commands can be run successfully on the default Red Hat Enterprise Linux system?

  1. A. ls/etc>/etc/lsetc.txt
  2. B./etc/lsetc.txt <ls/etc
  3. C. ls/etc>/home/elvis/lsetc.txt
  4. D./home/elvis/lsetc.txt <ls/etc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.