1

[root@localhost ~]# ls *.bak

test2.bak test3.bak test9.bak test.bak test.txt.bak

How to drop all .bak in all file?This is my current try

ls *.bak|xargs -t -i mv {}.bak {}

But it don't work for me.Is there any workaround here?

yode
  • 1,047

5 Answers5

2

A loop should do (in a POSIX-like shell):

for f in *.bak; do 
    mv -- "$f" "${f%.*}"
done

And as one line:

for f in *.bak; do mv -- "$f" "${f%.*}"; done
1

Use rename command:

rename 's/\.bak$//' *.bak

  • 's/\.bak$//' - perl expression to match all filenames ending with .bak and strip the extension
0

Something that should work across all platforms:

for i in *.bak
do
  mv $i `echo $i | sed 's/\.bak$//'`
done
unxnut
  • 6,008
  • You need quotes around $i and the command substitution to deal with whitespace in filenames. (Some newlines would still cause somewhat odd results.) – ilkkachu Jun 23 '17 at 15:23
0

As the promp of RomanPerekhrest's answer here,I find this command work for me.

rename .bak '' *.bak
yode
  • 1,047
-1
#!/bin/bash
for val in \`ls *.bat`
do
    mv $val `echo $val | cut -f1 -d'.'`
done
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • That makes a lot of assumptions, like that none of the bat files are of type directory, that their names don't contain space, tab, newline, *, ?, [ backslash characters, that they don't start with -, that they contain no other . characters. – Stéphane Chazelas Jun 23 '17 at 14:39
  • The ls here doesn't do anything (but cause issues), the shell still generates the list of file names. See also: http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29 – ilkkachu Jun 23 '17 at 15:25