linux learning journey (3) & delete tips

Posted by chombone on Wed, 12 Jun 2019 19:58:14 +0200

System structure of linux

1.linux is an inverted tree structure

2. Everything in Linux is a file

  • These files are in the system top-level directory "/"/ is the root directory / directory below the secondary directory, these directories are automatically established when the system is installed.
  • The role of secondary directories:

Addressing of documents:

Absolute path-relative path

Absolute path:
The real location of the file on the system, and the name of the file begins with "/"
Relative path:
Relative to the abbreviation of a name in the current location, the name does not start with / and the name automatically adds the value displayed by pwd

In fact, the difference between absolute path and relative path is only that the reference points used to describe directory path are different. For all files, the reference point of root directory is the same for all files. Therefore, using the path description method with the reference point of root directory is called absolute path.

  • The following are the special symbols used to establish the path and the meanings they represent:
// Current directory.
.... / / / / Represents the previous level of directory.
/// The root directory.
  • Now I want to go from / home/kiosk to / home/kiosk/Desktop:

** Among them, the first is the relative path, but in the production environment, it is often required to be accurate, so the allocation and so on should be marked with absolute path.

* To facilitate the explanation of the following commands, here we introduce the concept of general regular expressions.

(There will be a detailed explanation of the special regular expressions in the following three swordsman commands.)

  • Regular expression is a logical formula for string operation, which is to form a "regular string" by using pre-defined specific characters and combinations of these specific characters. This "regular string" is used to express a filtering logic for strings.
    Given a regular expression and another string, we can achieve the following objectives:
    1. Whether a given string conforms to the filter logic of a regular expression (called "matching");
    2. We can get the specific parts we want from the string through regular expressions. Regular expressions are characterized by:
    3. Flexibility, logic and function are very strong.
    4. Complex control of strings can be achieved quickly in a very simple way.
    5. For those who have just come into contact, it is more obscure and difficult to understand. Since regular expressions are mainly used for text, they are used in various text editors, ranging from EditPlus, a well-known editor, to Microsoft.
      Large editors such as Word and Visual Studio can use regular expressions to process text content.
*       ##matching0To any character
?       ###Matching a single character
[[:alpha:]] ###Match a single letter
[[:lower:]] ###Matching a single lowercase letter
[[:upper:]] ###Match a single capital letter
[[:digit:]] ###Match a single number
[[:alnum:]] ###Match a single number or letter
[[:punct:]] ###Matching a single symbol
[[:space:]] ###Matching a single space

{}Expressing nonexistence or existence.
{1..9}      ###1-9
{a..f}      ###a-f
{1,3,5}     ###135
{a,c,e}     ###a c e
{1..3}{a..c}    ###1a 2a 3a 2a 2b 2c 3a 3b 3c

[]Expressing existence
[a-C]       ###aA bB cC
[a-c]       ###aA or bB or c
[1-3]       ###1perhaps2perhaps3
[145]       ###1perhaps4perhaps5
[^abc]|[!abc]   ###Except a and except b and except c

~       ###Current User Home Directory
~username   ###Specified User Home Directory
~+      ###current directory
~-      ###The directory before the current directory

.       ###current directory
..      ###The upper level of the current directory

Simple commands for file management

1.touch builds or * modifies file timestamps

  • Modify the file timestamp: Usually the connection between the file and the server takes the same time. At this time, because of the different time, the touch command is used to change the file timestamp to achieve a similar trick system to complete the connection.

Format: touch [option]... [Document]

Parameters:

##Build 12 files WESTOS_classX_linuxY with one command (X ranges from 1 to 2, Y ranges from 1 to 6)

[root@server77 study]# Touch WESTOS_class {1.2}_linux {1.6}// Establish 12 files
[root@server77 study]# ls // / View the files in the current directory
WESTOS_class1_linux1  WESTOS_class1_linux5  WESTOS_class2_linux3
WESTOS_class1_linux2  WESTOS_class1_linux6  WESTOS_class2_linux4
WESTOS_class1_linux3  WESTOS_class2_linux1  WESTOS_class2_linux5
WESTOS_class1_linux4  WESTOS_class2_linux2  WESTOS_class2_linux6
//Change the timestamp of the file
root@localhost test]# ll                                 //View the file attributes in the directory
-rw-r--r-- 1 root root    0 10-28 16:01 lognew.log
-rw-r--r-- 1 root root    0 10-28 14:48 log.log
[root@localhost test]# touch -r log.log log2012.log      //Change timestamp
[root@localhost test]# ll
-rw-r--r-- 1 root root    0 10-28 16:01 lognew.log      //As you can see, the time attribute of the file has been changed
-rw-r--r-- 1 root root    0 10-28 14:48 log.log

2.mkdir Establishment of Directories

Usage: mkdir directory # create directory
IMPORTANT PARAMETER: - p # Superior Directory does not exist automatically

mkdir -p a/b/c/d      //Direct creation of directory a and its subdirectories b, B subdirectories c, C subdirectories d

3.rm Delete Directories or Files

  • rm is a common command. Its function is to delete one or more files or directories in a directory. It can also delete all files and subdirectories under a directory. For linked files, only deleted links, the original files remain unchanged.

    RM is also a dangerous command. Be careful when using it, especially for beginners, otherwise the whole system will be destroyed by this command (such as executing rm * -rf under /(root directory). So, before we execute rm, we'd better confirm which directory we are in, what we want to delete, and keep a high level of sober mind when we operate.

Usage: rm [parameter] [object]...

In general, rm directly adds objects and prompts appear:

Important parameters:
    - f # Forced deletion does not prompt, only delete files alone
    - r # Recursive Delete Directory

rmdir //Deleting empty directories is similar to rm

// rm commands are usually aliased, and the command line enters alias to view

alias  rm='rm -i  --color=auto'     // - i: Ignore case and case

tips (Important!!!) :

** Through the author's consultation with Daniel, it is found that RM command is a very experiential command. Generally, novices will add parameters fr directly when using rm, which is a very bad habit in linux. There are also many so-called "Daniel falls down" in this habit, must be used less!!!

** The most correct method in production environment is to replace rm with mv, move files to / tmp or similar trash directory, and delete them once when the TMP is full, so as to reduce errors. Or at intervals, find that the disappearance of this file has no impact, and then mass delete the files before this period of time.

1. and xargs Collocation usage:

    find  -type  f  -mtime  +  15  |  xargs  rm  -f              //Delete files from directories 15 days ago. Use this

    find  -type  d  -mtime  +30   |  xargs  rm  -f               //Delete directories 30 days ago ** Use cautiously

2.Use find Of -exec

    find  /logs  -type  f  -mtime  +5  -exec  rm  {}  \        //Find and delete files that changed 5 days ago in the / logs directory

4.cd switch working directory

cd directory    ##Switch working directory to specified directory

cd -        ##Switch the working directory to the previous directory

cd ~       ##Switch to your home directory
cd ~usernmae   ##Switch to the specified user's home directory

cd ..       ##Enter the parent directory of the current directory

5.ls lists directory or file information

ls              ##If there is no target later, the default target is the current directory
ls direcory|filename        ##List the contents of a file or directory
ls -d direcotry         ##List the catalog itself
ls -l filename|dorectory    ##List the attributes of the contents of a file or directory
ls -ld directory        ##List the properties of the directory itself
ls -a               ##Displays everything in the directory, including hidden files beginning with ".".
ls -R               ##Content in the Catalog

6.cp File Replication

Function: Copy the source file to the target file, or copy multiple source files to the target directory.

  • cp is the process of creating new files, that is, copying - > creating new files. The permissions of copied files are equivalent to those of re-establishing files.
cp file file1 file2 ... directory   ###Copy file file 1 file 2 to directory
cp file test                ###Create test file template as file
cp -r directory direcotry1      ###duplicate catalog
##cp is a creation process permission is the basic creation process, add parameter - p: do not change permission replication


- Other uses

//  Parameter - f, --force removes and retries the target file if it cannot be opened
[root@foundation77 ~]# \cp -f /mnt/test.txt   /tmp           //No prompt
[root@foundation77 ~]# /bin/cp -f /mnt/test.txt  /tmp/       //No prompt
[root@foundation77 ~]# cp -f /mnt/test.txt  /tmp/            //There are hints
cp: overwrite '/tmp/test.txt'? 
[root@foundation77 ~]# cp  /mnt/test.txt  /tmp/              //There are hints
cp: overwrite '/tmp/test.txt'? 
[root@foundation77 ~]# 

// The cp command is usually aliased, and the command line enters alias to view it.

alias  cp='cp -i  --color=auto'     // - i: Ignore case and case

7.mv move or rename

  • The mv of the same disk is renamed, and the mv of different disks is a copy and delete process.
  • mv replication and deletion process (actually referring to how the underlying layer works): replication still does not take privileges in different partitions
mv file file1   direcotry       ##Move file file 1 to directory
mv Existing files do not exist       ##rename file
mv westos/linux .           ##Move linux in westos to the current directory. Represents the current directory

// The mv command is usually aliased, and the command line enters alias to view it.

alias  mv='mv -i  --color=auto'     // - i: Ignore case and case

Topics: Linux Attribute less