Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove a specific extension from files recursively using a bash script

Tags:

bash

filenames

I'm trying to find a bash script that will recursively look for files with a .bx extension, and remove this extension. The filenames are in no particular format (some are hidden files with "." prefix, some have spaces in the name, etc.), and not all files have this extension.

I'm not sure how to find each file with the .bx extension (in and below my cwd) and remove it. Thanks for the help!

like image 863
nick Avatar asked Oct 01 '10 08:10

nick


4 Answers

find . -name '*.bx' -type f | while read NAME ; do mv "${NAME}" "${NAME%.bx}" ; done
like image 51
tylerl Avatar answered Oct 14 '22 11:10

tylerl


find -name "*.bx" -print0 | xargs -0 rename 's/\.bx//'
like image 41
Ken Avatar answered Oct 14 '22 12:10

Ken


Bash 4+

shopt -s globstar
shopt -s nullglob
shopt -s dotglob

for file in **/*.bx
do
  mv "$file" "${file%.bx}"
done
like image 29
ghostdog74 Avatar answered Oct 14 '22 12:10

ghostdog74


Assuming you are in the folder from where you want to do this

find . -name "*.bx" -print0 | xargs -0 rename .bx ""
like image 22
Raghuram Avatar answered Oct 14 '22 13:10

Raghuram