LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-01-2009, 01:06 PM   #1
laureynsr
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 0
Exclamation 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
 
Old 04-01-2009, 01:10 PM   #2
emetib
Member
 
Registered: Feb 2003
Posts: 482

Rep: Reputation: 33
man rename

rename should work for you. just changing what the example is to fit your situation should work.
 
Old 04-01-2009, 02:25 PM   #3
laureynsr
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Ok, maybe I'm missing something here.

Quote:
Originally Posted by emetib View Post
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
 
Old 04-01-2009, 02:33 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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
 
Old 04-01-2009, 02:37 PM   #5
emetib
Member
 
Registered: Feb 2003
Posts: 482

Rep: Reputation: 33
how are you ls those files? what are the options that you are using?
 
Old 04-01-2009, 03:13 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
huh?
 
  


Reply

Tags
charachters, invalid, rename


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename files with the first three characters towards the end of the name pwc101 Programming 12 06-15-2008 06:25 PM
Rename a file with hexa decimal characters sandeshsk007 Linux - Software 13 12-13-2007 02:11 AM
How to modify the names of files and replace characters with other characters or symb peter88 Linux - General 2 12-10-2006 03:05 AM
Rename multiple files in folders??? adds2one Linux - Software 19 10-05-2006 12:22 AM
rename file names with " ? characters fishbonz Linux - Newbie 2 05-31-2005 11:20 AM


All times are GMT -5. The time now is 12:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration