Linux directories and files detailed in Advanced Operations

A, Linux directory structure

Linux directory structure using a tree directory structure contains the root directory and subdirectories.
Linux directories and files detailed in Advanced Operations

1, the root directory

Position all the partitions, directories, files, and so the starting point, the entire directory tree structure, the use of a separate "/" means.

2, subdirectory

Common subdirectories such as / root, / bin, / boot, / dev, / etc, / home, / var, / usr, / sbin.

3, the role of subdirectories

Linux directories and files detailed in Advanced Operations

Two, Linux command to view the contents of the file base

1, cat-- view the file contents

cat for the entire contents of the file displayed at once. The basic syntax is as follows:
Linux directories and files detailed in Advanced Operations

Application examples:

[root@01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@centos01 ~]# cat /etc/sysconfig/network
# Created by anaconda
[root@centos01 ~]# cat /etc/sysconfig/network /etc/hosts
# Created by anaconda
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

2, more-- view the file contents

full-screen mode for more paging file contents. The basic syntax is as follows:
Linux directories and files detailed in Advanced Operations

Interactive methods of operation:

  • Press Enter to scroll down line by line;

  • Press the spacebar to turn down one screen;

  • Press q to exit;

Application examples:

[root@centos01 ~]# more /etc/httpd/conf/httpd.conf 
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
# In particular, see 
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
--More--(2%)

3, less-- view the file contents

With more action less command the same command, but more extensions. The basic syntax is as follows:
Linux directories and files detailed in Advanced Operations

Interactive methods of operation:

  • Page Up key: page up;

  • Page Down key: Down;

  • "/" Key: Find the key content;

  • "N": Find a key element;

  • "N": Find a key element;

Other features and more basic command similar;

4, head, tail-- view the file contents

head, tail basic command syntax is as follows:
Linux directories and files detailed in Advanced Operations

Linux directories and files detailed in Advanced Operations

  • head: View part of the beginning of the file (the default is 10 lines);

  • tail: see the end part of the contents of the file (the default is 10 lines);

Application examples:

[root@centos01 ~]# head -2 /var/log/messages <!--显示文件的开始2行内容-->
Jan 10 20:20:01 centos01 systemd: Started Session 9 of user root.
Jan 10 20:20:01 centos01 systemd: Starting Session 9 of user root.
[root@centos01 ~]# 
[root@centos01 ~]# tail -3 /var/log/messages <!--显示文件的最后3行内容-->
Jan 10 23:10:01 centos01 systemd: Starting Session 30 of user root.
Jan 10 23:20:01 centos01 systemd: Started Session 31 of user root.
Jan 10 23:20:01 centos01 systemd: Starting Session 31 of user root.
[root@centos01 ~]# 
[root@centos01 ~]# tail -f /var/log/messages 
     <!--动态跟踪文件尾部内容,便于实时监控文件内容的变化
(按Ctrl+c组合键终止)-->
Jan 10 23:01:01 centos01 systemd: Starting Session 29 of user root.
Jan 10 23:03:19 centos01 yum[11583]: Installed: httpd-tools-2.4.6-67.el7.centos.x86_64
Jan 10 23:03:19 centos01 yum[11583]: Installed: mailcap-2.1.41-2.el7.noarch
Jan 10 23:03:20 centos01 systemd: Reloading.
Jan 10 23:03:20 centos01 yum[11583]: Installed: httpd-2.4.6-67.el7.centos.x86_64
Jan 10 23:03:20 centos01 journal: g_dbus_interface_skeleton_unexport: assertion 'interface_->priv->connections != NULL' failed
Jan 10 23:10:01 centos01 systemd: Started Session 30 of user root.
Jan 10 23:10:01 centos01 systemd: Starting Session 30 of user root.
Jan 10 23:20:01 centos01 systemd: Started Session 31 of user root.
Jan 10 23:20:01 centos01 systemd: Starting Session 31 of user root.

5, wc-- statistics file contents

wc statistics for the number of words in the document, (Word Count), the number of lines, number of bytes. The basic syntax is as follows:
Linux directories and files detailed in Advanced Operations

wc common options are as follows:

  • -l: number of lines statistics;

  • -w: counting the number of words;

  • -c: byte counts;

Application examples:

[root@centos01 ~]# wc -l /etc/passwd   <!--统计文件行数-->
41 /etc/passwd
[root@centos01 ~]# wc -w /etc/passwd <!--统计文件中单词个数-->
81 /etc/passwd
[root@centos01 ~]# wc -c /etc/passwd  <!--统计文件中字节数-->
2104 /etc/passwd
[root@centos01 ~]# wc /etc/passwd   
   <!--不加选项统计顺序依次是43行,85个单词,2223个字节-->
  43   85 2223 /etc/passwd
[root@centos01 ~]# find /etc -name "*.conf" | wc -l  
     <!--统计/etc下有多少个以*.conf为后缀的文件数量-->
437 

6, grep-- search and filter file content

grep command is used to locate and display the line containing the specified string in the file. The basic syntax is as follows:
Linux directories and files detailed in Advanced Operations

grep command common options are as follows:

  • -i: ignore case when searching;

  • -v: reverse lookup, the line does not match the output conditions;

grep search criteria settings:

  • To find the string in double quotation marks;

  • "^ ......": means the search string that begins ......;

  • "...... $": means the search string to the end ......;

  • "^ $": Indicates Find a blank line;

Application examples:

[root@centos01 ~]# grep -i "SSHD" /etc/passwd  
          <!--查询sshd用户忽略大小写显示出来-->
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@centos01 ~]# grep "ftp" /etc/passwd   
                <!--查询ftp用户显示出来-->
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@centos01 ~]# grep -v "^#" /etc/yum.conf | grep -v "^$"  
                <!--过滤/etc/yum.conf中的注释行和空行-->
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

Third, compress and decompress files

1, gzip, gunzip-- compression and decompression

Linux directories and files detailed in Advanced Operations

Application examples:

<!--压缩和解压缩方法一(“-9”选项代表高压缩)-->
[root@centos01 ~]# ls 
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg
[root@centos01 ~]# gzip -9 initial-setup-ks.cfg 
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg.gz
[root@centos01 ~]# gzip -d initial-setup-ks.cfg.gz 
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg

<!--压缩和解压缩方法二(不加选项进行压缩,解压缩使用gunzip命令)-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg
[root@centos01 ~]# gzip initial-setup-ks.cfg 
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg.gz
[root@centos01 ~]# gunzip initial-setup-ks.cfg.gz 
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg

2, bzip2, bunzip2-- compression and decompression

Linux directories and files detailed in Advanced Operations

Application examples:

<!--方法一-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg
[root@centos01 ~]# bzip2 -9 initial-setup-ks.cfg    <!--高速压缩-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg.bz2
[root@centos01 ~]# bzip2 -d initial-setup-ks.cfg.bz2 <!--解压缩-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg

<!--方法二-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg
[root@centos01 ~]# bzip2 initial-setup-ks.cfg  <!--压缩-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg.bz2
[root@centos01 ~]# bunzip2 initial-setup-ks.cfg.bz2  <!--解压缩-->
[root@centos01 ~]# ls
anaconda-ks.cfg  CentOS-7.4-x86_64-1708.iso  initial-setup-ks.cfg

3, tar-- archive command

archive tar command to make the release archive. The basic syntax is as follows:
Linux directories and files detailed in Advanced Operations

tar command common options are as follows:

  • -c: create a package .tar file format;

  • -x: unpack .tar file format;

  • -v: Output details;

  • -f: indication archive;

  • -p: retain the original file and directory permissions during packaging;

  • -t: View a list of files in the package;

  • -C: Specifies the release unpack target folder;

  • -z: gzip program calls the compression or decompression;

  • -j: call bzip2 compression or decompression program;

Application examples:

[root@centos01 ~]# tar zcvf yun.gz yun/ <!--使用tar命令调用gzip将yun归档为yun.gz-->
yun/
[root@centos01 ~]# ls
1.txt  anaconda-ks.cfg  initial-setup-ks.cfg  www  yun  yun.gz
[root@centos01 ~]# tar zxvf yun.gz -C /usr/src/   
         <!--将压缩文件yun.gz解压缩到/usr/src目录中-->
yun/
[root@centos01 ~]# cd /usr/src/
[root@centos01 src]# ls
debug  kernels  yun
[root@centos01 ~]# tar jcvf yun.bz2 ./yun   
      <!--使用tar命令调用bzip将yun目录数据进行压缩-->
[root@centos01 ~]# tar jxvf yun.bz2 -C /usr/src/ 
    <!--将yun.bz2压缩文件解压缩到/usr/src/目录中-->
./yun/
[root@centos01 ~]# cd /usr/src/
[root@centos01 src]# ls
debug  kernels  yun

4, dd compression and backup command

Options and arguments:

  • if: input file (the original file) can also be a device;

  • of: output file (file backup) may be a device;

  • bs: a block plan (block) size, if not specified, the default is 512Bytes (bytes);

  • count: How many blocks mean.

Application examples:

[root@centos01 ~]# dd if=/dev/zero of=/usr/src/1.iso bs=30M count=10
<!--将/dev/zero文件中的信息复制到/usr/src目录下创建一个1.iso的文件,一次30M,10次-->
记录了10+0 的读入
记录了10+0 的写出
314572800字节(315 MB)已复制,0.212995 秒,1.5 GB/秒
[root@centos01 ~]# cd /usr/src/
[root@centos01 src]# ls
1.iso  debug  kernels

Four, vi text editor

Role 1, the text editor

Create or modify text files, maintaining Linux systems in a variety of profiles.

2, Linux is the most commonly used text editor

  • vi: Unix-like systems the default text editor;

  • vim: enhanced version of the vi editor, the habit has become vi;

3, the operating mode of the vi editor

Command mode, input mode, line mode. Switching between different modes as follows:
Linux directories and files detailed in Advanced Operations

4, common command mode operation

1) Move the cursor

Linux directories and files detailed in Advanced Operations

2) copy, paste, delete

Linux directories and files detailed in Advanced Operations

3) Find the file contents

Linux directories and files detailed in Advanced Operations

4) Undo edit and save and exit

Linux directories and files detailed in Advanced Operations

Basic operation 5, the line mode

1) Save the file and exit the vi editor

Linux directories and files detailed in Advanced Operations

2) Open a new file or read other file content

Linux directories and files detailed in Advanced Operations

3) Replace the contents of the file

Linux directories and files detailed in Advanced Operations

Guess you like

Origin www.linuxidc.com/Linux/2020-01/162123.htm