pants_happy Posted September 18, 2005 Posted September 18, 2005 how could i write a shell script that will recursively descend through a directory structure, checking all entry names for spaces, and when a name is encountered that contains one or more spaces, replace each space with an underscore character? if anyone can help me, i will post a smilie in your honour.
Musopticon? Posted September 19, 2005 Posted September 19, 2005 Not that I can help, but hello Mr Pantsworth. kirottu said: I was raised by polar bears. I had to fight against blood thirsty wolves and rabid penguins to get my food. Those who were too weak to survive were sent to Sweden. It has made me the man I am today. A man who craves furry hentai. So let us go and embrace the rustling smells of unseen worlds
pants_happy Posted September 19, 2005 Author Posted September 19, 2005 not to spam, but to reply and keep this topic near the top... hello.
Guest Fishboot Posted September 19, 2005 Posted September 19, 2005 Perhaps this is good evidence that no one in this video game developer fan forum is a competent nix shell script writer? I would have known a couple of years ago, or at least I would have known where to look to get an answer, but I stopped working with them and I've forgotten it all. I would think you would need to know a bit about the ls command (how to ls the nth file in a directory, basically) and how to pick out spaces from a string (I might do that in a slightly more sophisticated programming language like fortran or C rather than in-shell). So you feed the nth filename into a small program that puts the string into an array and then have a while loop that switches all of the empty cells into underscores, then send the new string back to the shell script to use with a rename command on the original file (using the mv program, I think?). There's probably a way to do it all in-shell but I don't know it.
Serious Callers Only Posted September 19, 2005 Posted September 19, 2005 #!/bin/bash for i in "`ls -d */`"; do newName="`echo -n $i | tr -s [:space:] "_"`" mv "$i" "$newName" done Am stumped in recursion Research.
Serious Callers Only Posted September 20, 2005 Posted September 20, 2005 lame error correction: #!/bin/bash for i in */; do { newName=`echo -n "$i" | tr -s [:space:] "_"` mv "$i" "$newName" } done
Lord Halewyn Posted September 21, 2005 Posted September 21, 2005 #!/bin/bash function RenameFiles { for i in * do if [[ -d "$i" ]] then cd "$i" RenameFiles cd .. elif [[ -f "$i" ]] then newName="`echo -n "$i" | tr -s [:space:] "_"`" mv "$i" "$newName" fi done } RenameFiles It still needs some work, but this will rename the files recursively.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now