[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 extensionprename 's/\.bak$//' *.bak (Perl version)
rename .bak '' *.bak work for this..
Something 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.)
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 -.
#!/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.
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