Copy files in respective directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files in respective directories
# 1  
Old 03-21-2017
Copy files in respective directories

Hi Guys,

I need to copy the files to respective directories based on name of the file. My script is something like below

Code:
con=$1

for file in `cat $con` 
do
file_tmp=$(ls -t1 $path|  grep -i $file | head -n 1)
echo $file_tmp

if [ -f $path$file_tmp ]
then
	cp $path$file_tmp $DIR/ap

	if [ "$?" = "0" ]
	then
		echo "file copied"
				 
               
		
	else
		echo "Failed copy"
			
	fi
else 
	echo "$file_tmp not present "

fi
done

con.txt

Code:
apple
mango
fish

if its apple file then copy to $DIR/ap
if its mango file then copy to $DIR/man
if its fish file then copy to $DIR/fis

I need to copy the file in respctive paths by reading the file naming convention from config file
# 2  
Old 03-21-2017
How to determine the target directory? There doesn't seem to be a rule behind it. Can con.txt be extended to contain it? What be the "file naming convention"?
Your $path is undefined, as is your $DIR.

Last edited by RudiC; 03-21-2017 at 07:11 AM.. Reason: Removed erroneous assumption.
# 3  
Old 03-21-2017
Hi RudiC,

I also though the same way to determine to add in con.txt respective path. for eg. or a better way to achieve
Code:
apple,/dir/ap
mango,/dir/man
fish,/dir/fis

path=input_files/path/
dir=/dir/

# 4  
Old 03-21-2017
Hello Master_Mind,

Could you please try following and let me know if this helps you. Considering that your paths dir/app, dir/man all are already created.
Code:
DIR=/my/directory/path
awk -vdir="$DIR"  'function copy(path){system("cp " $0 "  " path)} {if($0 == "apple"){copy(dir"/ap")};if($0 == "fish"){copy(dir"/fis")};if($0 == "mango"){copy(dir"/man")}}'    Input_file

EDIT: Adding a non-one liner form of solution too now.
Code:
DIR=/my/directory/path
awk -vdir="$DIR"  'function copy(path){
                                        system("cp " $0 "  " path)
                                      }
                                      {
                                        if($0 == "apple"){
                                                                copy(dir"/ap")
                                                         };
                                        if($0 == "fish") {
                                                                copy(dir"/fis")
                                                         };
                                        if($0 == "mango"){
                                                                copy(dir"/man")
                                                         }
                                      }
                  '    Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 03-21-2017 at 07:30 AM.. Reason: Adding a non-one liner form of solution successfully too now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 03-21-2017
How about
Code:
path=/path/to
while IFS="," read FN PTH; do echo cp $path/$FN $PTH/$FN; done <con.txt
cp /path/to/apple /dir/ap/apple
cp /path/to/mango /dir/man/mango
cp /path/to/fish /dir/fis/fish

Remove echo when happy with proposals.

And, try to be consistent with trailing slashes when defining paths.
# 6  
Old 03-21-2017
Thanks Ravi,

But i would require to print messages like how i need if successfully copied or not and if the file doesnt present in input directory to print not present
# 7  
Old 03-21-2017
Quote:
Originally Posted by Master_Mind
Thanks Ravi,
But i would require to print messages like how i need if successfully copied or not and if the file doesnt present in input directory to print not present
Hello Master_Mind,

Could you please try following and let me know if this helps you.
Code:
cat copy_files.ksh
DIR=/tmp
awk -vdir="$DIR"  'function copy(path){
                                        system("cp " $0 "  " path";if [[ $? == 0 ]]; then echo " $0 " file copied successfully to " path ".; else echo Please check there is an issue while copying file.; fi")
                                      }
                                      {
                                        if($0 == "apple"){
                                                                copy(dir"/ap")
                                                         };
                                        if($0 == "fish") {
                                                                copy(dir"/fis")
                                                         };
                                        if($0 == "mango"){
                                                                copy(dir"/man")
                                                         }
                                      }
                  '    Input_file

EDIT: If you want to get that a file is NOT present error then following may help you in same too.
Code:
cat copy_files.ksh
DIR=/tmp
awk -vdir="$DIR"  'function copy(path){
                                        system("if [[ -f " $0 " ]]; then cp " $0 "  " path";if [[ $? == 0 ]]; then echo " $0 " file copied successfully to " path ".; else echo Please check there is an issue while copying file.; fi; else echo " $0 " file not present.; fi")
                                      }
                                      {
                                        if($0 == "apple"){
                                                                copy(dir"/ap")
                                                         };
                                        if($0 == "fish") {
                                                                copy(dir"/fis")
                                                         };
                                        if($0 == "mango"){
                                                                copy(dir"/man")
                                                         }
                                      }
                  '    Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 03-21-2017 at 07:53 AM..
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