Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename all the files in the folder with increasing numbers

I have a folder and inside that folder I have 10-15 files with arbitrary names. The filenames may include spaces in them. For example: wWw.page.com __ (576)_002. In a terminal, when I press w and then tab the file name appears like this: wWw.page.com\ \ __\ \(576\)_0.txt.

I want some script that will rename all my files like this 0.txt, 1.txt, 2.txt and so on.

My problem is: wWw.page.com __ (576)_002.txt file not found.

index=0;
for i in $(ls *.txt)
do
    cp "${i}"  $index".txt" 
done
like image 701
Lukap Avatar asked Dec 23 '11 17:12

Lukap


People also ask

How do I rename a file with ascending numbers?

Another is to simultaneously press the Ctrl + A keys. Right click on the first file/folder and select Rename. Type in the name you want to use and press Enter. All the files/folders will now have the same name but with sequential numbers.

How do I rename multiple files with sequential numbers in Windows 11?

Use the File Explorer to navigate to the folder where your files are. Select all the files you want to rename, then right-click them and choose See more options in the context menu. Then, choose PowerRename in the second context menu. You'll now see the PowerRename interface with all your selected files.

How do I batch rename all files in a folder?

To rename multiple files from File Explorer, select all the files you wish to rename, then press the F2 key. The name of the last file will become highlighted. Type the new name you wish to give to every file, then press Enter.

How do you rename all images in a folder at once with numbers?

You can batch rename images in Windows by selecting (Shift+click or Ctrl+click to select several files; Ctrl+A to select all) and pressing right-click > "Rename".


1 Answers

Instead of ls try to glob:

index=0;
for name in *.txt
do
    cp "${name}" "${index}.txt"
    index=$((index+1))
done
like image 80
cnicutar Avatar answered Oct 29 '22 19:10

cnicutar