Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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.. ...
  1. #1
    Just Joined! phalaris_trip's Avatar
    Join Date
    Jan 2007
    Posts
    21

    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,

  2. #2
    Just 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

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
     ls -R *tif | while read line; do touch ${line/tif/txt}; done

  4. #4
    Just Joined! phalaris_trip's Avatar
    Join Date
    Jan 2007
    Posts
    21
    that's great, thanks for fast replies

  5. #5
    Just Joined! phalaris_trip's Avatar
    Join Date
    Jan 2007
    Posts
    21
    Quote Originally Posted by ennoil View Post
    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
    This doesn't work at all..

    Quote Originally Posted by ghostdog74 View Post
    Code:
     ls -R *tif | while read line; do touch ${line/tif/txt}; done
    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,

  6. #6
    Just Joined! Sivel's Avatar
    Join Date
    Jun 2005
    Location
    Maryland
    Posts
    20
    This should work for you.

    For use in a shell script:

    Code:
    #!/bin/bash
    
    for img in $(ls -1 | egrep '(.png$|.bmp$|.jpg$|.tif$|.gif$)')
    do
            touch $(echo $img | awk -F"." '{print $1}').txt
    done
    For use at the shell:

    Code:
    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.

    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!

  7. #7
    Linux 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

  8. #8
    Just Joined! phalaris_trip's Avatar
    Join Date
    Jan 2007
    Posts
    21
    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,

  9. #9
    Linux 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.

  10. #10
    Just Joined! phalaris_trip's Avatar
    Join Date
    Jan 2007
    Posts
    21
    Quote Originally Posted by vsemaska View Post
    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.
    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"
    done
    Last edited by phalaris_trip; 06-13-2007 at 10:55 AM. Reason: code

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...