Copy files in respective directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files in respective directories
# 15  
Old 03-21-2017
Quote:
Originally Posted by Master_Mind
Thanks Ravi for your help, One thing i wanted to know can you let me know why you care creating the directory for other files apart from fish, because i already have source directory created only for fish file i need to create directory with date folder

Code:
dir/fis/date

Code:
then  if [[ ! -d " path " ]] ; then  mkdir " \
                                        path " echo " path " got created successfully. ; \

And also I think I can your previous code that file successfully copied /not copied / file not present in input directory message in same function
Hello Master_Mind,

That was to make sure in case directory is not present it should create it else it will simply copy it. Let me know if you have any queries on same.

Thanks,
R. Singh
# 16  
Old 03-21-2017
Quote:
Originally Posted by RavinderSingh13
Hello Master_Mind,

That was to make sure in case directory is not present it should create it else it will simply copy it. Let me know if you have any queries on same.

Thanks,
R. Singh
Thanks ravi and one more thing just wanted to add regarding the echo messages hope I can add in same function. Once again is there a way to achieve in doing in loops instead of going with awk.
And one more thing Ravi, my files in input direcortry has mango_1.txt,apple_1.txt. you code will check exact file name in config file which fail in copying. I need to take if the file name contains like apple* and take the latest file only to copy

I tried something like this
Code:
conf=$1

filepath=/input/
output=/output/
for file in `cat $confFile` 
do
file_temp=$(ls -t1 $filepath|  grep -i $file | head -n 1)
echo $file_temp

if [ "$file" == "apple" ]
then
if [ -f $filepath$file_temp ]
then
	echo "Copying $file_temp file to location"

      	cp $filepath$bt_file $output/ap/ 

	if [ "$?" = "0" ]
	then
		echo "Successfully copied"
	else
		echo "failed copying"
	fi
else 
	echo "No apple file in $filepath "

fi

elif [ $file == "mango" ]
then
if [ -f $filepath$file_temp ]
then
	echo "Copying $file_temp file to location"
      	cp $filepath$file_temp $output/man/ 

	if [ "$?" = "0" ]
	then
		echo "Successfully copied"
	
	else
		echo "failed copying"	
	fi
else 
echo "No mango file in $filepath "    
fi
fi

done


Last edited by Master_Mind; 03-22-2017 at 01:17 AM.. Reason: code tried
# 17  
Old 03-25-2017
Hello Master_Mind,

As per your request following is in pure shell(uses GNU date too in it). Could you please try following and let me know if this helps you.
Code:
cat check_files.ksh
DIR=/tmp
SEARCH_DIR=`pwd`
DATE=$(date +%Y"/"%m"/"%d)
DATE_PREVIOUS_MONTH=$(date --date='-1 month' +%Y"/"%m"/"%d)
 
EXCEPTION_GET_FILE () {
                        for var in "$@"
                        do
                                exception_file=$var
                                if [[ -f $SEARCH_DIR"/"$exception_file ]]
                                then
                                        if [[ -d $DIR"/"$var"/"$DATE_PREVIOUS_MONTH ]]
                                        then
                                                mkdir $DIR"/"$var"/"$DATE
                                                cp -r $DIR"/"$var"/"$DATE_PREVIOUS_MONTH/*  $DIR"/"$var"/"$DATE
                                        else
                                                echo "Previous month directory " $DIR"/"$var"/"$DATE_PREVIOUS_MONTH " is NOT present for exception file named " $exception_file
                                                if [[ -d $DIR"/"$var ]]
                                                then
                                                        mkdir $DIR"/"$var"/"$DATE
                                                else
                                                        mkdir -p $DIR"/"$var"/"$DATE
                                                fi
                                                cp $exception_file $DIR"/"$var"/"$DATE
                                                CHECK_FILES_COPIED_STATUS $DIR $var"/"$DATE $exception_file
                                        fi
                                else
                                        echo "File named " $exception_file " itself NOT found in directory " $SEARCH_DIR
                                fi
                        done
                      }
CHECK_FILES_COPIED_STATUS () {
                                DIR=$1
                                dir=$2
                                REPLY=$REPLY
                                if [[ $? == 0 ]]
                                then
                                        echo "File named " $REPLY " has been successfully copied to directory named " $DIR"/"$dir " now."
                                else
                                        echo "please check seems there is some issue with file copy."
                                fi
                             }
GET_FILE () {
for var in "$@"
do
        input=${var%,*}
        dir=${var##*,}
if [[ -f $SEARCH_DIR"/"$input ]]
then
        while IFS=  read -r -d $'\0'
        do
                if [[ ! -d $DIR"/"$dir ]]
                then
                        mkdir $DIR"/"$dir
                        cp $REPLY $DIR"/"$dir
                        CHECK_FILES_COPIED_STATUS $DIR  $dir
                else
                        cp $REPLY  $DIR"/"$dir
                        CHECK_FILES_COPIED_STATUS $DIR  $dir
                fi
        ##echo $REPLY
        done < <(find $SEARCH_DIR -iname "${input}*" -print0)
else
        echo "File named " $input " NOT found in directory " $SEARCH_DIR
fi
done
            }
## Calling normal files to be copied here..
GET_FILE apple,app  mango,man
## Calling exception files to be checked and copied from last month's date to today's date....
EXCEPTION_GET_FILE fish

Above script's output will be as follows.
Code:
 ./check_files.ksh
File named  /singh_is_King/prince/R. Singh/apple_1.txt  has been successfully copied to directory named  /tmp/app  now.
File named  /singh_is_King/prince/R. Singh/apple  has been successfully copied to directory named  /tmp/app  now.
File named  /singh_is_King/prince/R. Singh/apple12111  has been successfully copied to directory named  /tmp/app  now.
File named  /singh_is_King/prince/R. Singh/apple1.txt  has been successfully copied to directory named  /tmp/app  now.
File named  /singh_is_King/prince/R. Singh/mango  has been successfully copied to directory named  /tmp/man  now.
File named  /singh_is_King/prince/R. Singh/path/mango  has been successfully copied to directory named  /tmp/man  now.
File named  /singh_is_King/prince/R. Singh/mango1211.txt  has been successfully copied to directory named  /tmp/man  now.
File named  /singh_is_King/prince/R. Singh/mango1211  has been successfully copied to directory named  /tmp/man  now.
File named  fish  itself NOT found in directory  /singh_is_King/prince/R. Singh

So above code will look for all the files with similar names like mango, mango1211.txt, mango1211 etc. Also there are 2 functions in above script named GET_FILE and EXCEPTION_GET_FILE so as per name suggests itself function named EXCEPTION_GET_FILE for files like fish etc. So here is how you need to run them, we need to pass arguments like EXCEPTION_GET_FILE exception_file_name and GET_FILE file_name,it's directory_name another_file_name,it's directory_name and so on. Also for exception files I am not checking similar names, I am exactly checking the same name as exception file name only.

Kindly check it and let me know if this helps you.

NOTE: I placed few test files with similar names into my test directory for testing purposes. Also exception function doesn't have argument of it's directory name so if you need it you could take a look to normal function one and could try to take same method into exception function too.

Thanks,
R. Singh

Last edited by RavinderSingh13; 03-25-2017 at 04:37 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recursive search for files and copy to new directories

So I have extremely limited experience with shell scripting and I was hoping someone could point out a few commands I need to use in order to pull this off with a shell script like BASH or whatnot (this is on OS X). I need to search out for filenames with account numbers in the name itself... (3 Replies)
Discussion started by: flyawaymike
3 Replies

2. Shell Programming and Scripting

create more than 100 directories and copy files into them

Hi, I have several files containing experiment measurements per hour (hour_1.txt has measurements for first hour, etc..etc..). I have 720 of these files (i.e. up to hour_720.txt) and i want to create 720 directories and in every one of them i want to copy its associative file (e.g.... (4 Replies)
Discussion started by: amarn
4 Replies

3. Shell Programming and Scripting

Copy files by automatically creating directories

I am in a situation where I have some hundreds of thousands of files in a directory (name of directory: big_directory). Now, working on this directory is extremely slow. I now plan to do this: Copy 1000 files in each of the directories by creating the directories automatically. All files have... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

4. Shell Programming and Scripting

Find all .htaccess files and make a backup copy in respective directories

Hey guys, I need to know how to locate all .htaccess files on the server and make a backup of them in the folder they reside before I run a script to modify all of them. So basically taking dir1/.htaccess and copying it as dir1/.htaccess_bk dir2/.htaccess copying as dir2/.htaccess_bk... (5 Replies)
Discussion started by: boxx
5 Replies

5. Shell Programming and Scripting

Copy respective path next to last column with awk

Hi to all, I have the short print out sample of the DOS command "dir S/" as showed below. Directory of C:\Program Files\Winamp\Skins\Bento\window 02/12/2010 11:35 p.m. <DIR> . 02/12/2010 11:35 p.m. <DIR> .. 11/12/2009 10:31 a.m. 13,556... (3 Replies)
Discussion started by: cgkmal
3 Replies

6. UNIX for Dummies Questions & Answers

how can i copy those files into other directories have the same name

how can i copy those files into other directories have the same name but different in the end i have files in directory called test: 10_10_asdadfsdfad.txt 10_10_11_asdawqefwkjasd.txt 10_10_11_12_asdafjjhoqwd.txt i want to put them in exist directory thart i have on my system i have... (1 Reply)
Discussion started by: t17
1 Replies

7. UNIX for Dummies Questions & Answers

Compare two directories and copy differing files

Hello there, I'm a total noob to shell scripting. :) What I want to do is compare the contents of Folder A and Folder B, and copy any files in Folder A that do not exist in Folder B over to Folder B. I have gotten so far as: diff -rq folderA folderB which returns the names of the files,... (3 Replies)
Discussion started by: raaaaaa
3 Replies

8. UNIX for Dummies Questions & Answers

Copy Directories excluding files

Hi guys, I want to copy folder and sub folders only. I don't want the files. If i use cp -r command it will copy entirely with files. Could any one suggest me. Thanks in advance (1 Reply)
Discussion started by: karthik82
1 Replies

9. UNIX for Dummies Questions & Answers

copy multiple files in different directories

I have a report file that is generated every day by a scheduled process. Each day the file is written to a directory named .../blah_blah/Y07/MM-DD-YY/reportmmddyy.tab I want to copy all of this reports to a separate directory without having to do it one by one. However, if I try cp... (3 Replies)
Discussion started by: ken2834
3 Replies

10. UNIX for Dummies Questions & Answers

How do copy certain files and directories from one hard drive to another?

I have two drives (scsi) mounted on one server and I need to copy certain files and directories from one drive to the other. Any suggestions :confused: (4 Replies)
Discussion started by: shorty
4 Replies
Login or Register to Ask a Question