problem in script that change files - LinuxQuestions.org
LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
Thread Tools
Old 12-29-2007, 11:06 AM   #1
nadavvin
Member
 
Registered: May 2006
Distribution: Fedora
Posts: 96
problem in script that change files


[Log in to get rid of this advertisement]
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
nadavvin is offline     Reply With Quote
Old 12-29-2007, 12:12 PM   #2
David1357
Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 584
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.
David1357 is offline     Reply With Quote
Old 12-29-2007, 12:14 PM   #3
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 185
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 12:17 PM..
radoulov is offline     Reply With Quote
Old 12-29-2007, 12:35 PM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 10,803
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 11:46 PM..
jschiwal is online now     Reply With Quote
Old 12-30-2007, 09:46 AM   #5
ghostdog74
Member
 
Registered: Aug 2006
Posts: 976
Blog Entries: 5
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
...
ghostdog74 is offline     Reply With Quote
Old 12-30-2007, 12:00 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,471
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.
matthewg42 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
command line script to change resolution of png files kaz2100 Programming 3 06-04-2007 08:53 PM
How to write a script to change web log files? tomwgf Linux - Desktop 6 02-07-2007 10:38 AM
script to change unix path to windows path in all files csross Programming 8 04-29-2006 02:05 PM
Script to change names of files in a directory geomonap Linux - General 2 12-03-2004 04:04 PM
problem creating BASH script with files that have whitespace scattaneo Linux - General 2 11-06-2004 06:40 PM


All times are GMT -5. The time now is 03:17 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
RSS2  LQ Podcast
RSS2  LQ Radio
Open Source Consulting | Domain Registration