Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove file extension by using find, xargs

Tags:

bash

shell

I am trying to remove file extension 'log' by practice:

find -name "*.log" | xargs -t -I {} mv {} {{}%.log}
like image 302
Bryan Avatar asked Sep 21 '25 06:09

Bryan


2 Answers

I would probably just use rename:

find -name '*.log' | xargs rename .log ''

Or a shell script:

find -name '*.log' | while read f; do mv $f ${f%.log}; done
like image 180
larsks Avatar answered Sep 22 '25 21:09

larsks


If you are using bash 4 or later, you can simply use

shopt -s globstar
for f in **/*.log; do
    mv -- "$f" "${f%.log}"
done

Otherwise, use find to execute a shell whose argument is the file to rename; this allows you to use parameter expansion to strip the extension.

find -name '*.log' -exec sh -c 'mv -- "$1" "${1%.log}"' {} \;
like image 42
chepner Avatar answered Sep 22 '25 23:09

chepner