I wrote this to fix windows directories (that come inside zip files people send me)
that have spaces in directory names. Renaming files is easier.
With directories you have to go to the bottom of the tree, and then
rename going upwards, on the way out. I couldn't find a script that worked
(I found this old request instead, and several others) so I wrote this
yesterday. It seems to work just fine.
Last edited by bakunin; 11-03-2008 at 07:26 AM..
Reason: Adding "code"-tags around code is a favourite pastime of mine
You don't need to use recursion, this will enumerate through directories and call a script.
Kudos, porter!! Thanks for sharing this sample code. It's very well written!
For those of you learning shell scripting, there's some good lessons in this short piece of code, so read on and I'll point out what I really like about porter's sample code:
1) Having the find command do the work, saving the trouble of writing and debugging a recursive function. Always use the tools already included in the shell and/or the UNIX/Linux command set before re-inventing the wheel!
2) Use of the read command non-interactively with the find command; very useful!
3) Use of parenthese to block all the code inside the do...done loop; this sure eliminates mucking about with semi-colons at the end of each line (and having some lines "break" because of unnecessary semi-colons).
4) Error handling: Good example of testing the exit code / return status of a command. e.g. if $test "$?" = "0" # if the exit code ($?) is True (0 zero), then the command succeeded.
------------
NOTES: The use of the double quotes around the variable $N makes the test code above necessary, should the directory (file) name contain one or more blank spaces. With the variable N in single quotes, the cd command would work regardless of blank spaces in the file name. Read about Quoting to find out more.
If a directory name contained non-printable charactes (symbols, device codes, etc.), and the read command never passed them into the variable N, then the cd command would fail.
Directory permissions might cause cd to fail; another reason to handle expected and unexpected errors!
The code below (as written anyway) won't always work,
I don't believe. Not if the purpose of run-some-script.sh
is to rename directories on the fly. Find (it must, I think)
make a list of names to iterate over. So, if you change a directory
name, then all the subdirectories below that one suddenly have
invalid names, and the script will blow up. There might be a way
to use find -depth......but at least as written, I don't think the code
below would work for directory renaming.....
find . -type d | while read N
do
(
cd "$N"
if test "$?" = "0"
then
run-some-script.sh
fi
)
done
The code below (as written anyway) won't always work,
I don't believe. Not if the purpose of run-some-script.sh
is to rename directories on the fly. Find (it must, I think)
make a list of names to iterate over. So, if you change a directory
name, then all the subdirectories below that one suddenly have
invalid names, and the script will blow up. There might be a way
to use find -depth......but at least as written, I don't think the code
below would work for directory renaming.....
find . -type d | while read N
do
(
cd "$N"
if test "$?" = "0"
then
run-some-script.sh
fi
)
done
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
Try running 'phone4 xyz' and see what happens.
Modify your program so that if no matching name is found, an... (1 Reply)
NB! I have already started a thread on this subject on ComputerHope (with the thread title "Recursive File Renaming & Logical Sorting"). However, on ComputerHope they are perhaps more specialized in Windows Command Prompt, and not that much in shell scripts for Bash (I guess).
I have a bulk... (1 Reply)
same script:
1- i am using grep to find a string called: tinker panic 0 in a file /etc/ntp.conf
if the string is not there, i want to add the strings in /etc/ntp.conf file in the first line of the file. if not do nothing or exit.
2- also i want to add # in front of the following lines in... (0 Replies)
Hi Guys i have a <script?> that spits out the location of each printer using snpget
here is the code
for i in `sed -n '/Start Printer/,/End Printer/p' /hosts/blah/etc/dhcp/hosts.conf | awk '!/^#/ {print $2}' | egrep -v \... (2 Replies)
have this code but when i run it i get this error
./pulse: line 2: and here is the code
#!/bin/bash
if ;
then
pulseaudio -k;
fi
what am i doing wrong
thanks
Adam (5 Replies)
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
i have to do this but i am confused,
Create a file containing the bash functions which perform the... (1 Reply)
hi all
i'm trying to get a script working upon connection with pppd
According to docu this happens ina clean environment with a couple of variables set, namely $1,$2,...
To be able to execute the statements i included a path statement but i think i'm running into trouble with the variables -... (6 Replies)
Hello,
I'm confused (oh, yes). I'm running Linux at work. When I type 'echo $SHELL' I am told that I'm running tcsh. In /bin I note that both tcsh and bash are listed.
Question 1: Can I swap to run bash rather than tcsh and, if so, how will this affect my system? Is there any advantage to... (6 Replies)
Hello there peeps:
There is a little piece of bash shell scripting problem i have, which i was hoping you could help me with.
#!/bin/bash
stored_word()
{
case $(( $$ % 8 )) in
0 ) echo "energy";;
1 ) echo "touch";;
2 ) echo "climbing";;
3 ) echo... (3 Replies)