This is a discussion on How automatically to number all the files of a repertory within the AIX Operating System forums, part of the Unix Operating Systems category; --> Hi, How to make automatically to number by a SHELL script all the files present in the repertory "/home/toto/"? ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi, How to make automatically to number by a SHELL script all the files present in the repertory "/home/toto/"? The files will be numbered in the following way of 1 to N: 1-*** 2-*** 3-*** .... .......... n-*** Thanks. GUY |
| ||||
| What would your criteria be for the renaming order ? timestamp ? filenames in alpha/numeric order ? As this will affect how you number your files right ? i.e. Lets create some files to play with... #!/bin/ksh93 for (( i= 1 ; i < 10 ; i++ )) do date > file-${i} done ls -l total 72 -rw-r--r-- 1 root system 25 30 Sep 11:58 file-1 -rw-r--r-- 1 root system 25 30 Sep 11:58 file-2 -rw-r--r-- 1 root system 25 30 Sep 11:58 file-3 -rw-r--r-- 1 root system 25 30 Sep 11:58 file-4 -rw-r--r-- 1 root system 25 30 Sep 11:58 file-5 -rw-r--r-- 1 root system 25 30 Sep 11:58 file-6 -rw-r--r-- 1 root system 25 30 Sep 11:58 file-7 -rw-r--r-- 1 root system 25 30 Sep 11:58 file-8 -rw-r--r-- 1 root system 25 30 Sep 11:58 file-9 now lets rename them (NOTE: this is REALLY FORKY and is NOT VERY EFFICIENT, you would be better off doing this in perl if you have thousands of files). Also, NOTE that I have just done a simple "ls" which will list the files in alpah/numeric order .. you could use any of the ls plags to get a different order, or the find command etc.. this is just an example of how you could do it, there are probably quite a few avenues to explore, this is just one of them. ls | awk '{print NR,$1}' | while read num file do mv $file ${num}-${file} done ls -l total 72 -rw-r--r-- 1 root system 25 30 Sep 11:58 1-file-1 -rw-r--r-- 1 root system 25 30 Sep 11:58 2-file-2 -rw-r--r-- 1 root system 25 30 Sep 11:58 3-file-3 -rw-r--r-- 1 root system 25 30 Sep 11:58 4-file-4 -rw-r--r-- 1 root system 25 30 Sep 11:58 5-file-5 -rw-r--r-- 1 root system 25 30 Sep 11:58 6-file-6 -rw-r--r-- 1 root system 25 30 Sep 11:58 7-file-7 -rw-r--r-- 1 root system 25 30 Sep 11:58 8-file-8 -rw-r--r-- 1 root system 25 30 Sep 11:58 9-file-9 HTH Mark Taylor |