ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
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
...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.