![]() |
|
|
grep unix.com with google
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Our Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
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 |
|
|||
|
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
|
|
|||
|
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.. |
|
|||
|
thank you all
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
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 |