Linux file management and Bash features

Time:2019-12-10

File management commands on Linux

Directory management command:

mkdir:make directories
mkdir [OPTION]… DIRECTORY…
-P: automatically create the parent directory on demand;
-V: verbose, showing the detailed process;
-M mode: give permission directly;
Note: the path base name is the action object of the command; the path before the base name must exist;

rmdir:remove empty directories
rmdir [OPTION]… DIRECTORY…
-P: after deleting a directory, if its parent directory is empty, it will be deleted at the same time;
-V: display process;

File management command

CP command: copy
Source file; target file;
Single source replication: CP [option] [-T] SOURCE DEST
Multi source replication: CP [option] SOURCE… DIRECTORY
        cp [OPTION]… -t DIRECTORY SOURCE…
Single source replication: CP [option] [-T] SOURCE DEST
If dest does not exist: create this file in advance and copy the data stream of the source file to dest;
If dest exists:
If dest is a non directory file: overwrite the target file;
If dest is a directory file: first create a file with the same name as the source file in dest directory, and copy its data flow;
Multi source replication: CP [option] SOURCE… DIRECTORY
        cp [OPTION]… -t DIRECTORY SOURCE…
If dest does not exist: error;
If dest exists:
If dest is a non directory file: error;
If dest is a directory file: copy each file to the target directory separately, and keep the original name;
Common options:
– I: interactive copy, i.e. remind the user to confirm before overwriting;
– F: force the target file to be overwritten;
– R, – R: recursively copy directory;
– D: copy the symbolic link file itself, not the source file it points to;
– A: – Dr – preserve = all, archive, for archiving;
      –preserv=
Mode: permissions
Ownership: owner and group
Timestamps: timestamps
Context: security label
Xattr: extended properties
Links: symbolic links
All: all the above properties

MV command: move
  mv [OPTION]… [-T] SOURCE DEST
  mv [OPTION]… SOURCE… DIRECTORY
  mv [OPTION]… -t DIRECTORY SOURCE..
Common options:
– I: interactive;
    -f:force

RM command: remove
  rm [OPTION]… FILE…
Common options:
    -i:interactive
    -f:force
    -r: recursive

Characteristics of Bash

Command execution status results
Status result of command execution:
Bash outputs this result through the status return value:
Success: 0
Failed: 1-255
Command line expansion
~: automatically expand to the user’s home directory or the specified user’s home directory;
{}: can host a comma separated list of paths, and can expand it into multiple paths;
  
Create: a UUC, a uud, B UUC, B uud in / tmp directory
  touch {a,b}_{c,d}  
Create the
  mkdir -pv   /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-      scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}
File metadata

File metadata mainly includes permission, size, inode, owner, group, access time, modify time, change time, etc
The metadata of the file can be viewed with the stat command.
The touch command can modify the time stamp:
  touch [OPTION]… FILE…
– C: do not create when the specified file path does not exist;
– A: modify access time only;
– M: modify time only;
    -t STAMP
     [[CC]YY]MMDDhhmm[.ss]

Command alias & refers to the execution result of a command

You can define command aliases in the form of alias name = value.

Execution result of reference command:
  $(COMMAND)
Or ` command`

Practice

Display all files or directories in the / var directory that start with L, end with a lowercase letter, and have at least one digit in the middle (other characters are allowed).
ls -ld /var/l*[0-9]*[a-z]

Display the files or directories in / etc directory that start with any number and end with non number.
ls -ld /etc/[0-9]*[^[:digit:]]

Display the files or directories in the / etc directory that start with a non letter, followed by a letter and any other characters of any length.
ls -ld /etc/[^[:alpha:]][a-z]*

In the / tmp directory, create a file that starts with tfile, followed by the current date and time. The file name is as follows: tfile-2016-05-27-09-32-22.
touch /tmp/tfile-`date +%Y-%m-%d-%H-%M-%S`

Copy all files or directories in the / etc directory that start with P and end with non number to the / TMP / mytest1 directory.
cp -r /etc/p*[^0-9] /tmp/mytest1/

Copy all files or directories ending in. D in / etc directory to / TMP / mytest2 directory.
cp -r /etc/*.d /tmp/mytest2/

Copy all files in the / etc / directory that start with L or m or N and end with. Conf to the / TMP / mytest3 directory.
cp -r /etc/[lmn]*.conf /tmp/mytest3/