Jump to content
  • Sign Up
Sign in to follow this  
pants_happy

help with linux shell script

Recommended Posts

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:

Share this post


Link to post
Share on other sites

Not that I can help, but hello Mr Pantsworth.


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

Share this post


Link to post
Share on other sites
Guest Fishboot

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...