LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-13-2008, 07:53 AM   #1
aapkae
LQ Newbie
 
Registered: Nov 2007
Posts: 2

Rep: Reputation: 0
help in batch renaming


i have a problem in my site.that all for my sites which have ' in there names are not getting download and i m getting a 404 error.i want to know that if there is any command which can rename all files in one folder and sub folders i want to replace "'" with "-"..so kindly help me in it
i know a command rename "'" "-"* but it only works for current directory i want it to work on current directory and all sub folders in it

please help me out
 
Old 02-13-2008, 09:51 AM   #2
premnarayan
LQ Newbie
 
Registered: Jan 2008
Location: noida
Distribution: archlinux, slackware
Posts: 21

Rep: Reputation: 15
Hi,
You could write a shell script using a recursive function to rename your files. Something like this one should do:
-----------------
#!/bin/sh
myrename()
{
rename "$1" "$2" *
for f in *;do
if [ -d "$f" ];then
cd "$f"
myrename "$1" "$2"
cd ..
fi
done
}
if [ -z "$1" ];then
echo "No renaming rules given"
exit
else
myrename "$1" "$2"
fi
------------------------------

Save this code into a file like batchRename.sh in home directory and then make it executable by "chmod u+x batchRename.sh".
To use it, go to the folder you want to recursively rename and then type
~/batchRename.sh "'" "-"

Hope it works the way you want.

PS: Rerun the command if there are multiple "'" in names.

Last edited by premnarayan; 02-13-2008 at 09:53 AM.
 
Old 02-13-2008, 11:11 AM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Here's a script I copied from somewhere on the internet 2 or 3 years ago. Unfortunately, I didn't keep a record of where I got it, so I don't know to whom to give credit. I've modified it slightly to fit your situation.
Code:
#! /bin/bash
# blank-rename.sh
#
# Substitutes underscores for blanks in all the filenames in a directory.
# The comment above reflects the original purpose of this script.
# I have changed the script to substitute dash (-) for apostrophe (').

ONE=1                     # For getting singular/plural right (see below).
number=0                  # Keeps track of how many files actually renamed.
FOUND=0                   # Successful return value.

for filename in *         #Traverse all files in directory. Edit to add path to directory
do                                # + containing the files indicated by  the wildcard *.
     echo "$filename" | grep -q "'"         #  Check whether filename
     if [ $? -eq $FOUND ]                   #+ contains apostrophe(s).
     then
       fname=$filename                      # Strip off path.
       n=`echo $fname | sed -e "s/'/-/g"`   # Substitute dash for apostrophe.
       mv "$fname" "$n"                     # Do the actual renaming.
       let "number += 1"
     fi
done   

if [ "$number" -eq "$ONE" ]                 # For correct grammar.
then
 echo "$number file renamed."
else 
 echo "$number files renamed."
fi 

exit 0
Notice that at the beginning of the loop, you should edit the script to add the path to the directory containing the files with the apostrophe (') in the name.
Example: for filename in /home/<username>/files/*

Last edited by bigrigdriver; 02-13-2008 at 11:13 AM.
 
Old 02-13-2008, 12:54 PM   #4
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,987

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
I think this simple command might do it:

$ find -name "*\'*" -exec rename \' - {} \;

The tricky part is that the ' character is reserved and used in the syntax of many bash commands and the bash shell itself. You have to use the "\" in front of ' as an escape character or you can run into all kinds of problems. Note, some of the above posted general purpose scripts may run into problems because you are searching for the reserved character ' and they are not using the escape character \. My simple command sequence should search the working directory and all subdirectories for any file with a ' in its file name and rename the file substituting - for '. Before you run the full command, be sure to test to make sure the find command is picking up the right files you want to rename and nothing else. Do that by running just the first part:

$ find -name "*\'*"

That will tell you which files find is picking up. In the full command, these results are sent one at a time to the rename command for renaming.
 
Old 02-13-2008, 02:10 PM   #5
aapkae
LQ Newbie
 
Registered: Nov 2007
Posts: 2

Original Poster
Rep: Reputation: 0
thanks for the suggestion and your help guys.but it did not fix the problem.can someone please be more specific
 
Old 02-13-2008, 06:49 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The above answers look pretty specific, what have you tried? what seems to be the problem? Show us what you did and what msgs you got.
 
  


Reply



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
batch renaming of files sycamorex Programming 9 08-13-2007 02:24 AM
Renaming Zmyrgel Linux - General 1 08-06-2006 07:59 AM
Batch renaming of dig. camera pics? fbfd1338 Linux - Newbie 2 04-09-2006 05:00 AM
Batch Renaming in bash xushi Programming 6 07-07-2005 03:24 PM
renaming batch of files linux_ub Linux - Newbie 6 10-27-2004 09:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:53 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration