Fedora Linux Support Community & Resources Center
  #1  
Old 11th December 2005, 02:28 AM
Johnny Fist Offline
Registered User
 
Join Date: May 2004
Posts: 72
bash script to move files

I'm fooling around over here creating a bash script to transfer the contents of one directory to another. It works fine as long as the directory or file doesn't contain a space. The second I have a space involved the script doesn't work.

Code:
#!/bin/bash

OLDIR=$1
NEWDIR=$2

for FILE in `ls -A $OLDIR`
do
mv $OLDIR/"$FILE" $NEWDIR/
echo "$FILE moved from $OLDIR to $NEWDIR"
done
I use two arguments to define which directory I am copying from and which directory to copy to. How can I get this to work on files and directories that have spaces?

Any and all help is greatly appreciated.
__________________
...the horror...
Reply With Quote
  #2  
Old 11th December 2005, 04:09 AM
Johnny Fist Offline
Registered User
 
Join Date: May 2004
Posts: 72
Well, I kind of have it working by simplifying things a bit.


Code:
#!/bin/bash

mv $1/* $2
I'm still interested in other solutions though.
__________________
...the horror...

Last edited by Johnny Fist; 11th December 2005 at 04:11 AM.
Reply With Quote
  #3  
Old 8th May 2011, 05:27 PM
s3cur17y Offline
Registered User
 
Join Date: Aug 2005
Location: Springfield, Oh
Age: 32
Posts: 66
linuxchrome
Re: bash script to move files

I know this is a very old post, but I had to google this same issue. But I thought I would post a resolution here.

Code:
#!/bin/bash

# You need to add this line below.
IFS=$'\n' 

OLDIR=$1
NEWDIR=$2

for FILE in `ls -A $OLDIR`
do
mv $OLDIR/"$FILE" $NEWDIR/
echo "$FILE moved from $OLDIR to $NEWDIR"
done
__________________
HP DV6000 Dual boo Windows 7 Ultimate, and F13
EeePc 900a 16GB SSD, 1GB Ram, F13 Quasar8000 Custom EeePc Kernel
Reply With Quote
  #4  
Old 9th May 2011, 09:51 AM
marriedto51 Offline
Registered User
 
Join Date: Jul 2009
Location: England, UK
Posts: 910
linuxfedorafirefox
Re: bash script to move files

At the risk of being accused of necrophilia...

Why not just use the following?
Code:
#!/bin/bash
mv "$1"/* "$2"
Reply With Quote
Reply

Tags
bash, files, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to iterate over files in a bash script? Dunhausen Programming & Packaging 12 11th July 2011 07:48 AM
Convert bash script to perl script homey Programming & Packaging 1 2nd September 2006 04:24 AM
easy script to move to RAWHIDE - how-to cybrjackle Programming & Packaging 2 18th September 2005 01:57 PM


Current GMT-time: 15:52 (Friday, 19-09-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat