[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?
[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?
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
Use rename
command:
rename 's/\.bak$//' *.bak
's/\.bak$//'
- perl expression to match all filenames ending with .bak
and strip the extensionSomething that should work across all platforms:
for i in *.bak
do
mv $i `echo $i | sed 's/\.bak$//'`
done
$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
As the promp of RomanPerekhrest's answer here,I find this command work for me.
rename .bak '' *.bak
best.baker.bak
to bester.bak
and would choke on file names that start with -
.
– Stéphane Chazelas
Jun 23 '17 at 14:35
#!/bin/bash
for val in \`ls *.bat`
do
mv $val `echo $val | cut -f1 -d'.'`
done
*
, ?
, [
backslash characters, that they don't start with -
, that they contain no other .
characters.
– Stéphane Chazelas
Jun 23 '17 at 14:39
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
prename 's/\.bak$//' *.bak
(Perl version) – RomanPerekhrest Jun 23 '17 at 14:17rename .bak '' *.bak
work for this.. – yode Jun 23 '17 at 14:23