Execution problem Shell script to copy files to folder and rename them sequentially

Discussion in 'Mac Programming' started by amanases, Jan 18, 2015.

  1. amanases, Jan 18, 2015
    Last edited: Jan 18, 2015

    amanases macrumors newbie

    Joined:
    Jan 17, 2015
    Location:
    Wichita Falls
    #1
    I need a program and I think a shell program will work.

    I need a program that will read each line and copy a specific JPEG to a folder and rename it sequentially as it copies them. It has to be in the order that the list.txt is in.

    I have had some luck with writing a shell script for it.

    I have a text file named list.txt
    it looks exactly like this when you open it
    and the picture that each of these contains is a picture of the letter or number or punctuation.
    Code:
    b.jpeg
    e.jpeg
    g.jpeg
    i.jpeg
    n.jpeg
    n.jpeg
    i.jpeg
    n.jpeg
    g.jpeg
    spacebar.jpeg
    g.jpeg
    o.jpeg
    d.jpeg
    spacebar.jpeg
    c.jpeg
    r.jpeg
    e.jpeg
    a.jpeg
    t.jpeg
    e.jpeg
    d.jpeg
    
    
    the files above write "beginning god created", but I want to do this to a book. or anything else I want.

    I have been using these commands
    Code:
    
    cp `cat list.txt` new-folder/
    
    
    This sorta successfully copies the files listed in list.txt but won't copy a file with the same name in the same folder.
    The date/time when it was copied to the folder on each file isn't accurate because of the duplicate file name issue.
    Since I am only using pictures of the 26 letters and 10 numbers and punctuation of the alphabet, I will obviously be running into the duplicate file name issue, I will be copying the same files many times over depending on the text in list.txt
    So I figure that I need to rename them as they are pasted in the new folder.

    So I use this to rename them.

    Code:
    find . -name '*.jpeg' \
    | awk 'BEGIN{ a=0 }{ printf "mv %s %04d.jpeg\n", $0, a++ }' \
    | bash
    
    
    This successfully renames the original JPEG's, but in the wrong folder and I need the ones copied to be renamed, not the source JPEG's

    and finally this piece to somehow combine them
    Code:
    cat [filename] | while read line; do [command] "$line"; done
    
    
    
    Ive tried all kinds of mutations to get it to work and it looks something like this

    Code:
    cd Desktop/test
    cp `cat list.txt` new-folder/ | while read line; do find . -name '*.jpeg' | awk 'BEGIN{ a=0 }{ printf "mv %s %04d.jpeg\n", $0, a++ }' | bash "$line"; done
    
    

    what am i missing? and doing wrong? I also need it to wait for each JPEG to be copied and renamed before going to the next line in list.txt
    I need the files copied and renamed in order. If you help me, treat me stupid when it comes to programming, because I am pretty much clueless.
     
  2. cqexbesd macrumors regular

    Joined:
    Jun 4, 2009
    #2
    Not 100% sure I understood the question, but what about:

    Code:
    awk '{ system(sprintf("ln %s new-folder/%04d-%s", $1, NR, $1)); }' list.txt
    
    I use ln rather than cp because you presumably don't really want to duplicate all those images.

    It may be important to note that there is no inherent ordering of files in a directory. The above adds a 4 digit number so that if you sort them by file name you get the same order that they appeared in the text file.

    While the above hopefully answers your question I do wonder if you are trying to solve your original problem in the best way...
     
  3. amanases thread starter macrumors newbie

    Joined:
    Jan 17, 2015
    Location:
    Wichita Falls
    #3
    I have moved this matter over to the Unix forum at
    http://www.unix.com/shell-programming-and-scripting/254652-problem-scripting-copy-renaming-shell-executable.html#post302932076

    I'm having better luck over there, and I think it is more suitable place.
     

Share This Page