Rename multiple files by cutting first part , the end or in middle of name under Linux – part 001.

You can see the images I make with Screenshot application and is named like :

When you want to rename multiple files recursively is very good to know how is finally format name.

It’s tree way to deal with this issue.

First you can cut the first part , the end or in middle of name.

Let cut first part in the named files. In this example I will remove : Screenshot – 10272013 – .

The next command will cut first 24 letters from the name of files.

Let’s see the output of this command:

Now I will cut the last 4 letters from the names of the files:

… and output will be :

Now you don’t use in command this:

… because you dont have now the .png in your names of files.

When you want to rename file in middle of name the you need something new.

Using the sed command to solve this issue:

You can see I change somethig from :

in this :

“$(echo $my_file | sed ‘s/ AM/ PM/g’)”

This will change the second from args of mv command into you want.

In this case will replace ‘ AM’ with ‘ PM’…

Sed also can change the special characters… Let’s remove the colon : with nothing.

… and the result of this command:

  • Michael Florian Schönitzer

    You don’t need sed for replacing strings in varibles, you can use ${var/from/to}.

    • I don’t know how to do this. If you make a simple tutorial… I will add to this website with your author signature. This will help many people. So why not to do this. Thank you for comment. Have a great day.

      • Michael Florian Schönitzer

        Instead of:
        $ for my_file in *; do mv “$my_file” “$(echo $my_file | sed ‘s/ AM/ PM/g’)”; done

        you can do:
        $ for my_file in *; do mv “$my_file” “${my_file/ AM/ PM}”; done

        Same for:
        $ for my_file in *; do mv “$my_file” “$(echo $my_file | sed ‘s/://g’)”; done
        $ for my_file in *; do mv “$my_file” “${my_file/:/”; done
        (you don’t need the slash before the : here, because : has no special meaning for the bash)