Linux batch change file suffix name

  • 2020-05-12 06:42:57
  • OfStack

Today, another student asked how to change the suffix name of the file in batches under Linux. This question has been asked for three times, so here are some solutions

1. rename solution

1. Under the Ubuntu system


rename 's//.c//.h/' ./*

Change the file in the current directory with the suffix.c to.h

2. Under CentOS 5.5 system


rename .c .h  *.c

Change the file in the current directory with the suffix.c to.h

2.shell script solution


#!/bin/bash
find ./ -name *.c | while read i
do
    echo "$i";
    mv $i.c $i.h
done

3. find xargs solution


find ./ -name "*.c" | awk -F "." '{print $2}' | xargs -i -t mv ./{}.c ./{}.h

Note that the third scenario is a recursive change that changes all the matching files in the current directory and its subdirectories


Related articles: