How to rename all files inside a directory to a sequence of incremented files in FISH shell ?

ยท 312 words ยท 2 minute read

rename all files inside a specific directory/folder ๐Ÿ”—

First of all, change directory (cd) into the directory where the files exist.

cd /path/to/your/directory/

Use this command to rename all images (ending with .png) to a number sequence.

set i 1
for file in *
    mv $file (printf "%02d.png" $i)
    set i (math $i + 1)
end

The command explained ๐Ÿ”—

set i 1 creates a variable called i and set its value to 1.

The for loop loops over all files in the current directory, hence * means ALL.

mv means move. It is a command used to change the location of a file/directory and/or change the name of them. In this code we use it to change file name.

printf "%02.png" $i is to create a new name for each file. The name will name the value of variable i presents in two-digit number. For example, if i is 1, it will be 01. If i is 12, it will be 12. and so on. You can change it to show numbers with 4 digits like this printf "%04.png" $i.

set i (math $i + 1) is to increment the value of i for the next iteration of the loop to handle the next file.

Make sure to specify the extension of your files - you want to rename - by change png in this code snippet mv $file (printf "%02d.png" $i). For example, if you want to change all plain text files, use this code snippet mv $file (printf "%02d.txt" $i). I just changed png to txt as you can see.

I hope this post helps you. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , Facebook , Telegram and GitHub .

Share: