Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming a series of files

Trying to rename a series of files on a linux server. Finding the files I want is easy:

find . -type f -wholename \*.mbox

Of course, being mbox files, some of them have spaces in the names, so it becomes:

find . -type f -wholename \*.mbox -print0

I'm piping to xargs so that I can rename the files:

find . -type f -wholename \*.mbox -print0 | xargs -0 -I{} echo ${"{}"/.mbox/}

The echo should return something like INBOX, given INBOX.mbox, however, bash complains:

bash: ${"{}"/.mbox/}: bad substitution

How can I fix this? I'd like to try to keep it in a find/xargs solution if possible, so that I'm not adding a lot of looping constructs around it.

like image 688
Glen Solsberry Avatar asked Jan 29 '26 01:01

Glen Solsberry


2 Answers

Try

find . -type f -wholename \*.mbox | sed 's/\(.*\)\.mbox/mv "\1.mbox" "\1"/' | sh

This is not 100% fool proof should some of the files contain double quote characters, but I assume you can ignore that :)

like image 175
hlovdal Avatar answered Jan 31 '26 14:01

hlovdal


GNU Parallel http://www.gnu.org/software/parallel/ has {.} that removes the extension:

find . -type f -wholename \*.mbox -print0 | parallel -0 mv {} {.}

If you know the filenames do not contain \n then this will work aswell:

find . -type f -wholename \*.mbox | parallel mv {} {.}
like image 33
Ole Tange Avatar answered Jan 31 '26 16:01

Ole Tange