The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
grep unix.com with google



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Nov 2007
Posts: 108
Creating a list of files in directory?

Hi All,
I would like to do a loop on all the files with extension .out in my directory this files should be sorted alphabetically.
Then I need to assign to each of them a progressive ID of three digits (maybe in HEX format?).

First I tried to list manually the files as

Code:
ARRAY=( 
A-001.out  B-006.out ...
)
LIST=(
01 02 ...)

ELEMENTS=${#ARRAY[@]}  
echo $ELEMENTS         
# echo each element in array 
# for loop             
for (( i=0;i<$ELEMENTS;i++)); do
    echo ${ARRAY[${i}]} ${LIST[${i}]}
done

Is there a smarter way to get the files and assign them an ID?

Thank you in advance,
Sarah
  #2 (permalink)  
Old 11-27-2009
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 4,669
Something like this?


Code:
ls | awk '{printf("%s_%03s\n",$0, ++i)}'

  #3 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Nov 2007
Posts: 108
This something I wrote that does the job, but there may be a smarter way


Code:
for I in `ls ${PATH}/*.out | cut -c${LENGTH}-`;
do
 ARRAY[$COUNTER]=$I
 if [ $COUNTER -lt 10 ]; then
     LIST[$COUNTER]=000$COUNTER
 else
     if [ $COUNTER -lt 100 ]; then
         LIST[$COUNTER]=00$COUNTER
     else
         if [ $COUNTER -lt 1000 ]; then
              LIST[$COUNTER]=0$COUNTER
         else
             if [ $COUNTER -lt 10000 ]; then
                  LIST[$COUNTER]=$COUNTER
             fi
         fi
     fi
 fi

  #4 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Dec 2005
Posts: 80
Creating list of files in directory

fraklins codes look, easier and smart,
posting the expected output along with question is always, better than
explaining the problems in words
  #5 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 1,258

Code:
for f in *.out; do
  printf "%03d %s\n" $((++i)) "$f"
done

Hex:

Code:
for f in *.out; do
  printf "%03X %s\n" $((++i)) "$f"
done

Hex number last

Code:
for f in *.out; do
  printf "%s %03X\n" "$f" $((++i))
done

etc.

---------- Post updated at 22:41 ---------- Previous update was at 21:54 ----------


Code:
cat -n <(ls -1 *.out)


Last edited by Scrutinizer; 11-27-2009 at 05:02 PM..
  #6 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Nov 2007
Posts: 108
thank you all
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find list of files from a list and copy to a directory manishabh Shell Programming and Scripting 3 09-13-2009 07:03 PM
Creating a List of Files With Find 008_ UNIX for Dummies Questions & Answers 3 06-19-2009 01:34 AM
Creating date directory and moving files into that directory ravi030 Shell Programming and Scripting 3 12-05-2008 04:18 AM
ls > file - Creating file containing the list of all files present in a directory pranavagarwal Shell Programming and Scripting 1 09-26-2008 09:37 PM
extract tar files without creating directory here2learn UNIX for Dummies Questions & Answers 4 10-02-2006 08:42 PM



All times are GMT -4. The time now is 10:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0