Rename files in sub directories with sequential numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename files in sub directories with sequential numbers
# 1  
Old 08-28-2011
Rename files in sub directories with sequential numbers

I can rename a file with sequential numbers from 1 to N with this script:

Code:
num=1
for file in *.dat;do
       mv "$file" "$(printf "%u" $num).txt"
       let num=num+1
done

The script begins with renaming a some.dat file to 1.dat.txt and goes on sequentially renaming other DAT files to 2.dat.txt, 3.dat.txt and so on.


This script works very well on my Linux system when I have one directory with all DAT files in it. But it cannot work when I have a parent directory with sub directories. My directory structure is like this:

Parent Directory and inside parent directory there are 20 more sub directories and inside those sub directories are the DAT files.

Is there any way I can modify the above script so that (I am in my parent directory and run this script) it takes in one sub-directory and renames all the files in that directory in sequential numbers say 1 to N, then goes to another sub-directory and renames from N+1 to M, then goes to third sub-directory and renames from M+1 to X and so on?
# 2  
Old 08-28-2011
The auto-increment operator and the -v option of mv are not standard, so use zsh, ksh93 or bash to run the script.
I'm assuming there are no embedded newlines in the filenames.

Code:
c=0
find . -type f -name '*.dat' |
  while IFS= read -r; do
    mv -v -- "$REPLY" "${REPLY%/*}/$(( ++c )).dat"
  done

If you want standard code, you should use something like this:

Code:
$(( c = c + 1 ))

... and you should remove the -v option from the mv command.

P.S. I don't remember if read without varname is standard, to lazy to check it right now Smilie

Last edited by radoulov; 08-28-2011 at 07:25 AM..
This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files from multiple directories along with directory indicator

Hi, Friends, i have a requirement where i need to rename my files residing in multiple sub directories and move them to one different directory along with some kind of directory indicator. For eg: test--is my parent directory and it has many files such as a1.txt a2.txt a3.txt ... (5 Replies)
Discussion started by: gnnsprapa
5 Replies

2. UNIX for Dummies Questions & Answers

Replace groups into sequential numbers

I have a file that looks like this: n1 1 n2 1 n3 1 n4 3 n4 3 n2 5 n2 5 n2 5 n2 5 n3 5 n3 5 n4 6 n7 6 that is a name followed be a descriptive number. I want to make these numbers sequential starting from 0 but without changing the "neighbours" each name belongs to. So the above... (3 Replies)
Discussion started by: FelipeAd
3 Replies

3. Shell Programming and Scripting

Rename the files in all the directories and sub-directories

Hi all, I have more than 12000 files in 46 different directories and each directory has 2 sub-directories named “dat” or “gridded”. Dat sub-directories have files with extension “jpg.dat” and gridded sub-directories have files with extension “.jpg”. I need to... (1 Reply)
Discussion started by: AshwaniSharma09
1 Replies

4. Shell Programming and Scripting

Read directories sequential based on timestamp

Hi, I have a directory structure like below Directoryname create time d1 12:00 d2 12:05 d3 12:08 I want to read the directories based on timestamp.That is oldest directory must be read first and kick off certain process. ... (7 Replies)
Discussion started by: chetan.c
7 Replies

5. Shell Programming and Scripting

Sequential numbers

Hi All, I am looking for a simple way to write numbers to a file sequentially starting from 1 and ending on a specified upper limit. Example of the output file is below Example 1 2 3 4 5 . . . . 1000 please let me know the best way to do it. (10 Replies)
Discussion started by: Lucky Ali
10 Replies

6. Shell Programming and Scripting

Rename files with continuous numbers

I have a huge collection of HTML files. They have their own file names with htmlextension. I want to rename each of these files with continuous numbers starting from 1.html till the last count of files. Simply it means that if there are three files like this abc.html cdfhg.html rmbd.htmlthen... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. Shell Programming and Scripting

Rename files and directories with special characters

Hello guys, I was looking for a shell script that removes all the special characters from the files and the subdirectories recursively. I could not locate it any more. Dose any body have a similar script that dose that? Thanks for the help. AV (0 Replies)
Discussion started by: avatar_007
0 Replies

8. Shell Programming and Scripting

Rename files recursivly in specified directories

Hi, This is what I would like to do. 1. Find all directories named "ByHost" in a specified directory 2. Rename all .plist files inside "ByHost" directories This is the way I have been able to do it so far. #!/bin/sh # # Rename ByHost files # # Thomas Berglund, 13.07.08 # Get the... (2 Replies)
Discussion started by: Thomas Berglund
2 Replies

9. UNIX for Dummies Questions & Answers

inserting uniq sequential numbers at the start of the file

Hi Unix gurus, I have a file. I need to insert sequential number at the starting of the file. Fields are delimited by "|". I know the starting number. Example: File is as follows |123|4test|test |121|2test|test |x12|1test|test |vd123|5test|test starting number is : 120 ... (7 Replies)
Discussion started by: jingi1234
7 Replies

10. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies
Login or Register to Ask a Question