LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 02-03-2015, 05:27 PM   #1
fritz1968
LQ Newbie
 
Registered: Feb 2015
Posts: 1

Rep: Reputation: Disabled
Question Moving files with a space


I have a directory structure that at least 10 directories (same level). Each directory contains a mix of media files: *.jpg, *.JPG, *.AVI, *.MOV, etc...

I want to move certain files to a new path, preserving the directory structure. I have the following, but the problem is that I have several files with a space in them. THose files are not moved. Here is my bash code so far:


#/bin/bash
#######################################

source1=$1
destDir=$2
fileExt=$3

echo $source1
echo $destDir
echo $fileExt

cd $source1 # This is where the file were downloaded to

for file in $(find -name "$fileExt" ) #-type f -not -path "./.svn/*")
do
fPath="$destDir/$file" # This is the full path of the file to copy to
dPath=$(dirname ${fPath}) # This finds the directory of the path

if [[ ! -d "$dPath" ]] # double checking that the directory exists
then # It does not, so create it
mkdir -p $dPath
fi
echo "=================================="
echo Path : "$fPath"
echo File : "$file"
mv "$file" $dPath
done

The error for a file with a space in it (IMG_5210 - Version 2.jpg) is :
Path : ../testDen/./HolidayCardPics/IMG_5210
File : ./HolidayCardPics/IMG_5210
mv: cannot stat ‘./HolidayCardPics/IMG_5210’: No such file or directory
==================================
Path : ../testDen/-
File : -
mv: cannot stat ‘-’: No such file or directory
==================================
Path : ../testDen/Version
File : Version
mv: cannot stat ‘Version’: No such file or directory
==================================
Path : ../testDen/2.jpg
File : 2.jpg
mv: cannot stat ‘2.jpg’: No such file or directory


How do I correct my code so that it does not look at the spaces as different files?

Thanks in advance for your help.
 
Old 02-03-2015, 05:53 PM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Use [code] around your code snippets for better readability.

Quote:
Originally Posted by fritz1968 View Post
for file in $(find -name "$fileExt" )
Unfortunately, no, this won’t work. You can try doing this:
Code:
find -name "$fileExt" | while read file; do
but even that will fail if any of the file contains a new line character. If I ever need to use find in such a way, I end up implementing it’s functionality in shell myself, i.e. something along the lines of:
Code:
handle_dir() {
    for entry in "$1/"*; do
        if [ -d "$entry" ]; then
            handle_dir "$entry"
        elif [ -f "$entry" ]; then
            handle_file "$entry"
        else
            echo "$entry: not a regular file or directory, skipping" >&2
        fi
    done
}

hanadle_file() {
    : … do what you need …
}
PS. Quote variables consistently and use ‘--’ to separate arguments from file names. For example:
Code:
mv -- "$file" "$dPath"
# NOT: mv "$file" "$dPath"
 
Old 02-03-2015, 06:15 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Use recursive globbing.

Code:
#/bin/bash
#######################################

shopt -s globstar

source1=$1
destDir=$2
fileExt=$3

echo $source1
echo $destDir
echo $fileExt

cd "$source1" # This is where the file were downloaded to

for file in **/$fileExt
do
	fPath="$destDir/$file" # This is the full path of the file to copy to
	dPath="$( dirname "${fPath}" )" # This finds the directory of the path

	if [[ ! -d "$dPath" ]] # double checking that the directory exists
	then # It does not, so create it
		mkdir -p "$dPath"
	fi
	echo "=================================="
	echo Path : "$fPath"
	echo File : "$file"
	mv -- "$file" "$dPath"
done
Also note the extra quoting (which was necessary).

Last edited by dugan; 02-03-2015 at 06:54 PM.
 
Old 02-03-2015, 11:46 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Another quick alternative for getting a directory path:
Code:
dPath="${fPath%/*}"
 
1 members found this post helpful.
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving free space to /home partition lonecrow Linux - Newbie 17 09-09-2007 05:48 PM
Moving space between partitions GoBillsBN Mandriva 1 07-12-2005 11:41 AM
moving system betw. harddisks - cp says not enough space furryspider Linux - Software 2 11-21-2004 07:13 AM
moving space in harddrive from windows to linux SuSE_User Linux - Software 2 02-21-2004 07:39 PM
Moving Disk Space in Linux ALF Linux - Software 6 11-27-2003 06:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration