-1

Possible Duplicate:
Extract filename and extension in bash
Linux: remove file extensions for multiple files

For example, A.txt B.txt, I want to rename then to A and B . 

How can I do it with shell script? Or other method ? Thanks.

Community
  • 1
  • 1
Clavier
  • 31
  • 3

3 Answers3

1
for i in *.txt; do mv "$i" "${i%.txt}"; done
Steve
  • 45,652
  • 12
  • 89
  • 100
0
for FILE in *.txt ; do mv -i "$FILE" "$(basename "$FILE" .txt)" ; done
Dietrich Epp
  • 194,726
  • 35
  • 326
  • 406
0

I would use something like:

#!/bin/bash

for file in *.txt
do
    echo "$file" "$( echo $file | sed -e 's/\.txt//' )"
done

Of course replace the two above references of ".txt" to whatever file extension you are removing, or, preferably just use $1 (the first passed argument to the script).

Michael G.

mjgpy3
  • 8,053
  • 4
  • 27
  • 48
  • 2
    `$(ls *.txt)` should just be `*.txt`, `$file` should be quoted as `"$file"` (as well as the `"$( echo ... )"`), and the regexp should be `'s/\.txt$//'` (the `g` flag means "do multiple substitutions", which is not what you want, you only want one). – Dietrich Epp Aug 14 '12 at 02:20
  • Thank you, I will change the code. – mjgpy3 Aug 14 '12 at 02:24