0

I'm looking for the regular expression to find all the files in a folder (and its subfolders) that do not have an extension and add the extension .mp3 to these files only (i.e. the files which already have the extension should not get an additional one)?

For example:

test is made into test.mp3

test1.mp3 remains as it is

An additional problem I have is that some of my file names have spaces.

So far I use the following expression for the first part (with maxdepth specifying the depth in terms of folder structure I want to have):

find . -maxdepth 1 ! -name "*.*" -o -name ".*[^.]*" 

I cannot work out how to do the adding of the .mp3 extension.

2 Answers 2

1

The below find command would recursively rename(adding .mp3 as extension to those files) the files which don't have any extension.

find . ! -name *.* -type f -exec mv {} {}.mp3 \;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this did the job. I added "..." around the {} though.
The quotes wouldn't be necessary, since the shell has no hand in constructing or executing the command specified by -exec.
0

The following command won't modify any files as long as -nono argument is present:

rename -v -nono 's{.+(\.mp3)?\Z}{.mp3}i' *

rename(1) utility is provided by "rename" package on Debian.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.