Review document wildcards and cp, mv

Posted by AShain on Sun, 06 Feb 2022 06:02:54 +0100

1. File wildcard pattern

File wildcard can be used to match multiple files that meet the conditions, which is convenient for batch management of files

Wildcards adopt specific symbols to represent specific meanings. This special symbol is called meta meta character

Common wildcards are as follows:

* Matches zero or more characters but does not match "." The first file is the hidden file
? Match any single character,A Chinese character is also a character
~ Current user home directory
~mage user mage Home directory
. and ~+ Current working directory
~-   Previous working directory
[0-9] Match number range
[a-z] One letter
[A-Z] One letter
[wang] Matches any character in the list
[^wang] Matches characters other than all characters in the list
[^a-z] Matches characters other than all characters in the list

In addition, there are predefined character classes in Linux system: man 7 glob

[:digit:]: Any number, equivalent to 0-9
[:lower:]: Any lowercase letter,express a-z
[:upper:]: Any capital letter,express A-Z
[:alpha:]: Any case letter
[:alnum:]: Any number or letter
[:blank:]: Horizontal white space character
[:space:]: Horizontal or vertical white space characters
[:punct:]: punctuation
[:print:]: Printable character
[:cntrl:]: Control (non printing) characters
[:graph:]: Graphic character
[:xdigit:]: Hexadecimal character

Example:

[root@centos7 data]# ls b[[:lower:]]
ba  bb  bc

 

 

2. How to view hidden files:

[root@centos7 ~]# ls * -d
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos 
You will find that hidden files will not be displayed because*Mismatch starts with.Documents

Then there are other ways:

[root@centos7 ~]# ls .[^.]* -d
.bash_history  .bash_profiley  .cache   .dbus      .ICEauthority  .mozilla
.bash_profile  .bashrc         .config  .esd_auth  .local
This only shows hidden files and does not contain.and..
[root@centos7 ~]# ls -d .*
.   .bash_history  .bash_profiley  .cache   .dbus      .ICEauthority  .mozilla
..  .bash_profile  .bashrc         .config  .esd_auth  .local

Of course, you can also use ls -I (not shown):

[root@centos7 ~]# ls -I '[^.]*' -a
.   .bash_history  .bash_profiley  .cache   .dbus      .ICEauthority  .mozilla
..  .bash_profile  .bashrc

 

 

3. Turn off the firewall:

[root@centos7 ~]# systemctl stop firewalld

Start or not:

[root@centos7 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

 

 

4. The difference between [] and {}:

[root@centos7 data]# ls f[a-l]      
fa  fb  fd  fk  fl
[root@centos7 data]# ls f{a..l}
ls: cannot access fc: No such file or directory
ls: cannot access fe: No such file or directory
ls: cannot access ff: No such file or directory
ls: cannot access fg: No such file or directory
ls: cannot access fh: No such file or directory
ls: cannot access fi: No such file or directory
ls: cannot access fj: No such file or directory
fa  fb  fd  fk  fl

Brackets can match as long as they match, while curly braces match one by one

 

 

5. Difference between [: lower:] and [[: lower:]]

[root@centos7 data]# ls f[:lower:].txt
fe.txt  fl.txt  fo.txt  fr.txt  fw.txt
[root@centos7 data]# ls f[[:lower:]].txt
fa.txt  fc.txt  fe.txt  fg.txt  fi.txt  fk.txt  fm.txt  fo.txt  fq.txt  fs.txt  fu.txt  fw.txt  fy.txt
fb.txt  fd.txt  ff.txt  fh.txt  fj.txt  fl.txt  fn.txt  fp.txt  fr.txt  ft.txt  fv.txt  fx.txt  fz.txt

It can be found that a bracket is only one of the matching brackets, while two brackets are all.

 

 

6. Practice

1) Displays all files or directories in the / var directory that start with i, end with a lowercase letter, and have at least one digit in the middle

ls -d /var/i*[[:digit:]]*[[:lower:]]

2) Displays files or directories that begin with any digit and end with a non digit in the / etc directory

ls -d /etc/[0-9]*[^0-9]

3) Only non hidden file directories under / etc are displayed

ls -d /etc/*

 

 

7.cp: use the cp (copy) command to copy files or directories

Format:
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

Common options:

-i If the target already exists, prompt whether to overwrite it before overwriting
-n Do not overwrite, pay attention to the order of the two
-r, -R Recursively copy the directory and all the contents inside
-a Archive, equivalent to-dR --preserv=all,Commonly used for backup functions
-d --no-dereference --preserv=links Do not copy the original file, only copy the link name
--preserv[=ATTR_LIST]
 mode: jurisdiction
 ownership: Main genus group
 timestamp:
 links
 xattr
 context
 all
-p equivalent--preserv=mode,ownership,timestamp
-v --verbose
-f --force
-u --update Copy only files where the source is newer than the destination or where the destination does not exist
-b The target exists. Backup before overwriting. The default form is filename~ ,Keep only the most recent backup
--backup=numbered The target exists. Before overwriting, backup and add a digital suffix in the form of filename.~#~ ,Can keep more
 Versions

 

 

 

8.cp example:

1) If f1 exists, an additional file of f1 ~ will be backed up

[root@centos7 data]# touch fo
[root@centos7 data]# touch f1
[root@centos7 data]# cp fo f1 --backup
[root@centos7 data]# ls
f1 f1~ fo

2) cp -i will prompt whether to overwrite

[root@centos7 data]# cp -i fo f1
cp: overwrite 'f1'? y

3) cp -d can copy links

[root@centos7 data]# cp -d grab.cfg1 4ll
[root@centos7 data]# ll 4ll
lrwxrwxrwx. 1 root root 2 Feb  6 12:38 4ll -> f1

4) CP all rights reserved - R

 

5)cp -a  = cp -dR --preserve=all

 

 

9.cp only general files can be copied

 

 

10.mv: move and rename files

mv command can move and rename files or directories

Moving data in the same partition is fast: the data location has not changed

Moving data in different partitions is relatively slow: the data location has changed

Format:

mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE.

Common options:

i interactive
-f force
-b Target exists, backup before overwriting

 

 

11. rename can be used to modify file names in batches

Format:

rename [options] <expression> <replacement> <file>...

example:

#Add for all conf files bak suffix:
[root@centos7 data]# ls
fa.txt  fc.txt  fe.txt  fg.txt  fi.txt  fk.txt  fm.txt  fo.txt  fq.txt  fs.txt  fu.txt  fw.txt  fy.txt
fb.txt  fd.txt  ff.txt  fh.txt  fj.txt  fl.txt  fn.txt  fp.txt  fr.txt  ft.txt  fv.txt  fx.txt  fz.txt
[root@centos7 data]# rename 'txt' 'txt.bak' f*
[root@centos7 data]# ls
fa.txt.bak  fe.txt.bak  fi.txt.bak  fm.txt.bak  fq.txt.bak  fu.txt.bak  fy.txt.bak
fb.txt.bak  ff.txt.bak  fj.txt.bak  fn.txt.bak  fr.txt.bak  fv.txt.bak  fz.txt.bak
fc.txt.bak  fg.txt.bak  fk.txt.bak  fo.txt.bak  fs.txt.bak  fw.txt.bak
fd.txt.bak  fh.txt.bak  fl.txt.bak  fp.txt.bak  ft.txt.bak  fx.txt.bak
#Remove all bak suffixes: [root@centos7 data]# rename
'.bak' '' f* [root@centos7 data]# ls fa.txt fc.txt fe.txt fg.txt fi.txt fk.txt fm.txt fo.txt fq.txt fs.txt fu.txt fw.txt fy.txt fb.txt fd.txt ff.txt fh.txt fj.txt fl.txt fn.txt fp.txt fr.txt ft.txt fv.txt fx.txt fz.txt

 

 

12.rm delete file

Use the rm command to delete files

Note: this command is very dangerous and should be used with caution. It is recommended to use mv instead of rm

Format:

rm [OPTION]... FILE...

Common options:

i interactive
-f Force deletion
-r recursion
--no-preserve-root delete/ 

 

 

2022-2-6 12: 50