LinuxQuestions.org
Help answer threads with 0 replies.
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 12-29-2007, 10:06 AM   #1
nadavvin
Member
 
Registered: May 2006
Distribution: ubuntu
Posts: 109

Rep: Reputation: 21
problem in script that change files


Hello

My friend ask me about script of changing large files with sub directories with index of them like:

01 file1
02 file2

I try to do one but the "find" command when their files with spaces return to the for loop each part.

for example file like "hello world.txt" will split to "hello" and "world.txt"

How do I use in for in bash loop correctly?

If there is script like that you can tell me about it

Code:
#!/bin/bash
THIS=`basename "$0"`
NUMBER=0
for file in $( find -type f ); do
	echo start loop
	echo $file
	FILENAME=`basename $file`
	if [ "$FILENAME" = "" ]; then
		echo problem
		exit
	fi
	if [ "$FILENAME" = "$THIS" ]; then
		continue
	fi
	DEST="`dirname $file`/$NUMBER $FILENAME"
	echo "$DEST"
	mv "$file" "$DEST"
	let NUMBER+=1
done
 
Old 12-29-2007, 11:12 AM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by nadavvin View Post
Code:
	# Maybe change this
	FILENAME=`basename $file`

	# ... to this
	FILENAME="`basename $file`"
I think your version only assigns the everything up to the first space to FILENAME. I just tested this theory on the command line in bash.
 
Old 12-29-2007, 11:14 AM   #3
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 37
You could try something like this:

Code:
find -type f|while IFS= read -r;do
	mv -v "$REPLY" "${REPLY%/*}/$(printf "%0.2d" $i) ${REPLY##*/}"
	i=$((++i))
done
If you put it in a script and you want to exclude
the script name from the result, you should modify
the command like this:


[assuming GNU find]

Code:
find ! -wholename "$0" -type f ...

Last edited by radoulov; 12-29-2007 at 11:17 AM.
 
Old 12-29-2007, 11:35 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
Code:
for file in $( find -type f ); do
    echo start loop
    echo "$file";
    FILENAME=`basename "$file"`
..
Use double quotes to preserve spaces. Including in `basename "$file"` and when referencing $FILENAME.
However, why are you messing with all of these basename and dirname functions.

Code:
mkdir test
jschiwal@hpamd64:~> touch test/"01 file"
jschiwal@hpamd64:~> touch test/"02 file"
jschiwal@hpamd64:~> touch test/"03 file"
jschiwal@hpamd64:~> mkdir destdir
jschiwal@hpamd64:~> for file in test/*; do
> mv "$file" destdir/ -v
> done
`test/01 file' -> `destdir/01 file'
`test/02 file' -> `destdir/02 file'
`test/03 file' -> `destdir/03 file'
Code:
find test/ -type f -print0 | xargs -0 mv -v --target-directory=destdir/
 `test/02 file' -> `destdir/02 file'
 `test/03 file' -> `destdir/03 file'
 `test/01 file' -> `destdir/01 file'

Last edited by jschiwal; 12-29-2007 at 10:46 PM.
 
Old 12-30-2007, 08:46 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by nadavvin View Post
Hello

My friend ask me about script of changing large files with sub directories with index of them like:

01 file1
02 file2

I try to do one but the "find" command when their files with spaces return to the for loop each part.

for example file like "hello world.txt" will split to "hello" and "world.txt"

How do I use in for in bash loop correctly?

If there is script like that you can tell me about it

Code:
#!/bin/bash
THIS=`basename "$0"`
NUMBER=0
for file in $( find -type f ); do
	echo start loop
	echo $file
	FILENAME=`basename $file`
	if [ "$FILENAME" = "" ]; then
		echo problem
		exit
	fi
	if [ "$FILENAME" = "$THIS" ]; then
		continue
	fi
	DEST="`dirname $file`/$NUMBER $FILENAME"
	echo "$DEST"
	mv "$file" "$DEST"
	let NUMBER+=1
done
use while loop instead
Code:
...
find -type f | while read file
do
 ...#your code##
done
...
 
Old 12-30-2007, 11:00 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
An alternative to calling basename is to use the ${variable##pattern} expansion. This removes the longest string matching pattern from the value of variable, thus these two commands will output the same thing:
Code:
echo base name is `basename "$0"`
echo base name is ${0##*/}
Calling basename is less desirable because it invokes an external program, which means it is slower and less reliable. It's not a big deal, but it's a nice touch to use the shell expansions like this.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] command line script to change resolution of png files kaz2100 Programming 3 06-04-2007 07:53 PM
How to write a script to change web log files? tomwgf Linux - Desktop 6 02-07-2007 09:38 AM
script to change unix path to windows path in all files csross Programming 8 04-29-2006 01:05 PM
Script to change names of files in a directory geomonap Linux - General 2 12-03-2004 03:04 PM
problem creating BASH script with files that have whitespace scattaneo Linux - General 2 11-06-2004 05:40 PM

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

All times are GMT -5. The time now is 06:00 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration