Linux basic Commands (ii)

Source: Internet
Author: User
Tags bz2 gz file unzip archive

Linux directory and file management 1.Linux directory structure

Linux is a reciprocal structure , from '/' as a starting point, where all files or directories are represented by '/'
Linux has only one '/', analogy to Windows has multiple '/' (a disk one '/')

Example :
such as the location of the Docs directory
/bin/mark/docsis equivalent to Windows (C:\bin\mark\docs)
Note : '/' as a starting point, the directory and files are separated by '/# '
Common Linux Directories :

  • /etc///Store all configuration files
  • /bin///Storing system commands that all users can execute
  • /sbin///Store system commands that only administrators (root) or users with administrator privileges can perform
  • /mnt///default is empty for temporary mount
  • /var///Dynamic storage directory, storing dynamic files, such as logs, mail, etc.
  • /dev///storage equipment, such as hard disk, CD-ROM, etc.
  • /home///General user home Directory
  • /root///admin Root home directory
  • /usr///user self-installing software storage directory
  • /boot///store kernel, boot file directory
    Noteall files under Linux, such as the keyboard, mouse and so on in the Linux are embodied as a file, but some files can not be opened directly, you need to use the program or software Call 2. View and retrieve file CD commands

    CD < directory path to enter >
    Switch the directory (equivalent to a mouse double-click into a directory in Windows, directly view the contents of the directory)
    Example :
    pwdView your current directory

    cd /etc/Switch from the current path to the/etc/directory

    cd 或 cd ~Enter the current login user home directory

    cd -Go to last switch directory

    Relative path and absolute path
    relative path : Represents the position starting from the current path;./For the current directory, ... /represents the upper-level path
    Absolute Path : The full representation of the path starting from "/"
    Example :
    represents the "/etc/sysconfig/network-scripts/ifcfg-eth0" file location, currently located in the "etc/" directory
    Relative path:./sysconfig/network-scripts/ifcfg-eth0 or Sysconfig/network-scripts/ifcfg-eth0
    Absolute path:/etc/sysconfig/network-scripts/ifcfg-eth0
    represents the "/etc/hosts" file location, currently located in the "/etc/sysconfig/network-scripts/" directory
    Relative path: /.. /hosts
    Absolute path/etc/hosts
    Note : Generally relatively more convenient than absolute use, can reduce the file structure, of course, according to the customary use can

    Cat command

    Cat [Options] File name:
    Display file contents to view multiple file contents at the same time
    Options :
    -N: Show line numbers
    -A: Displays all special symbols in the file, such as "$" terminator, etc.
    Example :
    cat /etc/hostsView/etc/hosts file contents

    cat -n /etc/hostsView/etc/hosts file contents and display line numbers

    cat -A /etc/hostsView the contents of the/etc/hosts file and all its special symbols

    More commands

    More [options] File name ...
    Full screen display of the contents of the file, can be paged to view multiple files at the same time
    shortcut keys :
    Enter key: Line down
    SPACEBAR: Turn down one page
    Q Key: Exit
    Example :
    more /root/anaconda-ks.cfgPaging View/root/anaconda-ks.cfg File


    Note : More can only be turned upside down, cannot go up

    Less command

    Less [options] file name ...
    Same as the more command, but supports scrolling up and down, and supports the search function
    shortcut keys :
    Page UP key: Page UP
    Page DOWN key: Page Down
    /text: Find specified file in file
    N: Jumps to the next address in the search text in the file
    N: Jump to the previous address in the file to search for files
    Q Key: Exit
    Example :
    less /root/anaconda-ks.cfg

    Detailed Head command

    Head-n file name ...
    View part of the beginning of a file (default is 10 lines)
    head /root/anaconda-ks.cfgView the beginning of the file 10 lines

    head -3 /root/anaconda-ks.cfgView the beginning of the file 3 lines

    Tail command

    Tail-n file name ...
    View a portion of the end of the file (default is 10 lines)
    Options :
    -F: Dynamic update To view the end of the file, real-time monitoring file changes (CTRL + C key termination), generally used for the log
    Example :
    tail /root/anaconda-ks.cfgView end of file 10 lines

    tail -3 /root/anaconda-ks.cfgView end of File 3 lines

    tail -f /var/log/messagesTo view the public message log file dynamically

    WC command

    WC [Options] File
    Information about the number of words in the statistics file (Word Count), file size, number of file lines, and more
    Options :
    -L: Count rows
    -W: Count the number of words, separated by spaces
    -C: Statistics of bytes
    Example :
    wc /etc/hostsThe situation of statistical/etc/hosts

    Note : When using the WC command, do not follow any options, default use WC-LWC
    wc -l /etc/hostsCount the number of lines in a/etc/hosts file

    wc -l /etc/passwdCount/etc/passwd file lines to get the number of accounts in the system

    find /etc -name "*.conf" | wc -lStatistics. conf number of files ending in

    grep command

    grep [Options] ... < find criteria > < destination file >
    Finds and displays the line containing the specified string in the file
    Options :
    -I: Ignore case when finding
    -V: Reverse lookup, output rows that do not match the condition
    Search Criteria :
    The searched string should be enclosed in double quotation marks ("").
    "^ ..." means ... The beginning, "... $" represents a ... End
    "^$" indicates a blank line
    Example :
    grep "f" /etc/inittabFilter the lines in the display file that contain F (match lowercase characters F only)

    grep -i "f" /etc/inittabFilter the lines in the display file that contain F (ignoring case, case f)

    grep "f$" /etc/inittabFilter the lines that end with F in the display file

    grep "^i" /etc/inittabFilter the lines that start with I in the display file

    grep -v "^#" /etc/inittabFilter the remaining lines in the display file except #

    grep -v "^$" /etc/inittabTo filter the remaining lines in the file that are empty lines apart

    grep -v "^#" /etc/inittab | grep -v "^$"Display valid lines in a file (no blank lines and # start lines are displayed)

  • 3. File compression and archiving gzip and gunzip

    gzip [-9] < filename ...//compression
    GZIP-D xx.gz Format compressed file or gunzip xx.gz format compressed file//Unzip
    Use the GZIP compression algorithm to compress files, unzip compressed files (can reduce file occupancy, but not absolute)
    Options :
    -9: High compression ratio, compressed files smaller (from 0~9, the larger the number, the higher the compression ratio, the default is 6)
    -D: For extracting files
    Example :
    dd if=/dev/zero of=/root/test bs=100M count=1Generate a test file of size 100M

    gzip /root/testUse gzip to compress the file, the original file is lost and changed to Test.gz

    gzip -d /root/test.gzUnzip the test.gz file to the original directory

    Note : Only files compressed using gzip can be decompressed, with the suffix. gz
    gzip -9 /root/testHigh compression ratio using gzip compressed files, default to 6

    gunzip /root/test.gzUnzip using the Gunzip command, as with the gzip-d effect

    Bzip2 and BUNZIP2

    bzip2 [-9] < filename ...//compression
    BZIP2-D xx.bz2 Format compressed file or BUNZIP2 xx.bz2 format compressed file//Unzip
    Use the BZIP2 compression algorithm to compress files, extract compressed files (can reduce file occupancy, but not absolute)
    Options :
    -9: High compression ratio, compressed files smaller (from 0~9, the larger the number, the higher the compression ratio, the default is 6)
    -D: For extracting files
    Example :
    bzip2 /root/testUse bzip2 to compress the file, the original file is lost and changed to TEST.BZ2

    bzip2 -d /root/test.bz2Unzip the test.bz2 file to the original directory

    Note : Only files compressed using the Bzip2 method can be decompressed, with the suffix. bz2
    bzip2 -9 /root/testHigh compression ratio using bzip2 compressed files, default to 6

    bunzip2 /root/test.bz2Unzip using the BUNZIP2 command, as with the bzip2-d effect

    Xz

    XZ [-9] < filename ...//compression
    xz-d compressed files//decompression in XX.XZ format
    Use the XZ compression algorithm to compress files, extract compressed files (can reduce file occupancy, but not absolute)
    Options :
    -9: High compression ratio, compressed files smaller (from 0~9, the larger the number, the higher the compression ratio, the default is 6)
    -D: For extracting files
    Example :
    xz /root/testUse XZ mode to compress the file, the original file is lost to Test.xz

    xz -d /root/test.xzUnzip the Test.xz file to the original directory

    Note : Only files compressed using XZ mode can be decompressed, with the suffix. XZ
    xz -9 /root/testHigh compression ratio using XZ compressed files, default to 6

    xz -d /root/test.xzUnzip the Test.xz file to the original directory

    Comparison of three compression modes

  • gzip is best for files to be used under the Unix/linux system, although bzip2 is already popular, but Gzip will still be used for a long time
  • bzip2 uses a different algorithm than gzip and produces smaller files than gzip, but takes longer to compress
  • XZ provides the best compression rate, but it also takes a considerable amount of time. Newer than gzip, bzip2, but some systems may not be installed (self-installing)
  • Summary: xz > Bzip2 > Gziptar

    tar [options] ... < archive file name > < source file or directory >//Create Archive
    tar [options] ... < archive file name > [-c < target directory;]//Unzip archive
    Options
    -Z: Call the gzip program for compression or decompression
    -J: Call bzip2 Program for compression or decompression
    -j: Call the XZ program for compression or program
    -C: Create a package file in. tar format
    -X: Unpack the. tar format package file
    -T: List view in-package files
    -V: Output details
    -F: Using an archive file
    -P: Preserve the original file and directory permissions while packaging
    -C: Specify the target folder to release when unpacking
    -P: Archive with absolute path (resolves "tar:removing leading '/' from member names" issue)
    Note: With tar archive, source files or folders are not lost and still exist (Windows-like compression tools)
    Example
    (1) Filing
    tar -zcvf /root/ceshi.tar.gz /boot/ /bin/Archive the/boot/and/bin/directories as a file using gzip and place them under/root/named ceshi.tar.gz

    tar -jcvf /root/ceshi.tar.bz2 /boot/ /bin/Archive the/boot/and/bin/directories as a file using bzip2 mode, and place them under/root/named ceshi.tar.bz2

    tar -Jcvf /root/ceshi.tar.xz /boot/ /bin/Archive the/boot/and/bin/directories as a file using XZ mode and place them under/root/named Ceshi.tar.xz

    ls-lh/root/ceshi*//List view files starting with Ceshi in the/root/directory

    (2) View archived files
    tar -ztvf /root/ceshi.tar.gzView ceshi.tar.gz files that are compressed using gzip

    tar -jtvf /root/ceshi.tar.bz2View ceshi.tar.bz2 files that are compressed using the Bzip2 method

    tar -Jtvf /root/ceshi.tar.xzView CESHI.TAR.XZ files that are compressed using XZ mode

    Note: Tar archive can only be viewed using the corresponding compression method when viewed, such as compression using gzip (-Z)
    (3) Unzip the archive file
    tar -zxvf /root/ceshi.tar.gz -C /tmp/Extract the ceshi.tar.gz archive file using gzip compression to the/tmp/directory

    tar -jxvf /root/ceshi.tar.bz2 -C /tmp/Extract the ceshi.tar.bz2 archive file using bzip2 compression to the/tmp/directory


    tar -Jxvf /root/ceshi.tar.xz -C /tmp/Extract the CESHI.TAR.XZ archive file compressed with XZ mode to the/tmp/directory


    Note: When the tar is decompressed, the corresponding compression method must be used to decompress the corresponding compression method;

Linux basic Commands (ii)

Related Article

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.