Creating a list of files in directory?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Creating a list of files in directory?
# 1  
Old 11-27-2009
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  
Old 11-27-2009
Something like this?

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

# 3  
Old 11-27-2009
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  
Old 11-27-2009
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  
Old 11-27-2009
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  
Old 11-30-2009
thank you all
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

2. Shell Programming and Scripting

Creating a list of files retrieved from MGET

Is there a way to create a txt file with the names of the files I retreive using mget *.file. I want to use this file as input to a delete command. Using HP-UX (3 Replies)
Discussion started by: jagf
3 Replies

3. Programming

Script for creating a directory & move the .tif files in it.

Hi Team, I have thousands of TIF files which are converted from PDF. Below is a sample of it. LH9406_BLANCARAMOS_2012041812103210320001.tif LH9406_BLANCARAMOS_2012041812103210320002.tif LH9406_BLANCARAMOS_2012041812103210320003.tif LH9411_ANGENIAHUTCHINSON_2012041812102510250001.tif... (9 Replies)
Discussion started by: paragnehete
9 Replies

4. UNIX for Dummies Questions & Answers

help creating gzip of directory files via cron

**BTW- very new to scripting** I have created a shell script to gzip the public_html files on our website. I have tested this script on another directory on our site and it worked, but when I replaced the directory with the public_html directory it failed. I am executing this script via a... (7 Replies)
Discussion started by: alblue
7 Replies

5. UNIX for Dummies Questions & Answers

Creating a file to count how many files in the directory

hello there i want to creat a file that count how many files i have in the directory. for this i use the command find . -type f | wc -l > 1In1.myfile the problem with this command is that it not update after i add a new file in the directory. Anyone got any ideas how i can... (5 Replies)
Discussion started by: AntiPin
5 Replies

6. UNIX for Dummies Questions & Answers

Creating a List of Files With Find

Hi, I need to go through all the files on my system and build a list/output file with the paths of all files where the first two characters within the file match an expression. I know I can use something like find . | xargs cut -b1-2 or find . -exec cut -b1-2 {} \; to get the characters... (3 Replies)
Discussion started by: 008_
3 Replies

7. Shell Programming and Scripting

Creating date directory and moving files into that directory

I have list of files named file_username_051208_025233.log. Here 051208 is the date and 025233 is the time.I have to run thousands of files daily.I want to put all the files depending on the date of running into a date directory.Suppose if we run files today they should put into 05:Dec:08... (3 Replies)
Discussion started by: ravi030
3 Replies

8. Shell Programming and Scripting

ls > file - Creating file containing the list of all files present in a directory

Hi All, I need to create a file which contains the list of all the files present in that directory. e.g., ls /export/home/user/*.dat > list_file.dat but what i am getting is: $ ls /export/home/user/*.dat > list_file.dat /export/home/user/*.dat: No such file or directory But I have... (1 Reply)
Discussion started by: pranavagarwal
1 Replies

9. UNIX for Dummies Questions & Answers

extract tar files without creating directory

I received a tar file of a directory with 50,000 files in it. Is it possible to extract the files in the tar file without first creating the directory? ie. Doing tar -xvf filename.tar extracts as follows: x directory/file1.txt x directory/file2.txt . . . I would like to avoid... (4 Replies)
Discussion started by: here2learn
4 Replies

10. Programming

creating object files in a specific directory

hello, i have a makefile in which i am specifying the option for creating the object files of the source files. The option which i am using is this : gcc -c main.c first.c by default these object files are created in the same directory in which the makefile is present. what option... (1 Reply)
Discussion started by: svh
1 Replies
Login or Register to Ask a Question