Learning Linux common instructions (Part 1): file processing instructions

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it

preface

Environment and related software: VMWare 14, CentOS 7, SecureCRT.

Tip: the following is the main content of this article. The following cases can be used for reference

1, View Linux version information

Taking the environment in which this article is located as an example, you can view the installed version information of CentOS under linux through the following methods:

  1. Linux view kernel information of current operating system version uname
  • Show all information uname -a
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

-Show help uname --help

[root@localhost ~]# uname --help
 Usage: uname [option]...
Output a set of system information. If you do not follow the option, it is considered to be attached only-s Options.

  -a, --all                     Output all information in the following order. Among them, if-p and
                                -i If the detection result of is unknown, it is omitted:
  -s, --kernel-name             Output kernel name
  -n, --nodename                Output the hostname on the network node
  -r, --kernel-release          Output kernel release number
  -v, --kernel-version          Output kernel version
  -m, --machine         Output the hardware architecture name of the host
  -p, --processor               Output processor type or"unknown"
  -i, --hardware-platform       Output hardware platform or"unknown"
  -o, --operating-system        Output operating system name
      --help            Display this help message and exit
      --version         Display version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Please to<http://translationproject. org/team/zh_ CN. HTML > report translation errors of uname
 To get a complete document, run: info coreutils 'uname invocation'
  • Displays the operating system name uname -s or uname --sysname
[root@localhost ~]# uname -s
Linux

[root@localhost ~]# uname --sysname
Linux
  • Displays the computer type uname -m or uname --machine
[root@localhost ~]# uname -m
x86_64

[root@localhost ~]# uname --machine
x86_64
  1. Linux view the current operating system version cat /proc/version
[root@localhost ~]# cat /proc/version
Linux version 3.10.0-1160.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Mon Oct 19 16:18:59 UTC 2020
  1. View the current operating system release information of linux version cat /etc/issue or cat / etc / CentOS release
[root@localhost ~]# cat /etc/issue
\S
Kernel \r on an \m

[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
  1. Command LSB for viewing system version information_ release -a
    (command not found is prompted when using the command. You need to execute the installation command Yum install RedHat LSB - y before entering)
[root@localhost ~]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.9.2009 (Core)
Release:        7.9.2009
Codename:       Core

2, File processing command

(1) Command format and directory processing commands

1. Command format

Command format: Command [- options] [parameters]
explain:
1) Individual commands do not follow this format
2) When there are multiple options, they can be written together
3) Simplified and full options - a equals – all

2. Display directory file: ls

Command English original meaning: list
Command path: / bin/ls
Execution Authority: all users
Function Description: display directory file
Syntax: ls option [- ald] [file or directory]
-a displays all files, including hidden files
-l detailed information display
-d view directory properties

  1. View files in the current directory
[root@localhost ~]# ls
anaconda-ks.cfg

/root/anaconda-ks.cfg: record the option information set during installation in the format of Kickstart configuration file (this file is a configuration file for unattended installation dependency).

  1. View all files in the current directory (including hidden files) ls -a
[root@localhost ~]# ls -a
.  ..  anaconda-ks.cfg  .bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .lesshst  .tcshrc
  1. View files in the specified directory/
[root@localhost ~]# ls /
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

/: represents the root directory

  1. View the long list mode of all files in the current directory (details) ls -la
[root@localhost ~]# ls -la
 Total consumption 32
dr-xr-x---.  2 root root  151 1 September 13-19:40 .
dr-xr-xr-x. 17 root root  224 1 August 2:26 ..
-rw-------.  1 root root 1421 1 August 2:26 anaconda-ks.cfg
-rw-------.  1 root root   35 1 June 13-18:27 .bash_history
-rw-r--r--.  1 root root   18 12 September 29, 2013 .bash_logout
-rw-r--r--.  1 root root  176 12 September 29, 2013 .bash_profile
-rw-r--r--.  1 root root  176 12 September 29, 2013 .bashrc
-rw-r--r--.  1 root root  100 12 September 29, 2013 .cshrc
-rw-------.  1 root root   45 1 September 13-19:40 .lesshst
-rw-r--r--.  1 root root  129 12 September 29, 2013 .tcshrc

With - rw-r – R - 1 root 18 December 29, 2013 bash_ For example, logout is explained as follows:

- rw- r-- r–.1rootroot18December 29, 2013.bash_logout
(see the explanation below)Reference countownerGroupfile sizeLast modification timefile name
  • The following figure is a simple explanation of the first column:
  1. Displays details of the specified directory itself. ls -ld /etc
[root@localhost ~]# ls -ld /etc
drwxr-xr-x. 83 root root 8192 1 June 16-18:12 /etc

/Etc /: the location where the configuration file is saved. The configuration files of all services using the default installation method (rpm installation) in the system are saved in this directory, such as user account and password, service startup script, configuration files of common services, etc.

  1. Make the file size display more user-friendly ls -lh
[root@localhost ~]# ls -lh
 Total consumption 4.0K
-rw-------. 1 root root 1.4K 1 August 2:26 anaconda-ks.cfg
  1. Displays the i node of the current directory file (that is, the unique ID of the file)
[root@localhost ~]# ls -i
33574978 anaconda-ks.cfg

3. Create a new directory: mkdir

make directories
Command path: / bin/mkdir
Execution Authority: all users
Syntax: mkdir -p [directory name]
Function Description: create a new directory
-p recursive creation

  1. Create a directory day1 mkdir /tmp/day1 under the tmp directory
[root@localhost ~]# mkdir /tmp/day1
[root@localhost ~]# ls -l
 Total consumption 4
-rw-------. 1 root root 1421 1 August 2:26 anaconda-ks.cfg
[root@localhost ~]# ls -l /tmp
 Total consumption 4
drwxr-xr-x. 2 root root   6 1 June 16-20:46 day1
-rwx------. 1 root root 836 1 August 2:26 ks-script-_KSXlM
drwx------. 3 root root  17 1 May 15-22:49 systemd-private-0dd6a9e7e1024e15ba73db310090c201-chronyd.service-vyFtyd
drwx------. 3 root root  17 1 April 13:36 systemd-private-86a69b4ef3cf440e9e3cd345c20f1569-chronyd.service-jVuvd3
drwx------. 3 root root  17 1 June 16-15:22 systemd-private-d8000b3920d44f71b59a0531fec19d10-chronyd.service-n1KqK5
drwx------. 3 root root  17 1 June 16-18:12 systemd-private-edafe6e8a1284b37be6c9a30e921f73f-chronyd.service-3z0bxa
drwx------. 2 root root   6 1 May 15-22:49 vmware-root_664-2722697761
drwx------. 2 root root   6 1 June 16-15:22 vmware-root_666-2731021219
drwx------. 2 root root   6 1 June 16-18:12 vmware-root_667-3980363901
drwx------. 2 root root   6 1 April 13:36 vmware-root_673-3988556249
drwx------. 2 root root   6 1 August 2:27 vmware-root_697-3988163015
-rw-------. 1 root root   0 1 August 2:22 yum.log

/tmp /: temporary directory. The directory where the system stores temporary files, which can be accessed and written by all users. We suggest that important data cannot be saved in this directory. It is best to empty this directory every time you start up.

  1. Create MKDIR recursively - P / TMP / Day2 / test1
[root@localhost ~]# mkdir -p /tmp/day2/test1
[root@localhost ~]# ls /tmp/day2            
test1
  1. Create multiple directories at the same time
    mkdir /tmp/day1/test1 /tmp/day1/test2
[root@localhost ~]# mkdir /tmp/day1/test1 /tmp/day1/test2
[root@localhost ~]# ls /tmp/day1
test1  test2

4. Switch directory and display current directory: cd and pwd

  • Command English original meaning: change directory
    Command path: shell built-in command
    Execution Authority: all users
    Syntax: cd [directory]
    Function Description: switch directory
  • Original English meaning of the command: print working directory
    Command path: / bin/pwd
    Execution Authority: all users
    Syntax: pwd
    Function Description: displays the current directory
  1. Switch to the specified directories cd /tmp and pwd
[root@localhost ~]# cd /tmp
[root@localhost tmp]# pwd
/tmp
  1. Switch to the previous directory cd
[root@localhost tmp]# cd ..
[root@localhost /]# pwd    
/

5. Delete the empty directory: rmdir (understand)

English original meaning of the command: remove empty directories
Command path: / bin/rmdir
Execution Authority: all users
Syntax: rmdir [directory name]
Function Description: delete empty directory

[root@localhost ~]# ls /tmp/day1
test1  test2
[root@localhost ~]# rmdir /tmp/day1/test1
[root@localhost ~]# ls /tmp/day1         
test2

6. Copy files or directories: cp

Command English original meaning: copy
Command path: / bin/cp
Execution Authority: all users
Syntax: cp -rp [original file or directory] [destination directory]
-r copy directory
-p keep file properties
Function Description: copy files or directories

  1. Copy the specified file to the destination directory
[root@localhost ~]# cp /etc/locale.conf /tmp    
[root@localhost ~]# ls /tmp
day1
day2
ks-script-_KSXlM
locale.conf
systemd-private-0dd6a9e7e1024e15ba73db310090c201-chronyd.service-vyFtyd
systemd-private-86a69b4ef3cf440e9e3cd345c20f1569-chronyd.service-jVuvd3
systemd-private-d8000b3920d44f71b59a0531fec19d10-chronyd.service-n1KqK5
systemd-private-edafe6e8a1284b37be6c9a30e921f73f-chronyd.service-3z0bxa
vmware-root_664-2722697761
vmware-root_666-2731021219
vmware-root_667-3980363901
vmware-root_673-3988556249
vmware-root_697-3988163015
yum.log
  1. Copy directory cp -r
[root@localhost ~]# cp -r /tmp/day1 /root
[root@localhost ~]# ls
anaconda-ks.cfg  day1
  1. Copy multiple files to the destination directory
[root@localhost ~]# cp /etc/yum.conf /etc/sudo.conf /tmp
[root@localhost ~]# ls -l /tmp
 Total consumption 16
drwxr-xr-x. 3 root root   19 1 June 16-21:10 day1
drwxr-xr-x. 3 root root   19 1 June 16-20:52 day2
-rwx------. 1 root root  836 1 August 2:26 ks-script-_KSXlM
-rw-r--r--. 1 root root   19 1 June 16-21:18 locale.conf
-rw-r-----. 1 root root 1786 1 June 16-21:31 sudo.conf
drwx------. 3 root root   17 1 May 15-22:49 systemd-private-0dd6a9e7e1024e15ba73db310090c201-chronyd.service-vyFtyd
drwx------. 3 root root   17 1 April 13:36 systemd-private-86a69b4ef3cf440e9e3cd345c20f1569-chronyd.service-jVuvd3
drwx------. 3 root root   17 1 June 16-15:22 systemd-private-d8000b3920d44f71b59a0531fec19d10-chronyd.service-n1KqK5
drwx------. 3 root root   17 1 June 16-18:12 systemd-private-edafe6e8a1284b37be6c9a30e921f73f-chronyd.service-3z0bxa
drwx------. 2 root root    6 1 May 15-22:49 vmware-root_664-2722697761
drwx------. 2 root root    6 1 June 16-15:22 vmware-root_666-2731021219
drwx------. 2 root root    6 1 June 16-18:12 vmware-root_667-3980363901
drwx------. 2 root root    6 1 April 13:36 vmware-root_673-3988556249
drwx------. 2 root root    6 1 August 2:27 vmware-root_697-3988163015
-rw-r--r--. 1 root root  970 1 June 16-21:31 yum.conf
-rw-------. 1 root root    0 1 August 2:22 yum.log
  1. Copy directory and keep directory properties * cp -rp
[root@localhost ~]# ls -l /tmp/day1
 Total consumption 0
drwxr-xr-x. 2 root root 6 1 June 16-20:55 test2
[root@localhost ~]# cp -rp /tmp/day1/test2 /root
[root@localhost ~]# ls -l  /root                
Total consumption 4
-rw-------. 1 root root 1421 1 August 2:26 anaconda-ks.cfg
drwxr-xr-x. 3 root root   19 1 June 16-21:25 day1
drwxr-xr-x. 2 root root    6 1 June 16-20:55 test2
  1. Copy the directory and rename the copied directory
[root@localhost ~]# cp -rp /tmp/day2/test1 /root/learn2
[root@localhost ~]# ls -l  /root                       
Total consumption 4
-rw-------. 1 root root 1421 1 August 2:26 anaconda-ks.cfg
drwxr-xr-x. 3 root root   19 1 June 16-21:25 day1
drwxr-xr-x. 2 root root    6 1 June 16-20:55 learn1
drwxr-xr-x. 2 root root    6 1 June 16-20:52 learn2
drwxr-xr-x. 2 root root    6 1 June 16-20:55 test2

7. Cut file and rename: mv (operate with caution)

Command English original meaning: move
Command path: / bin/mv
Execution Authority: all users
Syntax: mv [original file or directory] [target directory]
Function Description: cutting files and renaming

  1. Cut a directory or file
[root@localhost ~]# ls /root
anaconda-ks.cfg  day1  learn1  learn2  test2
[root@localhost ~]# ls /tmp
day1
day2
ks-script-_KSXlM
locale.conf
sudo.conf
systemd-private-0dd6a9e7e1024e15ba73db310090c201-chronyd.service-vyFtyd
systemd-private-86a69b4ef3cf440e9e3cd345c20f1569-chronyd.service-jVuvd3
systemd-private-d8000b3920d44f71b59a0531fec19d10-chronyd.service-n1KqK5
systemd-private-edafe6e8a1284b37be6c9a30e921f73f-chronyd.service-3z0bxa
vmware-root_664-2722697761
vmware-root_666-2731021219
vmware-root_667-3980363901
vmware-root_673-3988556249
vmware-root_697-3988163015
yum.conf
yum.log
[root@localhost ~]# mv /tmp/day2 /root
[root@localhost ~]# ls /root          
anaconda-ks.cfg  day1  day2  learn1  learn2  test2
[root@localhost ~]# ls /tmp           
day1                                                                     vmware-root_664-2722697761
ks-script-_KSXlM                                                         vmware-root_666-2731021219
locale.conf                                                              vmware-root_667-3980363901
sudo.conf                                                                vmware-root_673-3988556249
systemd-private-0dd6a9e7e1024e15ba73db310090c201-chronyd.service-vyFtyd  vmware-root_697-3988163015
systemd-private-86a69b4ef3cf440e9e3cd345c20f1569-chronyd.service-jVuvd3  yum.conf
systemd-private-d8000b3920d44f71b59a0531fec19d10-chronyd.service-n1KqK5  yum.log
systemd-private-edafe6e8a1284b37be6c9a30e921f73f-chronyd.service-3z0bxa
  1. Rename a directory or file
[root@localhost ~]# ls /root          
anaconda-ks.cfg  day1  day2  learn1  learn2  test2
[root@localhost ~]# mv test2 test1
[root@localhost ~]# ls /root      
anaconda-ks.cfg  day1  day2  learn1  learn2  test1

8. Delete files or directories: rm (caution)

English original meaning of the command: remove
Command path: / bin/rm
Execution Authority: all users
Syntax: rm -rf [file or directory]
-r delete directory
-f enforcement
Function Description: delete file

  1. Delete file rm
[root@localhost ~]# cp /etc/yum.conf /root
[root@localhost ~]# ls /root
anaconda-ks.cfg  day1  day2  learn1  learn2  test1  yum.conf
[root@localhost ~]# rm yum.conf 
rm: Delete normal file "yum.conf"?y
[root@localhost ~]# ls /root    
anaconda-ks.cfg  day1  day2  learn1  learn2  test1

Where y represents yes and n represents No. In addition, there is no recycle bin in Linux, which is difficult to recover.
2. Forced deletion of file rm -f

[root@localhost ~]# cp /etc/yum.conf /root    
[root@localhost ~]# rm -f yum.conf           
[root@localhost ~]# ls /root              
anaconda-ks.cfg  day1  day2  learn1  learn2  test1
  1. Force delete directory rm -rf
[root@localhost ~]# ls /root              
anaconda-ks.cfg  day1  day2  learn1  learn2  test1
[root@localhost ~]# rm -rf learn1   
[root@localhost ~]# ls /root      
anaconda-ks.cfg  day1  day2  learn2  test1

(2) File processing command

9. Create an empty file: touch

Command path: / bin/touch
Execution Authority: all users
Syntax: touch [file name]
Function Description: create an empty file

  1. Creates an empty file in the specified directory
[root@localhost ~]# touch /tmp/study.list /tmp/"program files"
[root@localhost ~]# ls /tmp
day1              systemd-private-0dd6a9e7e1024e15ba73db310090c201-chronyd.service-vyFtyd  vmware-root_667-3980363901
ks-script-_KSXlM  systemd-private-86a69b4ef3cf440e9e3cd345c20f1569-chronyd.service-jVuvd3  vmware-root_673-3988556249
locale.conf       systemd-private-d8000b3920d44f71b59a0531fec19d10-chronyd.service-n1KqK5  vmware-root_697-3988163015
program files     systemd-private-edafe6e8a1284b37be6c9a30e921f73f-chronyd.service-3z0bxa  yum.log
study.list        vmware-root_664-2722697761
sudo.conf         vmware-root_666-2731021219

Note: to create a file name with spaces, you need to add "".

10. Display file content: cat (only suitable for viewing files with less content)

Command path: / bin/cat
Execution Authority: all users
Syntax: cat [file name]
Function Description: display file content
-n displays the line number

  1. View file contents (forward)
[root@localhost ~]# cat -n /etc/issue
     1  \S
     2  Kernel \r on an \m
     3

11. Display file content (reverse listing): tac

Command path: / usr/bin/tac
Execution Authority: all users
Syntax: tac [file name]
Function Description: display file content (reverse listing)

  1. View file contents (reverse)
[root@localhost ~]# tac /etc/issue   

Kernel \r on an \m
\S

12. Paginated display of file content: more (suitable for viewing files with many contents) (important)

Command path: / bin/more
Execution Authority: all users
Syntax: more [file name]
(space) or f page turning
(Enter) wrap
Q or Q exit
Function Description: displays file contents in pages
Example: $more /etc/services

13. Paginated display of file content (page up): less (more powerful than more)

Command path: / usr/bin/less
Execution Authority: all users
Syntax: less [file name]
Command path: / usr/bin/less execution permission: all users
Syntax: less [file name]
(space) or f page turning
(Enter) wrap
Q or Q exit
PageUp page up
↑ previous line
/Keyword + Enter search keyword
n view next keyword (next)
Function Description: display the file contents in pages (page up)

14. Display the first few lines of the file: head

Command path: / usr/bin/head
Execution Authority: all users
Syntax: head [file name]
Function Description: displays the first few lines of the file
-n specifies the number of rows

  1. Displays the first few lines of the file
[root@localhost ~]# head -n 5 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10

Note: when no line number is specified, the first ten lines are displayed by default.

15. Display the last few lines of the file: tail (see head for usage)

Command path: / usr/bin/tail
Execution Authority: all users
Syntax: tail [file name]
Function Description: displays the following lines of the file
-n specifies the number of rows
-f dynamically display the contents at the end of the file

(3) Link command

16. Generate link file: ln

Command English original meaning: link
Command path: / bin/ln
Execution Authority: all users
Syntax: ln -s [original file] [target file]
-s create a soft link
Function Description: generate linked file

summary

Tip: here is a summary of the article:
Unfinished to be continued...

Keywords: Linux Operation & Maintenance server

Added by inVINCEable on Wed, 19 Jan 2022 16:20:37 +0200