Problem scripting a copy and renaming shell executable

I also posted this on macrumors forum, then i realized that this is a more suitable forum for matters like this. I apologize for the username, I was looking at a bag of doritos when it asked me for a username. lol

I need a program (see below for what I've tried) 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.

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


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.

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

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

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 to 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.

Heya Dorito, my love :stuck_out_tongue:

Whut?

You want to create a book using images as letters?
First and foremost, you dont want to have a complete book with each letter as a picutre.
By that i mean, there will be thousends of a.jpg, s.jpg and probably ten-thousens of spacechar.jpg....

You want to reuse each image representing a char.

So my question is, how to you plan to make them a book?
Actualy, another more important question rises, is this some homework?
If so, please see: Rules for Homework & Coursework Questions Forum

Thank you & Have a good day

It's not going to be just pictures of letters of text in books but things that bring emotion when you see the picture. No I'm not in school, no homework nothing like that. I'm retired navy. Honorable medical discharge. Anyway I'm going to put the pictures in a time lapse and then I'm going to speed it up till each picture only flashes for about 16 milliseconds. I do tree removal time lapses for my dad's business. I set up my camera to take a picture every 5 seconds for 5 or so hours then we take the tree down and then I turn the pictures into a 1 minute timelapse.
When I do a tree timelapse I put the pictures into the timelapse program and it is a big video 3000 pictures about 20 GB but then I put it through iMovie and speed it up and it's a very small file size. Like 45MB
its an idea/experiment i had 3 days ago, and I'm try to put it together. I just need the script to do the copying and sequentially renaming.

You could try something like:

Folder content:

letters.sh
letters/{a-zA-Z0-1}.jpg
example-text.txt

example-text.txt

God gave us a free will.
And he loves us.

letters.sh

#!/bin/bash
# Prepare - dummy files
#for char in {a..z} {A..Z} {0..9} 
#do
#	touch $char.jpg
#done

[[ -z "$1" ]] && \
	echo "$0: Requires a file to parse!" && \
	exit 1

[[ ! -f "$1" ]] && \
	echo "$0: Requires a file to parse!" && \
	exit 1

TEXT_FILE="$1"
CHAR_DIR="$(dirname $0)/letters/"

while read line;do
	len=${#line}
	c=0
	while [[ $c -lt $len ]]
	do 	char=${line:$c:1}
		img="$CHAR_DIR/$char.jpg"
		
		# WHAT TO DO WITH CHAR (-image) ?
		echo "Current char: $char -> $img"
		
		c=$(( $c + 1))
	done
	echo "------------"
done<"$TEXT_FILE"

Outputs as:

./letters.sh example-text.txt

Current char: G -> ./letters//G.jpg
Current char: o -> ./letters//o.jpg
Current char: d -> ./letters//d.jpg
Current char:   -> ./letters// .jpg
Current char: g -> ./letters//g.jpg
Current char: a -> ./letters//a.jpg
Current char: v -> ./letters//v.jpg
Current char: e -> ./letters//e.jpg
Current char:   -> ./letters// .jpg
Current char: u -> ./letters//u.jpg
Current char: s -> ./letters//s.jpg
Current char:   -> ./letters// .jpg
Current char: a -> ./letters//a.jpg
Current char:   -> ./letters// .jpg
Current char: f -> ./letters//f.jpg
Current char: r -> ./letters//r.jpg
Current char: e -> ./letters//e.jpg
Current char: e -> ./letters//e.jpg
Current char:   -> ./letters// .jpg
Current char: w -> ./letters//w.jpg
Current char: i -> ./letters//i.jpg
Current char: l -> ./letters//l.jpg
Current char: l -> ./letters//l.jpg
Current char: . -> ./letters//..jpg
------------
Current char: A -> ./letters//A.jpg
Current char: n -> ./letters//n.jpg
Current char: d -> ./letters//d.jpg
Current char:   -> ./letters// .jpg
Current char: h -> ./letters//h.jpg
Current char: e -> ./letters//e.jpg
Current char:   -> ./letters// .jpg
Current char: l -> ./letters//l.jpg
Current char: o -> ./letters//o.jpg
Current char: v -> ./letters//v.jpg
Current char: e -> ./letters//e.jpg
Current char: s -> ./letters//s.jpg
Current char:   -> ./letters// .jpg
Current char: u -> ./letters//u.jpg
Current char: s -> ./letters//s.jpg
Current char: . -> ./letters//..jpg
------------

Hope this helps to get you started.
If not, i'm not getting/understanding what you try to achieve.

---------- Post updated at 09:22 ---------- Previous update was at 09:18 ----------

Ahh yes... main cause is.... AFAIK there is no way to 'flash' an image in the shell. (to display anyway - with the exception of framebuffer - above my knowhow.)

I'm not going to do the time lapse with shell. I just need the shell to copy specific files and rename them

---------- Post updated at 02:28 AM ---------- Previous update was at 02:26 AM ----------

I don't see any cp command or sequential renaming in that script

Again, (just as far i understand you, which seems its not 'that' far), it makes now sense to copy the char-images, just make a one folder containing these, and call the specific image upon need.

Reuse what is already existing, there is no sense in having 300 letter a.jpg in several folders.

If that is not what you want to achieve, please elaborate more about that copy issue you mention, because from your first post, its quite (very) unclear to me what you actualy want to do OR what the precice issue is.
Specialy since you use a mv command rather that cp ...

---------- Post updated at 09:32 ---------- Previous update was at 09:32 ----------

Neither do i in yours.
In mine its not required.

My code I tried worked partly, I got it to copy the specific Jpegs but it wouldn't copy ones again that existed in the folder, and I got it to sequentially rename the files but it renamed the source files and not the copied ones. I just don't know how to combine the functions

Well, but the first:

cp `cat list.txt` new-folder/

--->

while read entry;do  
    cp $entry new-folder/
done<list.txt

Maybe this helps? (nevermind, its the same, got confused by those quotes which are supposed to execute, please use $( command ) , it helps to read and helps to differ between commands and strings.

---------- Post updated at 09:35 ---------- Previous update was at 09:34 ----------

Yes, obviously...
As i already stated, there is no sense to copy 300 char a.jpgs....

But I figure that if I can get the shell to rename the copied jpeg as it is pasted in the other folder then I won't run into the duplicate file name issue

Doh, me not really wake yet, sorry.. i guess i figured it.

find . -name '*.jpeg' \
| awk 'BEGIN{ a=0 }{ printf "mv %s %04d.jpeg\n", $0, a++ }' \
| bash

If you run this in the original path, it will not rename the files in new-folder, unless new-folder is a child of the current path. (??)
If that is the case, i'm sorry i couldnt help and i'm out then.

Good luck.

I think you understand. I have one folder that contains the Jpeg's (for this version of the experiment , this folder has 26 lowercase letters 10 numbers and a spacebar jpg) and the list.txt. I need the shell to copy the first jpeg listed in list.txt, and then paste it in the destination folder rename it to 0001.jpg, Wait for it to paste and then move on to the next line of list.txt and repeat

One possibility is to rename on copy .

eg (warning untested code)

index="0000000"
for image in $(cat list.txt) ; do 
 cp ${SRC_DIR}/$image new_folder/${index}.jpg
 index = $(printf %06d $(( $index + 1 )) )
done
find . -name

I know that period means it will rename the files in the current directory, but I need it to rename the ones it just copied in the new-folder
I don't know what I am doing, I just thought of this thing 3 days ago.

---------- Post updated at 03:21 AM ---------- Previous update was at 03:09 AM ----------

I know nothing about how to code except what I've researched in the last 2 days.

This does NOT apply if it is: ./new-folder

Try:

find /path/to/new-folder -name '*.jpeg' \
| awk 'BEGIN{ a=0 }{ printf "mv %s %04d.jpeg\n", $0, a++ }' \
| bash

hth
(note: my awk 'knowledge' is limited, but it looks right'ish, not sure about the last pipe to | bash though)

index="0000000" # set a starting index of 0 but with 7 digits to allow for up to 10M characters.
for image in $(cat list.txt) ; do # setp through list.txt one line at a time
 cp ${SRC_DIR}/$image new_folder/${index}.jpg # copy the apropriate image from the images source directory to a file named "the value of the index".jpg
 index = $(printf %06d $(( $index + 1 )) ) # increment the value of the index
done

So you would (in the example above) be copying
b.jpeg => 0000000.jpg
e.jpeg => 0000001.jpg
g.jpeg => 0000002.jpg
i.jpeg => 0000003.jpg
...

This would ensure that the sort order was the same as the book order if you were to generate a slideshow of the directory.

I'll have to wait till later to test it. Another thing I thought about. The copy time for each jpeg, each jpeg is between 100KB to 250KB, so I would run into a copy time delay for each depending on the file size. However small. I don't expect it to copy the sequence of images in list.txt all at once, a delay for each copy might be needed? So as to not overload the computer with a sudden long list of copying to do

How about a softlink?
This would not only save quite a bit of diskspace, but also be alot faster upon creation (i think).

As in, instead of:

cp a.jpg 000000.jpg

You do:

ln -s a.jpg 000000.jpg

hth

I'm not familiar with softlink. I'm guessing it is a a shortcut. I did see the option when I was reading the man for the commands for a symbolic link. I would have to wait till I get home to see if the time lapse program will accept it.

---------- Post updated at 11:52 AM ---------- Previous update was at 11:46 AM ----------

Whenever it is going to copy the first jpeg of the list.txt " let's say it is a.jpeg , it copies and renames that one 0000001.jpeg, then 4 lines later it has to copy another a.jpeg but rename this one 000005.jpeg", because I can't have the same name files in the same folder.

---------- Post updated at 01:38 PM ---------- Previous update was at 11:52 AM ----------

i ran this

cd Desktop/sourcepictures
index="0000000" 
for image in $(cat list.txt) ; do 
cp ${sourcepictures}/$image orderedpictures/${index}.jpeg
index = $(printf %06d $(( $index + 1 )) )
done

and it says this


Last login: Mon Jan 19 13:28:21 on ttys000
timothys-mbp:~ timothynorris$ /Users/timothynorris/Desktop/new\ test.command ; exit;
cp: /i.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /n.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /spacebar.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /t.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /h.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /e.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /spacebar.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /b.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /e.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /g.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /i.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /n.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /n.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /i.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /n.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /g.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /spacebar.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /G.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /o.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /d.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /spacebar.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /c.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /r.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /e.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /a.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /t.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /e.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /d.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /spacebar.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /t.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /h.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
cp: /e.jpeg: No such file or directory
/Users/timothynorris/Desktop/new test.command: line 5: index: command not found
logout

[Process completed]


i have the code in a file named "test.command" on the desktop

i have the folder named "sourcepictures" on the desktop and the jpeg files are in that folder and inside the "sourcepictures" folder i have the folder named "orderedpictures". also, list.txt is in "sourcepictures" folder. it found the list.txt

what do i have to put in the script to get it to find the files and directories

You've used php/lua/java settings to assign a value, but its bash.
index = $(printf %06d $(( $index + 1 )) ) --> index=$(printf %06d $(( $index + 1 )) )

And regarding the folders, i now assume it lookes like (yes the renaming of the folders is on purpose, just for the example):

  • $HOME/Desktop/test.sh
  • $HOME/Desktop/pictures-source
  • $HOME/Desktop/pictures-ordered

Now according to your posted code snippet, i would change that to:
test.sh

#!/bin/bash
SOURCES="$HOME/Desktop/sourcepictures"
TARGET="$HOME/Desktop/orderedpictures"
LIST="$HOME/Desktop/list.txt"
index="0000000" 

for image in $(cat $LIST) ; do 
    ln -s $SOURCES/$image  $TARGET/$index.jpg
    index=$(printf %06d $(( $index + 1 )) )
done

If list.txt contains only the chars (a,b,c,d...) you need to add ".jpg" after "$image".
If list.txt contains the proper image name, you wont need to change anything.

hth & gn8

i ran this exactly

#!/bin/bash
SOURCES="$HOME/Desktop/sourcepictures"
TARGET="$HOME/Desktop/orderedpictures"
LIST="$HOME/Desktop/sourcepictures/list.txt"
index="0000000� 

for image in $(cat $LIST) ; do 
    ln -s $SOURCES/$image  $TARGET/$index.jpeg
    index=$(printf %06d $(( $index + 1 )) )
done


that worked for 8 pictures , it didnt copy the actual pictures and rename them instead it made a softlink/shortcut and renamed them in order, and the timelapse program i ran the shortcuts through made a video from it.

it does what i want it to but it stops at 8 pictures, does the index "0000000" have to be really long, i need this done for lots of pictures

it says this after i run it
how do i get it to run until the end of list.txt?

Last login: Mon Jan 19 15:31:50 on ttys000
timothys-mbp:~ timothynorris$ /Users/timothynorris/Desktop/test.command ; exit;
/Users/timothynorris/Desktop/test.command: line 9: 000008: value too great for base (error token is "000008")
ln: /Users/timothynorris/Desktop/orderedpictures/.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000001.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000002.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000003.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000004.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000005.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000006.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000007.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000008.jpeg: File exists
/Users/timothynorris/Desktop/test.command: line 9: 000008: value too great for base (error token is "000008")
ln: /Users/timothynorris/Desktop/orderedpictures/.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000001.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000002.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000003.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000004.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000005.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000006.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000007.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000008.jpeg: File exists
/Users/timothynorris/Desktop/test.command: line 9: 000008: value too great for base (error token is "000008")
ln: /Users/timothynorris/Desktop/orderedpictures/.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000001.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000002.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000003.jpeg: File exists
ln: /Users/timothynorris/Desktop/orderedpictures/000004.jpeg: File exists
logout

[Process completed]