changing filenames in a directory to a number within a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting changing filenames in a directory to a number within a loop
# 15  
Old 12-13-2007
try this:
Code:
#!/bin/bash

ls -1 *.jpg | sort | while IFS= read fname ; do
    print "$fname"
done

# 16  
Old 12-13-2007
while i appreciate the effort vgersh, i haven't learned much this entire thread. this began as a simple script, but now i can see how it is much more difficult. why wouldn't i just write it in perl or python?

here's what your script returned
Code:
kumi@throne:atlas/images/photos % ./gallery
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"
Error: no "print" mailcap rules found for type "image/jpeg"

still, no one has explained WHY things are not working, they just throw me scripts that they haven't tried themselves. and i'm back at square one. what kind of forum is this?
# 17  
Old 12-13-2007
well.... we're are not your regular customer support help desk and cannot test ALL solutions..... we try to do our best - and once in awhile bugs/mispellings do slip by. You know how it goes, doncha?
If you could implement everything in say perl, why you came here asking for a shell solution to begin with? (this is a somewhat rhetorical question).

Try this:
Code:
#!/usr/local/bin/bash

ls -1 *.jpg | sort | while IFS= read fname ; do
    echo "[$fname]"
done

# 18  
Old 12-13-2007
to answer your question, because i already know perl and python. i'd like to pick up shell scripting to automate much of my daily-weekly-and monthly tasks.

anyway i tried your script and this time it works. but it's only printing out the filenames. what i need this script to do is change all the files with a specific extension to ascending numbers with the same extension:

wonder.jpg -> 1.jpg
sally.JPG -> 2.jpg
box.jpg -> 3.jpg

and so on and so on.

so, how do i establish a variable that will increment, much like in a for loop? because i think i've got it if i can figure out how to add a variable and add 1 to it each loop.

Code:
#!/bin/bash

ls -1 *.$1 | sort | while IFS= read fname ; do
    mv $fname ${i}.${1}
done

does that look right?
# 19  
Old 12-13-2007
Strange - you're the first person I know that goes from perl/Python/Ruby to shell scripting....
But anyhoo....
you're almost there...
Code:
#!/bin/bash
typeset -i i=1

ls -1 *.$1 | sort | while IFS= read fname ; do
     echo "mv [$fname] [{i}.${1}]"
    #mv "$fname" "${i}.${1}"
    ((i++))
done

when you're satisfied with what 'echo' outputs, comment it out, and uncomment the actual 'mv' line.

Last edited by vgersh99; 12-13-2007 at 02:08 PM..
# 20  
Old 12-13-2007
sooo it worked, at first. and i was very very excited. but when i tried the same script on the same files, it messed up. it removed like half of my images. at first i had 19 images with random names. then i ran the script and i had 19 images with names from 1.jpg - 19.jpg which is perfect. then, i ran the same script, on the same files, and it removed 1.jpg - 9.jpg..... why would it have done that? it shouldn't have done that. there's nothing in the script that i can see that would matter what the name of the file was before... what's the issue?

Code:
#!/bin/bash
typeset -i i=1

ls -a1 *.$1 | sort | while IFS= read fname ; do
    #echo "mv $fname ${i}.${1}"
    mv "$fname" "${i}.${1}"
    ((i++))
done

yeah i'm sure it's strange. i actually started out with php as my first programming language, which wasn't the best idea, but it wasn't too bad either. it provided a good transition, i will say that. i have pretty much most high-level scripting languages under my belt, except for shell scripting. also i'm trying to pick up sed and awk at the same time. those programs are friggin awesome!

Last edited by visitorQ; 12-13-2007 at 02:16 PM..
# 21  
Old 12-13-2007
most welcome - y'all come back now, y'hear!

Last edited by vgersh99; 12-13-2007 at 02:20 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For/While Loop to Increment Filenames in a Script

Daily stupid question. I want to increment the file name everytime the script is run. So for example if the filename is manager.log and I run the script, I want the next sequence to be manager.log1. So to be clear I only want it to increment when the script is executed. So ./script... (10 Replies)
Discussion started by: metallica1973
10 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Writing a loop to changing the names of files in a directory

Hi, I would like to write a loop to change the names of files in a directory. The files are called data1.txt through data1000.txt. I'd like to change their names to a1.txt through a1000.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

3. Shell Programming and Scripting

Generate filenames in a loop

Hi all, I want to generate output files in a loop, run the same command on the same input file 1000 times and output in files with a new name each time, maybe a number appended to it. The output will be different each time as I`m sampling randomly from the input file. I want to do the... (3 Replies)
Discussion started by: newbie83
3 Replies

4. UNIX and Linux Applications

Firefox changing hidden filenames

Is firefox changing the filename of this hidden file for anyone else? I tried in firefox 3, 4, and 6. They all got changed when I tried when I downloaded the file. Firefox removed the leading dot that makes the file be hidden. MEGAUPLOAD - The leading online storage and file delivery service (2 Replies)
Discussion started by: cokedude
2 Replies

5. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

6. Shell Programming and Scripting

Whitespace in filenames in for loop in bash script

I'm trying to search all .odt files in a directory for a string in the text of the file. I've found a bash script that works, except that it can't handle whitespace in the filenames. #!/bin/bash if ; then echo "Usage: searchodt searchterm" exit 1 fi for file in $(ls *.odt); do ... (4 Replies)
Discussion started by: triplemaya
4 Replies

7. Shell Programming and Scripting

loop through numbered filenames

Hi I'm very new to this script thing, so please be gentle. I am trying to get a command - the mach2qtl command in the code below - to loop through a set of files. Each command should take the same two .dat and .ped files, but the .mlinfo and .mlprob files with filenames including 'chrom1' ... (7 Replies)
Discussion started by: polly_falconer
7 Replies

8. Shell Programming and Scripting

Help with how to account for changing filenames

The files below are essentially “lookup files” used in a series of scripts that run through the look up files and grab the correct Group Name and File Type and then unless I have the file name specified will replace the variable with the corresponding group name when processing the files before... (1 Reply)
Discussion started by: defiant82
1 Replies

9. UNIX for Dummies Questions & Answers

extracting and using date from filenames in a loop

HIya, Having a dumb day whilst writing an archive process in Shell want to extract from the filename the date and archive into tar files based on this, I don't want to use mtime as it may not be the actual file date. The files are -rw-rw---- 1 user admin 100 Aug 29 11:10... (2 Replies)
Discussion started by: badg3r
2 Replies

10. UNIX for Dummies Questions & Answers

Appending text to a number of similar filenames

Hi, I was wondering if there was a way to append something to filenames based on a wildcard. For example, if I have the following files in a directory: blah1 blah2 blah3 blah4 blah5 I want to rename these all to: blah1.txt blah2.txt blah3.txt blah4.txt blah5.txt Is there a... (4 Replies)
Discussion started by: Djaunl
4 Replies
Login or Register to Ask a Question