LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-02-2006, 09:01 PM   #1
Furlinastis
Member
 
Registered: Dec 2004
Location: Ball of Confusion
Distribution: Slackware,Bluewhite64
Posts: 252

Rep: Reputation: 30
file copying issues with bash


I'm a newb when it comes to scripting to try not to laugh too hard.

I wrote this script awhile ago to move files from A to B but it doesn't seem to make the directory correctly. I have to manually create a folder or else it will copy the file into a file named as the directory I want it named. For example, I have a file in $folr1 which is named "hooga-booga episode 5.avi" and var1 = booga. It will move the file to $folr2 and name it just booga. The file will be ok, but if I have another file in $folr1 named "dooga-booga episode 2.mpg" it will overwrite the file booga in $folr2. But I want it to create a folder in $folr2 named "booga" and copy the files into that folder.

I hope that made sense. I've had this script in cron.weekly for half a year or so and it's fine so long as I create the folder first, but that kind of kills the point of writing a script in the first place. Can anyone give me any pointers on how to fix this issue?

Code:
#!/bin/bash
log=/home/hedron/logfiles/copylog
folr1=/home/hedron/incoming/files
folr2=/home/hedron/Docs/drive01

for blah in var1 var2 var3 var4 etc; do

#ensures that there are folders for the files to be copied to

find $folr2 -type d -iname "*" | grep -i $blah
if [ $? = 1 ]; then
	mkdir $folr2/$blah
	if [ $? = 1 ]; then
		mv $folr2/$blah $folr2/$blah'.renamed'
		mkdir $folr2/$blah
	fi
fi

#copies the files into the specified folders

find $folr1 -type f -iname "*$blah*" | grep -i $blah

if [ $? = 0 ]; then

	find $folr1 -type f -iname "*$blah*" -exec mv {} $folr2/$blah \;
	tim=$(date)
	echo $tim': The files containing '$blah' have hopefully been copied' >> $log
else
	tim=$(date)
	echo $tim': Apparently there are no '$blah' files to be copied' >> $log
fi	

done

Last edited by Furlinastis; 05-02-2006 at 09:03 PM.
 
Old 05-02-2006, 10:47 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
Your if statements use wrong syntax, use:

if [[ $? -eq 0 ]]

also, I'd use

ls $folr1/$blah

instead of the find cmd; more specific
 
Old 05-02-2006, 11:34 PM   #3
Furlinastis
Member
 
Registered: Dec 2004
Location: Ball of Confusion
Distribution: Slackware,Bluewhite64
Posts: 252

Original Poster
Rep: Reputation: 30
OK, thanks. Syntax always gets me with this stuff. What is the difference between single brackets and double brackets?
 
Old 05-03-2006, 01:32 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
From http://www.tldp.org/LDP/abs/html/testconstructs.html :
Tip

Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Shell script : copying a file to multiple folder zamri Programming 14 04-29-2008 10:27 AM
Licensing issues and copying and redistributions sureshkellemane Red Hat 2 02-24-2005 09:19 PM
Copying linked files, replacing directories in bash scripts? ta0kira Programming 2 10-10-2004 04:46 AM
Bash script to do copying and ISO image creating, etc. satimis Programming 2 09-19-2004 09:47 PM
copying a lot of files with bash or terminal. rob-n Linux - Newbie 3 04-13-2004 06:01 PM


All times are GMT -5. The time now is 09:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration