Jump to content

Recommended Posts

Posted

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. :thumbsup:

Posted

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

Guest Fishboot
Posted

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.

Posted

#!/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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...