How to shorten a string

Say I have the following files: 1.html, 2.html and 3.html, and I want to rename them to 1.html-bak, 2.html-bak and 3.html-bak. To do this, I run the following command:

find . -name '*.html' -print0 | xargs -0 -I {} mv {} {}-bak

Everything is in order, the files are renamed as expected.

The question is, how to rename them back to *.htmlinstead *.html-bak?

How to remove the last 4 lines from a line?

+4
source share
2 answers

You can use ${file%-*}to return the desired file name. The following code goes through all the files whose name ends with html-bakand renames, deleting everything after the last dash:

for file in *html-bak
do
   echo "mv $file ${file%-*}" # <-- using "echo" for safety. Remove once checked
done

${var%-*} * $var. , , - :

$ file="1.h-tml-bak"
$ echo ${file%-*}
1.h-tml

, , , 4 :

$ echo ${file:0:-4}
1.h-tml
+7

GNU Parallel, :

find . -name '*.html-bak' -print0 | parallel -0 mv {} {.}.html

, "," "".

, . :

GNU Parallel - , , ssh.

32 , 4- , - 8 :

Simple scheduling

GNU Parallel , - , , :

GNU parallel scheduling

root. 10 , :

(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash

. http://git.savannah.gnu.org/cgit/parallel.git/tree/README

: http://www.gnu.org/software/parallel/man.html

: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

: http://www.gnu.org/software/parallel/parallel_tutorial.html

, : https://lists.gnu.org/mailman/listinfo/parallel

+2

All Articles