Beginner - scripting help!

hi all, i am very new to linux and am trying to create a basic script.

I would like the script to copy files from one directory into another, (e.g Script ~/my-documents/fileone ~/my-documents/filetwo)

Once all files have been copied, i'd like another script to run automatically and rename the files inside the directory (filetwo) to filetwo1.jpg, filetwo2.jpg, filetwo3.jpg etc etc (sorry for the confusing names).

I am really struggling and dont even know how to begin, any help would be really appreciated.

thanks guys

Is this a homework assignment?

What have you tried?

Why create two scripts? Why not rename the scripts as you move them the first time?

Please show your attempts to script one, which, unless there's additional conditions that you don't mention in your post, would not contain much more than a simple single cp command. And, as the renaming could be done in the same step, your second script might be superfluous. Is there any pattern/logics/algorithm defining which file becomes file 1, 2 etc?

No it is not homework.

My purpose of doing this is not to simply run the script for the example i have given, but for any directory that is instructed.

So far i have managed to make the files copy across into the new directory, i done this by:

#!/bin/sh
if cp -r "$1" "$2"
then echo "copy success!"
else echo "copy failed!"
fi

Now i'd like to for the script to rename all the files within each directory of this directory, according to the directory name, for example if a directory is called help, the .jpg files inside would be renamed help.jpg, help2.jpg, help3.jpg. This is where it seems to get complex, a little steer in the right direction with some examples would be appreciated!

thanks guys

Please use code tags as required by forum rules!

What shell are you using? Your script's shebang indicates /bin/sh, but mayhap you got a recent version of bash or ksh at hand?

Sorry i will do! I am scripting within puppylinux distro, been trying to get my head around sh/bash at the moment!

So, with bash , you could try

for i in $1/*; do echo cp $i $2/${2##*/}$((++CNT)).jpg; done

and report back. The echo just prints out what would be executed in case the echo were removed.

Thanks, that worked a little, this is what happened: i have two directories, one named test and one named moved.

Data was copied from test to moved succesfully.

Two files were renamed inside the moved folder, they were renamed moved1.jpg and moved3.jpg.

Inside the moved folder there is another folder called test, the files in here were not renamed unfortunately. there is also another folder within test named data, which again was unchanged.

console says:

cp: omitting directory /root/test/data

any ideas? Thanks for your help

There's no reason moved2.jpg should be missing except the file that was encountered in the second place was not a normal file. Directories in the moved folder will NOT be touched; only files copied from the original directory will be renamed when copied.

The message that you post (alas again without code tags) does not match the directory structure that you talked about. I'd propose you elaborate carefully your situation and requirements.

The structure is different as i'd like the script to work whatever the environment, there could be many subdirectories or only one subdirectory for example, i was just testing the script in different variables.
The files within test and data (copied already) did not rename according to the directory names, however the parent directory seemed to work fine!

---------- Post updated at 12:53 PM ---------- Previous update was at 12:46 PM ----------

perhaps the renaming can be achieved during the copy process? Or copying first then renaming after the more reasonable option?

thanks again

Some relevant information is missing.
Files in the "test" sub folder, should they be named "test..."? (Or "moved..." i.e. the parent folder?)
Should the "test" sub folder be created if not yet present?

should be named test... and also yes it should be created :):smiley:

---------- Post updated at 04:07 PM ---------- Previous update was at 04:01 PM ----------

I have written up a more detailed explanation of what i need to get done :

find is a very good tool for recursion into directories.
The following implementation meets most of your requirements

#!/bin/bash
# strip a trailing /
src=${1%/}
dest=${2%/}
export PATH=/bin:/usr/bin:/usr/sbin:/sbin
# only a cd and pwd does a reliable pathname resolving
dpwd=$(cd "$dest"&& pwd)
spwd=$(cd "$src" && pwd)
if [ "$spwd" = "$dpwd" ]; then
  echo "src and dest are identical ($dpwd)"
  exit 1
fi

#find "$src" -depth -type f \( -name "IMG*" -o -name "*.???" \) -print |
# -o is or
# -a is default and can be omitted
# here \( -name "IMG*" -name "*.???" \) can be simplified
find "$src" -depth -type f -name "IMG*.???" -print |
while IFS="" read -r sname
do
  # strip the leading src and replace with dest
  ddir=$dest${sname#$src}
  # strip the trailing filename
  ddir=${ddir%/*}
  if [ "$ddir" != "$pddir" ]; then
    # "new" folder: 'find' can visit a folder several times
    pddir=$ddir
    echo mkdir -p "$ddir"
  fi
  # get the filename extension
  ext=${sname##*.}
  # compose the base dest filename
  dname=$ddir/${ddir##*/}
  # find a free number for dname
  CNT=0; while [ -e "$dname$((++CNT)).$ext" ]; do :; done
  echo cp "$sname" "$dname$CNT.$ext"
done

The comments will help you to understand and further adapt this.