-1

I have this snippet:

#!/bin/bash
parent=/parent
newfolder=/newfolder
mkdir "$newfolder"
for folder in "$parent"/*; do
if [[ -d $folder ]]; then
foldername="${folder##*/}"
for file in "$parent"/"$foldername"/*; do
filename="${file##*/}"
newfilename="$foldername"_"$filename"
cp "$file" "$newfolder"/"$newfilename"
done
fi
done

I do need to turn it around in a way that the copied files would be named after the folder they are being moved to (e.g. moving to the /root/Case22 files would be renamed to case22_1.jpg, case22_2.docx, case22_3.JPG etc). The files would be copied from USB and both destination and source directries would be entered by the user. I have written everything else and it works apart from actual renaming and thought I could adapt this snippet.

thanks

p.s. the snippet is written by Jahid and found on stackoverflow

d34d_d0g
  • 1
  • 1
  • 3
  • This is not a code writing service. YOU try adding the renumbering stuff, we (maybe) try help fix it. – Marc B Oct 21 '16 at 16:11
  • @Marc thanks for the constructive criticism. I was mislead by some of the posts and answers in the stackoverflow and lost my way ;) – d34d_d0g Dec 07 '16 at 12:50
  • Possible duplicate of [Rename multiple files in Unix](https://stackoverflow.com/q/1086502/608639), [https://stackoverflow.com/q/6911301/608639](https://stackoverflow.com/q/16541582/608639), [Rename multiple files by replacing a particular pattern in the filenames using a shell script](https://stackoverflow.com/q/6840332/608639), [Find directories with names matching pattern and move them](https://stackoverflow.com/q/22319557/608639), [Rename multiple files shell](https://stackoverflow.com/q/6911301/608639), etc. – jww May 11 '18 at 00:31

2 Answers2

1

you can try something like this;

#!/bin/bash
parent=/root
a=1
for file in $parent/Case22*; do
filename="${file%.*}"
extension="${file##*.}"
  newfilename=$(printf "$filename"_"$a"."$extension") 
  mv -- "$file" "$newfilename"
  let a=a+1
done
Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24
0

Thanks for the help. I have found the solution and thought I might post it here in case someone else will be looking at this. As the title suggests I needed a Linux shell script to copy and rename multiple files keeping original directory tree (the file source and archive locations would be specified by the user of the script). Here is the code that I came up with after few days research of different sources (it includes a trap so only one instance of script would be running at a time):

lockdir=/var/tmp/mylock         #directory for lock file creation
pidfile=/var/tmp/mylock/pid     #directory to get the process ID number


if ( mkdir ${lockdir} ) 2> /dev/null; then      #main argument to create lock file 
        echo $$ > $pidfile      #if successful script will proceed, otherwise it will skip to the else part of the statement at the end of the script
        trap 'rm -rf "$lockdir"; exit $?' INT TERM EXIT #trap to capture reasons of script termination and removal of the lock file so it could be launched again
        #start of the main script body, part of successful "if" statement 


#        read entry_for_testing_only #request for user entry to keep script running and try to run another script instance


        findir="$2/$(basename "$1")" #variable that defines final directory to which files from USB will be copied

        if [ ! -d "$1" ]; then #testing if first directory entry is a valid directory’’
         echo "$1" "is not a directory"
         echo ""
         exit
        else
         if [ ! -d "$2" ]; then #testing if second entry is a valid directory
          echo "archive directory non existant"
          exit
        else 
         if [ -d "$findir" ] && [ "$(ls -A "$findir")" ]; then #testing if second entry directory contains the same name folders and if the folders are empty - to avoid file overwriting
          echo "such folder already there and it's not empty"
          exit
                else 
                 if [ ! -d "$findir" ] ; then #last archive directory argument to create final archive directory
                         mkdir "$findir"
                 else true
                 fi
         fi
         fi
        fi

        rsync -a "$1"/ "$findir" #command to copy all files from the source to the archive retaining the directory tree


        moved_files="$(find "$findir" -type f)" #variable that finds all files that have been copied to the archive directory


        for file in $moved_files; do #start of the loop that renames copied files
         counter="$((counter+1))" #incrementation variable
         source_name="$(basename "$1")" #variable that captures the name of the source directory
         new_name="$source_name"_"$counter" #variable that puts start of the file name and incrementation element together
         if echo "$file" | grep "\." #argument that captures the extension of the file
          then 
           extension="$(echo "$file" | cut -f2 -d. )"
          else
           extension=
         fi
         full_name="$new_name"."$extension" #variable that defines the final new name of the file
                dir="$(dirname "${file}")" #variable that captures the directorry address of currently looped file
                mv "$file" "$dir/$full_name" #move command to rename currently looped file with the final new name
        done

        #end of the main script body, unsuccessful "if" statement continues here
else
        echo "Another instance of this script is already running. PID: $(cat $pidfile)"
fi
d34d_d0g
  • 1
  • 1
  • 3
  • Marked as accepted because it was working and did what it had to. Does not mean it is the most elegant or best way to achieve required result. – d34d_d0g Dec 07 '16 at 12:56