This paper introduces the usage of CP and MV with {,} in the shell in Linux

Time:2022-1-9

You often see the following commands on blogs or forums: brace expansion {} shell is extended with braces


  cp /etc/httpd/httpd.{,.bakup}

Or


  mv resume{z,}.doc

So what does it mean in the UINX / Linux} shell command? What does it do?

{} has no practical meaning, but it can often be used to generate various groups as brace expansion (brace extension or curly bracket extension). The following is a translation from GNU / bash man page:

Brace expansion is a mechanism used to randomly generate string combinations. This mechanism is similar to the extension of a file, but there is no need for a responsive file. The brace expansion mode is an optional preamble followed by a series of comma separated strings contained in a pair of curly braces, followed by an optional postscript (print programming language). Preamble (leading character) is added before each string in curly braces, and postscript (print programming language) is appended to each result string and expanded from left to right. Curly bracket extensions can be nested. The result of the extended string is not sorted; It retains the order from left to right.

 

1. If necessary, enter the following command in the terminal:


  echo foo{1,2,3}.txt

The output is as follows:


foo1.txt foo2.txt foo3.txt

 

2. You can also use the following additional examples to “create” a parameter in the command to save input time and improve work efficiency:
    echo file.txt{,.bak}


  echo file-{a..d}.txt

  echo mkdir -p /apache-jail/{usr,bin,lib64,dev}

  echo cp httpd.conf{,.backup}

  echo mv delta.{txt,doc}

 

3. You can use brace expansion (brace extension or curly bracket extension) to copy, rename files, backup files, or create directories. In the following habitually used example, use file1 Txt file 2 txt. Bak, enter:


cp  -v file1.txt file1.txt.bak

 

If you use brace expansion to save time, enter:


cp  -v  file1.txt{,.bak}

 

The output is as follows:


file1.txt -> file1.txt.bak

More examples of brace extensions are as follows:

4. If we implement:


$ cp /etc/apt/sources.list     /etc/apt/sources.list.bak

 

The two / etc / apt / sources sections are the same. In order to input / etc / apt / sources twice less, the parameter is extended with braces. In addition, when renaming, backing up files and creating links, you can use braces to expand parameters to improve efficiency.

Braces extension, that is, the parameters surrounded by braces and separated by commas will be extended to multiple independent parameters.


$ cp /etc/apt/sources.{list,list.bak}

 

When interpreting, the shell will automatically expand the following parameters into two, which will become the same complete command as above.

Of course, it can be simpler. Take “list” into account and write nothing before comma, as follows:


$ cp /etc/apt/sources.list{,.bak}

 

In this way, if there is nothing in front of the comma, the parameters will remain unchanged, and the parameters behind the comma will be expanded as before.

Recommended Today

JVM + GC parsing (premise knowledge concatenation)

Premise preparation JVM GC garbage collection JVM virtual machine monitoring, tuning and troubleshooting Tomcat and microservice optimization 1. Premise review 1.1. JVM memory structure 1.1.1、 JVM Architecture Overview The gray part in the figure isThread private, there is almost no garbage collectionOrange partThread sharing, the main place where garbage recycling occurs What is the class […]