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.
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?
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, 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.
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?
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?
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.