-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
  • 51,466
  • 13
  • 89
  • 103
0
for FILE in *.txt ; do mv -i "$FILE" "$(basename "$FILE" .txt)" ; done
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
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,597
  • 5
  • 30
  • 51
  • 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