Shell script to modify file names in batches

One: add suffix to batch files

  • Requirements: Add the suffix ".tar.gz" to all files ending in .txt in the current directory
#!/bin/sh
for files in $(ls *.txt) 
	do mv $files $files".tar.gz"

Two: Batch files delete specified characters

  • Requirements: For all files ending in .txt.tar.gz in the current directory, delete the .tar.gz suffix
#!/bin/sh
for file in `ls | grep .txt.tar.gz`
do
 newfile=`echo $file | sed 's/.tar.gz//g'`
 mv $file $newfile
done

Three: Batch replace file names

  • Requirements: Replace all files ending with .txt in the current directory with .p.txt
#!/bin/sh
for file in `ls | grep .txt`
do
 newfile=`echo $file | sed 's/.txt/.p.txt/g'`
 mv $file $newfile
done

Guess you like

Origin blog.csdn.net/qq_41341757/article/details/127406769