Replacing _ with - from "SSH"

Discussion in 'Apache' started by phantomddl, Nov 18, 2007.

  1. #1
    i got some folder names like name_surname in a folder and i wanna make them all name-surname, as there are like thousands of them i am not able to rename one by one, is there a command i can do this simplier?
    thanks
     
    phantomddl, Nov 18, 2007 IP
  2. Alfarin

    Alfarin Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There is no command to do that automatically. I suck with shell script, so I do things like that using php instead.
    <?php
    $dir = "/home/user/domain.ext/folder/";
    
    if (is_dir($dir)) {
    	if ($dh = opendir($dir)) {
    		while (($file = readdir($dh)) !== false) {
    			$files[] = $file;
    		}
    		sort($files);
    		foreach($files AS $file) {
    			$starts_with	= substr($file, 0, 1);
    			if (($starts_with != ".")) {
    				// . and .. are directives, other . files probably shouldn't be touched anyways
    				$newname = str_replace("_", "-", $file);
    				rename($dir . $file, $dir . $newname);
    			}
    		}
    		closedir($dh);
    	}
    }
    ?>
    PHP:
    Something like that will probably do the trick for you; though, if the files you're looking to change are folders, then I don't know for sure.
     
    Alfarin, Nov 18, 2007 IP
  3. hostingonweb

    hostingonweb Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have shell script:

    create rename.sh

    #!/bin/sh

    for name in $(find . -type d -print | sed 's/_/____/g')
    do
    if [ $name != "." ] ; then
    oldname="$(echo $name | sed 's/____/_/g')"
    newname="$(echo $name | sed 's/____/-/g')"
    if [ "$oldname" != "$newname" ] ; then
    echo "renaming \"$oldname\" to $newname"
    mv "$oldname" "$newname"
    fi
    fi
    done


    save it and chmod to 755.

    Ask your host to execute the shell script in the directory with ./rename.sh
     
    hostingonweb, Nov 18, 2007 IP
  4. phantomddl

    phantomddl Well-Known Member

    Messages:
    2,856
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    160
    Articles:
    15
    #4
    thanks for the responds :)
     
    phantomddl, Nov 19, 2007 IP