The most common directory and file management commands in Linux

Source: Internet
Author: User

One, the command to view the file

For a text file, there are several ways to view the contents of a file in Linux, such as displaying the entire text content directly, paging through the content, or viewing only parts of the file at the beginning or end of the document. Linux can be implemented with different commands.

1. cat-Displays and connects the contents of the file 

This command is the most widely used content viewing command. When you use this command, you only need to take the path of the file you want to view as an argument. For example, the following operations can view the contents of the/etc/sysconfig/network-scripts/ifcfg-eth0 configuration file for the configuration information of the first NIC.

  

2. More and less-paging to view the contents of a file

With cat, you can simply display the entire contents of the file, but when the file is too large, it is possible to see only the last part of the message, which may be too late to read. And more and less can use pagination to display the file, so that we read. In the reading interface, you can see the progress of the file reading in the lower left corner, you can press ENTER to scroll down by line to view, press the space down a screen, press the B key up one screen, press the Q key to exit.

For example, we can use more to view the contents of a/etc/vsftpd.conf file.

  

More can also be combined with the pipe symbol "|" Split-screen view executes the contents of the command output. For example, the following operation can be viewed in the/etc directory with a ". conf" extension. You cannot page up this way.

  

3. head and tail-view content at the beginning or end of a file

Head and tail are a pair of opposite commands, which are used to display part of the beginning of a file, which is used to display a portion of the end of a file, and you can use the "-n" option to specify a specific number of rows, without specifying the default 10 rows. Tail also monitors dynamic updates at the end of a file in real time using the "-f" option (such as viewing changes in log files).

Use head to view/etc/passwd files first 3 lines: head-3/etc/passwd

  

View End 5 Lines

  

Ii. statistics and retrieval of the contents of documents

1.WC-Count the number of words, lines, and bytes of a file

-C: Count bytes-L: Count Rows-W: Count the number of words, without the option default three statistics.

For example: Statistics/etc/passwd

  

You can also combine the Find command to count the number of files in the/etc directory with the suffix ". conf", as follows:

  

2.grep-Retrieve and filter file contents

Used to find and display a line in a file that contains the specified character, which can be combined with a conditional expression (for example, "^word" means beginning with Word, "word$" means ending in Word, "^$" means a blank line.) )

There are two common options:

-I: Ignore case when finding

-V: Reverse lookup, which outputs rows that do not match the lookup criteria

For example, to see if there is an FTP user, operate as follows:

  

Some profiles have a large number of comment files and blank lines that begin with #, which is not good for viewing, and can also be filtered with grep, for example:

  

Iii. Backup and Recovery documentation

1.gzip and bzip2-compression tools

gzip compressed files are generally based on the. gz suffix, and bzip2 compression is generally based on the. bz2 suffix (the suffix in Linux does not determine the format of the file, which is distinguished from windows).

Gzip compressed can be extracted with gzip-d and gunzip

Bzip2 can be extracted with bunzip2.

    

2. Tar-archive and release tools

Primarily used to archive directories and files, the package files are usually compressed (call the previous gzip or bzip2) in the actual work to save space.

Tar has several important options (the "-" before the option with the TAR command can be omitted):

-C: Create a. tar format file

-C: Specify the target folder to release

-F: Indicates the use of an archive file

-J: Call bzip2 Program for compression or decompression

-P: Preserve file and directory permissions while packaging

-P: Preserve the absolute path of files and directories while packaging

-T: View the files in the package

-V: Output details

-X: Unpack package files in. tar format

-Z: Call the gzip program for compression or decompression

The following is a package backup of/etc and/opt to the/tmp directory, as follows:

  

Again, we can extract it into its directory (note that the compression tool is called, and the options are not mistaken), for example:

  

Follow-up will continue to update other content ~ ~

The most common directory and file management commands in Linux

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.