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?

1

Use rename command:

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

  • 's/\.bak$//' - perl expression to match all filenames ending with .bak and strip the extension
| improve this answer | |
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
| improve this answer | |
0

Something that should work across all platforms:

for i in *.bak
do
  mv $i `echo $i | sed 's/\.bak$//'`
done
| improve this answer | |
  • 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
| improve this answer | |
  • Note that it would rename best.baker.bak to bester.bak and would choke on file names that start with -. – Stéphane Chazelas Jun 23 '17 at 14:35
-1
#!/bin/bash
for val in \`ls *.bat`
do
    mv $val `echo $val | cut -f1 -d'.'`
done
| improve this answer | |
  • 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: mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29 – ilkkachu Jun 23 '17 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.