Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move and number files?

I working with linux, bash.

I have one directory with 100 folders in it, each one named different.

In each of these 100 folders, there is a file called first.bars (so I have 100 files named first.bars). Although all named first.bars, the files are actually slightly different.

I want to get all these files moved to one new folder and rename/number these files so that I know which file comes from which folder. So the first first.bars file must be renamed to 001.bars, the second to 002.bars.. etc.

I have tried the following:

ls -d * >> /home/directorywiththe100folders/list.txt
cat list.txt | while read line;
do cd $line;
mv first.bars /home/newfolder

This does not work because I can't have 100 files, named the same, in one folder. So I only need to know how to rename them. The renaming must be connected to the cat list.txt, because the first line is the folder containing the first file wich is moved and renamed. That file will be called 001.bars.

like image 658
rubano Avatar asked Nov 26 '25 16:11

rubano


1 Answers

Try doing this :

$ rename 's/^.*?\./sprintf("%03d.", $c++)/e' *.bar

If you want more information about this command, see this recent response I gave earlier : How do I rename multiple files beginning with a Unix timestamp - imapsync issue

like image 114
Gilles Quenot Avatar answered Nov 28 '25 17:11

Gilles Quenot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!