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
# 8  
Old 12-11-2007
Interesting, I didn't know that "( *.ext )" was guaranteed to return the list in any particular order.

I think you lost photos (you tested this with samples, right?) because of a permissions problem. Check your man page for "mv", some editions have a "-f" flag or similar that answers the "mode 0644?" question. Either way, the script should exit on failure:

Code:
    mv ${list[$i]} $((++i)).$filetype || { echo "Crap!"; exit; }

# 9  
Old 12-11-2007
hey gus. how about i write it for now to just print out the list of files and the name it will be changed to, first. that way i can see any errors before. i have copies of the photos in a different directory, so i didn't 'lose' any data, just the script doesn't work perfectly.

i like jim mcnamara's idea of adding an argument to the script so i can type the extension i want. however, i want to add in the script to match lowercase and uppercase extensions, and change them to be lowercase once they're matched and the names have been changed. how would i do this? and what do you think of this feature?

this is also something off-topic but i was wondering how to add a shellscript to the standard linux command repository so you don't have to use it in the directory only. and how do you write man-pages for your new scripts? where do they go?
# 10  
Old 12-12-2007
bump =\ bump
# 11  
Old 12-12-2007
Quote:
Originally Posted by visitorQ
and how do you write man-pages for your new scripts? where do they go?
(a) carefully, Linux Man Page Howto

(b) on the MANPATH
# 12  
Old 12-13-2007
thanks for the link porter. but i'm still having trouble with my script:

Code:
kumi@throne:atlas/images/photos % ls -l
total 8148
-rwxr-xr-x 1 root root    133 2007-12-12 22:06 gallery*
-rw-r--r-- 1 kumi kumi 739930 2007-10-30 01:09 Luis Work 001.jpg
-rw-r--r-- 1 kumi kumi 760898 2007-10-30 01:10 Luis Work 010.jpg
-rw-r--r-- 1 kumi kumi 778884 2007-10-30 01:10 Luis Work 016.jpg
-rw-r--r-- 1 kumi kumi 826150 2007-10-30 01:10 Luis Work 017.jpg
-rw-r--r-- 1 kumi kumi 790932 2007-10-30 01:10 Luis Work 018.jpg
-rw-r--r-- 1 kumi kumi 687920 2007-10-30 01:10 Luis Work 028.jpg
-rw-r--r-- 1 kumi kumi 731860 2007-10-30 01:10 Luis Work 029.jpg
-rw-r--r-- 1 kumi kumi 868742 2007-10-30 01:10 Luis Work 030.jpg
-rw-r--r-- 1 kumi kumi 853904 2007-10-30 01:10 Luis Work 042.jpg
-rw-r--r-- 1 kumi kumi 725632 2007-10-30 01:10 Luis Work 070.jpg
-rw-r--r-- 1 kumi kumi 500695 2007-10-30 01:10 Pic 1.jpg
drwxr-xr-x 2 kumi kumi   4096 2007-12-12 22:06 untitled folder/
kumi@throne:atlas/images/photos % ./gallery 
mv: target `1.jpg' is not a directory
mv: target `2.jpg' is not a directory
mv: target `3.jpg' is not a directory
mv: target `4.jpg' is not a directory
mv: target `5.jpg' is not a directory
mv: target `6.jpg' is not a directory
mv: target `7.jpg' is not a directory
mv: target `8.jpg' is not a directory
mv: target `9.jpg' is not a directory
mv: target `10.jpg' is not a directory
mv: target `11.jpg' is not a directory

here's the script:
Code:
#!/bin/bash
filetype="jpg"
list=( *.$filetype )
for (( i = 0 ; i < ${#list[*]} ; )) ; do
    mv ${list[$i]} $((++i)).$filetype
done

any help?
# 13  
Old 12-13-2007
Common pitfall. The "list=( *.$filetype )" style is quite efficient except:

1. It screws up with filenames with spaces
2. It screws up with empty directories

Similarly avoid "for i in ... ; do" style, when you anticipate 1 & 2 mentioned above.

A better way is manage these troubles is:

Quote:
ls -1 *.jpg |sort |while read fname ; do
<whatever on "${fname}">
done
HTH
# 14  
Old 12-13-2007
thanks for the advice buddy, but it left me in a worse position than before:


Code:
kumi@throne:atlas/images/photos % ls -a1
./
../
gallery*
.gallery.swp
.jpg
Luis Work 001.jpg
Luis Work 008.jpg
Luis Work 009.jpg
Luis Work 010.jpg
Luis Work 016.jpg
Luis Work 017.jpg
Luis Work 018.jpg
Luis Work 028.jpg
Luis Work 029.jpg
Luis Work 030.jpg
Luis Work 042.jpg
Luis Work 043.jpg
Luis Work 044.jpg
Luis Work 045.jpg
Luis Work 046.jpg
Luis Work 055.jpg
Luis Work 068.jpg
Luis Work 069.jpg
Luis Work 070.jpg
untitled folder/

kumi@throne:atlas/images/photos % ./gallery jpg
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "001.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "008.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "009.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "010.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "016.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "017.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "018.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "028.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "029.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "030.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "042.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "043.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "044.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "045.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "046.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "055.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "068.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "069.jpg"
Warning: unknown mime-type for "Luis" -- using "application/*"
Warning: unknown mime-type for "Work" -- using "application/*"
Error: no such file "Luis"
Error: no such file "Work"
Error: no such file "070.jpg"

here's the script:
Code:
#!/bin/bash

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

pretty simple script, but it's in bad shape. apparently even this method has trouble with spaces in filenames... can someone just explain to me what the problem is and why, so i can fix it myself, instead of trying scripts that don't work? please?

Last edited by visitorQ; 12-13-2007 at 01:10 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