Results 1 to 10 of 12
I've been given this task to do manually, but I think a very simple Bash script could do this. I know nothing about it though, so any help is appreciated..
...
- 06-06-2007 #1
Need a simple bash script..
I've been given this task to do manually, but I think a very simple Bash script could do this. I know nothing about it though, so any help is appreciated..
I've got some 5000+ images, and I need to create a text file for each image that has the same name, but a .txt extension..
e.g.
If I have one.tif, two.tif, three.tif, then I need to make
one.txt, two.txt, three.txt..
I'm obviously not going to do this manually.. it might take a week or something.. any ideas?
Thanks in advance,
- 06-06-2007 #2Just Joined!
- Join Date
- Dec 2006
- Posts
- 52
for i in `ls -R *.tif`
do
bse=`echo ${i} | cut -d. -f1`
touch ${bse}.txt
done
This can be done on command line...
John
- 06-06-2007 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
Code:ls -R *tif | while read line; do touch ${line/tif/txt}; done
- 06-07-2007 #4
- 06-12-2007 #5
This doesn't work at all..
This only worked once.. other than that it doesn't work..
Can someone think of a reliable script that will work? Maybe even something that operates on any format (not just TIF).. since I only have images in these particular directories..
It's really important that I figure this out, because it's for work (cell images in a lab) and I'm on a deadline. I might have to stay up all night to do this manually if this doesn't work..
Thank you,
- 06-12-2007 #6
This should work for you.
For use in a shell script:
For use at the shell:Code:#!/bin/bash for img in $(ls -1 | egrep '(.png$|.bmp$|.jpg$|.tif$|.gif$)') do touch $(echo $img | awk -F"." '{print $1}').txt done
Note that you can use however many file extensions you want by separating them with a | and putting a $ at the end to make sure you only get files that end exactly with that extension. Remove the $'s if you don't care about this.Code:for img in $(ls -1 | egrep '(.png$|.bmp$|.jpg$|.tif$|.gif$)'); do touch $(echo $img | awk -F"." '{print $1}').txt; done;
Also note that this script uses a '.' (period) as the separator in the filename. If the filename has multiple periods this won't work so well.
Enjoy!
- 06-12-2007 #7Linux User
- Join Date
- Jun 2007
- Posts
- 318
Note that in egrep the period '.' means any character so something like 'file.ttif' will match. To search for a period preceed it with the escaped character(\). The egrep string should be:
egrep '(\.png$|\.bmp$|\.jpg$|\.tif$|\.gif$)'
To handle filenames with multiple periods in them and assuming the file extensions you're searching for is always 3 characters:
#!/bin/bash -vx
typeset -i len
for img in $(ls -1 | egrep '(\.png$|\.bmp$|\.jpg$|\.tif$|\.gif$)')
do
touch $(len=${#img}; len=len-4; echo "$img $len" | \
awk '{print substr($1,1,$2)}').txt
done
- 06-12-2007 #8
thanks guys..
I'll post back tomorrow and let you know if it worked.. using puppy 2.16 fwiw.. Some of the files have multiples periods, and a lot of the files have spaces.. there's something like 10k images now, so I hope spaces are ok..
thanks again,
- 06-12-2007 #9Linux User
- Join Date
- Jun 2007
- Posts
- 318
Spaces in the filenames, hmm. You have to make it difficult don't you.

My script will break. This should handle filenames with spaces in them:
#!/bin/bash -vx
typeset -i len
ls -1 | egrep '(\.png$|\.bmp$|\.jpg$|\.tif$|\.gif$)' | while read img
do
touch "$(len=${#img}; len=len-4; echo "$img^$len" | \
awk -F^ '{print substr($1,1,$2)}').txt"
done
This assumes there are no filenames with '^' in them. If there are then use some other character.
- 06-13-2007 #10
works perfectly! thanks a million.. you're a legend

Just a couple more things (our of curiosity more than anything):
1) Is it possible to modify it to include subfolders?
2) Is it possible to save is a bash function (not too sure how to do that) ... or maybe some sort of complex alias?
...oh and just trying to make sense of the code.. am I right in guessing that if I had file extensions such as .jpeg or .tiff, then I could just change it like this:
Code:#!/bin/bash -vx typeset -i len ls -1 | egrep '(\.png$|\.bmp$|\.jpg$|\.tif$|\.gif$)' | while read img do touch "$(len=${#img}; len=len-5; echo "$img^$len" | \ awk -F^ '{print substr($1,1,$2)}').txt" doneLast edited by phalaris_trip; 06-13-2007 at 10:55 AM. Reason: code


Reply With Quote
