Get the App
SLTechnology News&Howtos  ›  Development  › 

How to change the file extension with Linux bash recursion

Shulou Source: shulou.com Published: 2022-06-01 20:59:46 10月05日 Update

This article shows you how to change the file extension with Linux bash recursion. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

How to change the file extension recursively in Linux system? It is a good choice to use bash recursion when we need to change a lot of file extensions, which is very useful when debugging tests.

Use the find-exec and mv commands to recursively change the file extension find. -name "* .t1"-exec bash-c'mv "$1" ${1%.t1} ".t2'-'{}'+ for example: we want to find all the .spec.ts files in the src directory and change the extension

Find. / src-name "* .spec.ts"-type f bash-c'mv "$1"${1%.t1}" .T2'-'{}'+. / src/users/users.controller.spec.ts. / src/auth/auth.service.spec.ts. / src/auth/auth.controller.spec.ts. / src/roles/roles.controller.spec.ts. / src/roles/roles.service.spec.ts. / src/shared/prisma.service .spec.ts. / src/app.controller.spec.ts. / src/permissions/permissions.controller.spec.ts. / src/permissions/permissions.service.spec.ts' {}'is the source file name The package line is relative to the lookup location directory. / src/auth/auth.service.spec.ts, the reason for adding single quotation marks is that shell interprets {}, which normally means creating an array array in shell

A call in the form of + returns a non-zero value as the exit status, that is, find returns a non-zero exit status. If you find an error, exit immediately.

Bash-c executes the specified command

'mv "{1%.t1}" .t2' this is a mv command that accepts two arguments. The first argument is $1, which is the passed -'{}'.

Extra open brace or missing close brace1 is deconstructed, and the path part is assigned to 1%, where 1% is. / src/auth/auth.service

Use the find-exec and rename commands to change the file extension recursively to install rename in ubuntu and debian sudo apt install rename to install rename in Centos and RedHat

Sudo dnf install rename find. -name'* .t1'- exec rename .t1.t2 {} + find. -name "* .t1"-exec rename's /\ .t1 $/ .t2 /'{}'+ the above is how Linux bash recursively changes the file extension. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Extension file recursion command content parameter that is skill status directory knowledge good concise concise two reason location quotation mark form situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Redmi Xiaomi Shulou Information