0

I have a number of files of different type in a directory: file, file0, file.txt, file.jpg etc.

Some with extensions, some without.

I want to rename files in a directory by inserting 'final' to them.

So they would appear as: filefinal, file0final, filefinal.txt, filefinal.jpg etc.

I imagine that to deal with the presence of files with and without extensions, I'll have to use some if statements, maybe not. To deal with the files with extension I've been thinking that perhaps I could determine the index of the decimal and then add the 'final' string right before that. But I'm not sure how to do that or if it's the best approach.

Any tips greatly appreciated.

1 Answer 1

1

Rather than venture into some one liner script, I would opt for a safer approach.

  1. Dump all your filenames in a file. eg.:

    find . > allMyFiles

  2. use 'vi allMyFiles' and duplicate the names

    %s/.*/"&" "&"/g

  3. add your replacements

    %s/ (.)(..$)/ \1final\2/g

  4. Check your file and remove obvious errors:

    "." "final." <<--- remove

  5. Add the mv command

    %s/^/mv /g

  6. Change to execute permissions and execute your script.

    chmod 700 allMyFiles ./allMyFiles

Sign up to request clarification or add additional context in comments.

2 Comments

Since you dump the output of find in a file this approach results similar to parse the ls output. I know that you consider it in your point 4., but each human action is source of error :-)
I agree... but you can always create the output as a security layer to check before execute it...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.