LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need to rename files and folders with invalid characters in the names (https://www.linuxquestions.org/questions/linux-newbie-8/need-to-rename-files-and-folders-with-invalid-characters-in-the-names-716128/)

laureynsr 04-01-2009 01:06 PM

Need to rename files and folders with invalid characters in the names
 
Hi All,

I have 1.6GB of files in a folder on a Linux server that I need to copy to a Windows 2003 server. The problem is that all the files and sub folder names have 2 colons (::) in them. Is there a script or command way to rename all these files and folders to replace the colons with a space or dash so I can copy them without the dreaded "Invalid filename" errors?

Here is a capture of some of the files/folders:
-rwxr-xr-x 1 root root 20480 Apr 1 09:18 1141761865::Inforequest.doc
drwxr-xr-x 2 root root 48 Apr 1 09:18 1143511838::temp
drwxr-xr-x 2 root root 96 Apr 1 09:18 1143728379::Proposal
-rwxr-xr-x 1 root root 1071616 Apr 1 09:18 1144183601::Vantage1.doc

Any help would be greatly appreciated.

Thanks

Ray Laureyns

emetib 04-01-2009 01:10 PM

man rename

rename should work for you. just changing what the example is to fit your situation should work.

laureynsr 04-01-2009 02:25 PM

Ok, maybe I'm missing something here.
 
Quote:

Originally Posted by emetib (Post 3495203)
man rename

rename should work for you. just changing what the example is to fit your situation should work.

Hi emetib,

Well, maybe I wasn not clear. I have thousads of files and folders that I need to rename in this folder. So simply usning rename I have not found a syntax that can do all the files and folder recursivly. I have seen some examples of using tr to truncate the files but I don't want to corrupt them.

Any other ideas?

Thanks

Ray Laureyns

Tinkster 04-01-2009 02:33 PM

Maybe something like this old kludge of mine? Modify to suite your needs ...
Code:

#!/bin/bash
# for this script we don't want " " as a separator
# so we don't confuse mv with "multiple arguments"
IFS=$'\n'

name=$1

# check for, and replace upper case letters if applicable
echo $name | grep -e [[:upper:]+]  > /dev/null
if [ "$?" = "0" ]
then
    newname=`echo $name |tr '[:upper:]' '[:lower:]'`
fi

# check for and delete leading spaces
echo $newname | egrep "^ +" > /dev/null
if [ "$?" = "0" ]
then
    newname=`echo $newname  |  sed "s/ *//"`
fi

# check for and delete leading spaces as presented by find
echo $newname | egrep "\./ +" > /dev/null
if [ "$?" = "0" ]
then
    newname=`echo $newname | egrep "\.\/ +" | sed "s/  *//" `
fi


# check for and delete trailing spaces
echo $newname | egrep " $" > /dev/null
if [ "$?" = "0" ]
then
    newname=`echo $newname  |  sed "s/  *$//"`
fi

# check for and delete other non-welcome characters
echo $newname | grep [:\(\)\&\'\!]  > /dev/null
if [ "$?" = "0" ]
then
    newname=`echo $newname | sed "s/[\(\)\&\'\!]/_/g" `
fi

# check for and replace all other spaces with "_"
echo $newname | egrep " " > /dev/null
if [ "$?" = "0" ]
then
    newname=`echo $newname  |  sed "s/ /_/g"`
fi

# should we have generated a caravan of underscores
# this should take care of it
echo $newname | egrep "_+" > /dev/null
if [ "$?" = "0" ]
then
    newname=`echo $newname  |  sed "s/__*/_/g"`
fi


# "_-_" ... another artefact to be taken care of
echo $newname | grep "_-_" > /dev/null
if [ "$?" = "0" ]
then
    newname=`echo $newname  |  sed "s/_-_/_/g"`
fi

# "," - I decided that I don't like those, either...
echo $newname | grep "," > /dev/null
if [ "$?" = "0" ]
then
    newname=`echo $newname  |  sed "s/,//g"`
fi



# if there were modifications, commit them
if [ "$name" != "$newname" ]
then
    echo "moving " $name $newname
    mv "$name" "$newname"
fi

Save as some scriptname, and then use with find; iterate over the directories
first, with increasing depth; then repeat with files.

find -type d -maxdepth 1 -exec savedscript "{}" \;
find -type d -mindepth 1 -maxdepth 2 -exec savedscript "{}" \;
find -type d -mindepth 2 -maxdepth 3 -exec savedscript "{}" \;
...

find -type f -exec savedscript "{}" \;




Cheers,
Tink

emetib 04-01-2009 02:37 PM

how are you ls those files? what are the options that you are using?

Tinkster 04-01-2009 03:13 PM

huh?


All times are GMT -5. The time now is 10:41 PM.