Philippe Blayo
Philippe Blayo

Reputation: 11030

Bash: remove file suffix in one command line

Given file path such as fiz.js and dir/buz.js, how to remove suffixes to obtain fiz dir/buz?

My best shot (I'm looking for something simpler):

find -name '*.js'| \
 xargs -I{} echo {} | \
 sed 's/.js$//' | \
 xargs -I{} mv {}.js {}

Upvotes: 3

Views: 1160

Answers (4)

cromod
cromod

Reputation: 1809

This works for me : rename ".js" "" *.js

Upvotes: 0

Andrea Patricelli
Andrea Patricelli

Reputation: 1

find -name '*.js'|  xargs -I file rename 's/\.js$//' file

it works and if you want to overwrite existing files you have to add a -f to rename (rename -f).

Upvotes: 0

Vijay
Vijay

Reputation: 67301

echo "fiz.js"|awk -F. '{print $1}'

echo "dir/buz.js"|awk -F. '{print $1}'

Upvotes: 0

Mat
Mat

Reputation: 206851

You could do it with just bash (version 4) by using globstar to get a recursive file list.

$ shopt -s globstar
$ for i in **/*.js ; do echo "$i" "${i%.js}" ; done

For the bash man page:

globstar

If set, the pattern ** used in a pathname expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.

${x%ext} removes ext from the end of $x. See String manipulations for more.

Upvotes: 2

Related Questions