Copying files from one directory to another, renaming duplicates.
Below is the script i have but i would like simplified but still do the same job.
I need a script to copy files not directories or sub-directories into a existing or new directory. The files, if have the same name but different extension; for example 01.doc 01.pdf then only copy the .doc file.
If the file names from different directories are the same the file needs to be copied and renamed; for example test/01.doc, pic/01.doc --> pic/01_1.doc
This is what i have...
Code:
#! /bin/bash
# echo "this file is for copying directory files"
INP=$1
OUT=$2
# creates the variables
if [ $# -ne 2 ]; then
echo "usage is param1 = input directory name"
echo "usage is param2 = output directory name"
echo "exiting"
exit
fi
if [ ! -d $1 ]; then
echo "$1 is not a directory please reinput"
echo "exiting"
exit
fi
if [ -d "$OUT" ]; then
echo "output directory exists"
else
echo "about to create output directory"
mkdir $OUT
fi
# checks for a valid Input directory, if this does not exist it will exit the script
# checks for a valid output directory if this does not exist it will create a new directory
IFILE="GETFILES";
FINDFILE="FINDFILE";
WORKDIR="WORKDIR";
mkdir $WORKDIR;
#get all the input files into one file
#this file can then be read and each file copied to an output dir
find $INP -name "*.doc" > $FINDFILE
find $INP -name "*.pdf" >> $FINDFILE
find $INP -name "*.PDF" >> $FINDFILE
while read -r line;
do
cp $line $WORKDIR
done < "$FINDFILE"
##searches the input directory and searches the heirarchal directory lookind for.doc files a inserts them into workfile and then repeats for the .pdf .PDF ####
INP=$WORKDIR;
echo " the inout dir should be working as $INP"
###this changes the name of original input directory to workdir ####
IFILE="$OUT/GETFILES";
ls -R $inp | egrep -i ".doc|.pdf" > $IFILE;
# count how many files are already in the input directory
# now read this output file line at a time
while read -r line;
do
### searches for .doc files and .pdf files and counts the amount in the input file.###
echo " the line that has just been read is ________ $line"
LCOUNT=0
DIFFCOUNT=0
FOUND=0
ARCHIVE=1
BASE="${line%.*}"
EXTENSION="${line##*.}"
echo "base name is $BASE"
echo "extension is $EXTENSION"
COUNT=$ (ls $OUT | grep ${line%%.*} | wc -l);
echo "count number of files in output dir is $Count"
echo "file is $line"
if [ $COUNT -eq 0 ];
then
cp $INP/$line $OUT;
echo "count means no file in output directory"
echo " so can just copy the file"
### copies and searches the files from the input directory to the output directory if the file has same name it then adds an _1###
###
else
echo "there is already a file in the output so need to compare"
COMP=$OUT/$line"
while [ "$FOUND" -eq 0 ] && [ $LCOUNT - lt $COUNT ]; do
echo "diffcount is $DIFFCOUNT"
### compares the file from the input directory to the file in then output directory ###
if [ "$DIFFCOUNT" -eq 0 ];
then
echo "file has already been archived no action required"
FOUND=$[ $FOUND+1 ]
else
LCOUNT=$ [ $LCOUNT+1 ]
COMP="OUT"/"$BASE"_"$LCOUNT"."$EXTENSION"
echo "line count for next compare is $LCOUNT"
echo "get the next file to compare"
echo "the comparison file is now $COMP"
if [ $LCOUNT -ne $COUNT ]; then
ARCHIVE=$ [ $ARCHIVE+1 ]
else
ARCHIVE=0
fi
### if the file in the inout directory is the same as the file in the output directory it will be called _1.doc, it will repeat this until it has checked the entire directory
fi
if [ $ARCHIVE -eq 0 ];
then
echo "now needs to archive with the latest generation number"
echo "as file does not exist but there are other generations"
echo "new generation number is $LCOUNT"
NEWOUT="OUT"/"$BASE"_"$LCOUNT"."$EXTENSION";
echo "newfile name is $NEWOUT"
cp $INP/$LINE $NEWOUT
fi
done
fi
done < "$IFILE"
rm $IFILE
#now run search for duplicate .pdf files
OFILE=$OUT/DOCFILES";
ls $OUT | grep ".doc" > $OFILE;
### put all the .doc files into a file to be read" ###
### now read this output file line at a time ###
while read -r line;
do
echo " the line that has just been read is ______ $line"
### this will then compare all the .doc files with .pdf files. ###
BASE="${line%.*}"
EXTENSION=${line##*.}"
NEWEXTENSION=".pdf"
SEARCHFILE=$BASE$NEWEXTENSION"
echo "base name is $BASE"
echo "extension is $EXTENSION"
echo "searchfile is $SEARCHFILE"
echo "if found will delete $SEARCHFILE so no duplicates as there is a .doc file"
find $OUT -name "$SEARCHFILE" -exec /bin/rm -f {} ';'
done < "OFILE"
rm $OFILE
### this will then remove any duplicate files so only individual .doc .pdf files will exist ###
Hi,
I have a problem.
I have some text files in a folder. The names can be like:
emp.txt
emp1.txt
emp3.txt
32emp4.txt
What i need is i have to copy all the files which have "emp" string in their filename
to a different folder and those file names... (7 Replies)
I have many files with duplicate names spread out over several tens of directories. I would like to mv them to the parent directory, but to avoid conflicting filenames I'd like to prefix each filename with the name of the directory it was in.
For example, if this is my directory structure:... (2 Replies)
suppose i have a few file like "a b c.txt" , "hello world.c" etc...(files that have space between them in their filenames).
how do i change their filenames to "a_b_c.txt" and "hello_world.c" respectively?
heres what i have tried...but failed
$ find -name "*" -exec mv "{}" "`echo {} | tr "... (4 Replies)
I would like to copy files from one directory to another directory while renaming them at the same time. Does anyone have any ideas on how to do this in a script?
Basically, would like to copy files from
/user/data/prod/*.txt
to
/user/data/bck/*.txt.bck (3 Replies)
Hi,
I am a newbie in shell scripting. I have to copy a particular sub-directory (data) from a large no. of directories (all in the same folder) and paste them to another directory ( /home/hubble/data ) and then rename all the subdirectories (data) as the name of its parent directory.
please... (8 Replies)
The goal is to read names of files defined ouside in upload.conf and rename them using date, time and proper extension. I have made short script
while read; do
cp "$RELAY" "$RELAY(date +%Y-%m-%d_%H:%M:%S)_DEPLOYED.ear"
done < upload.conf
but unfortunatelly it fails printiong the... (9 Replies)
hi,
source directory as /home/home01
target directort as /home/home02
I have below files in source directory:
FrontOf_history.dat
FrontOf_history1.dat
In target directory have many files as:
Kront_2014.dat
Kront_2015.dat
Kront_2016.dat
Now i want to copy the two files... (1 Reply)
Hi
I think this should be relatively simple but I can't figure it out. I have several files with the same name in different folders within a directory (the output of a program that I ran). Something like this:
./myAnalysis/item1/round1/myoutput.txt
./myAnalysis/item1/round2/myoutput.txt... (2 Replies)
Hi All,
I need to copy files from one directory to another with the files to be renamed while copying if a file with the same name already exists in the target directory.
THanks,
Dev (2 Replies)