Introduction to the usage of cp and mv in Linux with {,} in shell

Source: Internet
Author: User

Introduction to the usage of cp and mv in Linux with {,} in shell

This article mainly introduces the usage of cp and mv matching {,} in shell in Linux. The author provides four examples of extension in braces. For more information, see

I often see the following command braces in a blog or forum to extend the Brace expansion {} shell function.

?

1

Cp/etc/httpd. {,. bakup}

Or

?

1

Mv resume{z,developer.doc

So what does ubench/linux shell Command mean? What does it do?

{} Has no actual meaning, but it can be used as Brace expansion (braces extension or curly braces extension) and is often used to generate various groups. The content translated from GNU/BASH man page is as follows:

Brace expansion (braces extension or curly braces extension) is used to randomly generate string combinations. This mechanism is similar to the file extension, but does not need to have a response file. The Brace expansion (braces extension or curly braces extension) mode is an optional preamble (Leading Character) followed by a series of comma-separated strings, which are contained in a pair of curly braces, next is an optional postscript (print programming language ). Preamble (Leading Character) is added to the front of each string in curly brackets, and postscript (print Programming Language) is appended to each result string and expanded from left to right. Curly braces can be nested. The results of the extended string are not sorted, but the order from left to right is retained.

1. For example, enter the following command in the terminal:

?

1

Echo foo{, 32.16.txt

The output is as follows:

?

1

Foo1.txt foo2.txt foo3.txt

2. You can also use the attached instance below to "CREATE" a parameter in the command to save input time and improve work efficiency:

Echo file.txt {,. bak}

?

1

2

3

4

5

6

7

Echo file-0000a..d0000.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 (braces extension or curly braces extension) to copy, rename a file, back up a file, or create a directory. In the following example, back up file1.txt as file2.txt. bak, and enter:

?

1

Cp-v file1.txt file1.txt. bak

If you use Brace expansion to save time, enter:

?

1

Cp-v file1.txt {,. bak}

The output is as follows:

?

1

File1.txt-> file1.txt. bak

Examples of more braces extension are as follows:

4. If we execute:

?

1

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

The two/etc/apt/sources parts are the same. To enter/etc/apt/sources twice less, braces are extended. You can also use braces to extend parameters when renaming, backing up files, and creating links to improve efficiency.

Braces are extended, that is, parameters enclosed by braces. Parameters separated by commas are extended to multiple independent parameters.

?

1

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

Shell automatically expands the following parameters into two during interpretation, and then becomes the same complete command as above.

Of course, it can be simpler. You can also take the list into account and do not write anything before the comma, as shown below:

?

1

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

In this way, there is nothing before the comma, so the parameter remains unchanged, and the extension after the comma remains unchanged.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.