bash script to move files
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 4 of 4
  1. #1
    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...

  2. #2
    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.
    Last edited by Johnny Fist; 11th December 2005 at 05:11 AM.
    ...the horror...

  3. #3
    Join Date
    Aug 2005
    Location
    Springfield, Oh
    Age
    36
    Posts
    67

    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

  4. #4
    Join Date
    Jul 2009
    Location
    England, UK
    Posts
    969

    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"

Similar Threads

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •