D. N.
D. N.

Reputation: 67

How to rename multiple files with different names at once?

I have several docx files (approximately 250 files) each one with a different name. I need to rename all these files so that the result has to be that each file name must be a consecutive number for the previous file, as follows:

1.docx - 2.docx - 3.docx - etc.

How can I do this using the command line in my Linux?

My biggest problem is that each file has a completely unreadable name.

For example:

ÄÿÇä ÄùàÄàÜ ÄæÆë ÜÖÆà.docx

Because these files were originally named in Hebrew and when the zip file was extracted the files were given illegible names.

Upvotes: 2

Views: 1879

Answers (3)

Ole Tange
Ole Tange

Reputation: 33685

This might work:

parallel mv -- {} {#}.docx ::: *.docx

Upvotes: 1

potong
potong

Reputation: 58391

This might work for you (GNU parallel):

parallel --dry-run mv -v ::: *.docx :::+ {1..300}.docx

This will print commands to rename all files ending in .docx to consecutive numbers upto and including 300.

To actually run the commands (after inspection), remove the --dry-run option.

Upvotes: 1

John1024
John1024

Reputation: 113834

Try:

count=0; for f in *.docx; do mv -i -- "$f" "$((count=count+1)).docx"; done

If there was a chance that one of the original files was actually named with a simple number, like 3.docx, then it is safer to move the files to some other directory at the same time that they are being renamed:

count=0; for f in *.docx; do mv -i -- "$f" "some/other/dir/$((count=count+1)).docx"; done

How it works

  • count=0

    This initializes the variable count to zero.

  • for f in *.docx; do

    This starts a loop with f being assigned to the name of each docx file in turn.

  • mv -i -- "$f" "some/other/dir/$((count=count+1)).docx"

    This renames/moves the files. $((count=count+1)) tells the shell to increment the count each time.

    Under bash, $((count=count+1)) can be simplified to $((++count)).

  • done

    This signals the end of the loop.

Upvotes: 1

Related Questions