Unix Technical Forum

How automatically to number all the files of a repertory

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/"? ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-05-2008, 08:32 AM
guytou77
 
Posts: n/a
Default How automatically to number all the files of a repertory

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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-05-2008, 08:32 AM
Mark Taylor
 
Posts: n/a
Default Re: How automatically to number all the files of a repertory


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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:56 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com