Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-13-2008, 07:53 AM   #1
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 "-" 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
LQ Newbie
Registered: Jan 2008
Location: noida
Distribution: archlinux, slackware
Posts: 21

Rep: Reputation: 15
You could write a shell script using a recursive function to rename your files. Something like this one should do:
rename "$1" "$2" *
for f in *;do
if [ -d "$f" ];then
cd "$f"
myrename "$1" "$2"
cd ..
if [ -z "$1" ];then
echo "No renaming rules given"
myrename "$1" "$2"

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

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
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,889

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
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.
#! /bin/bash
# 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).
       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"

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

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
Senior Member
Registered: Oct 2003
Posts: 2,540

Rep: Reputation: 212Reputation: 212Reputation: 212
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
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
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,371

Rep: Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381Reputation: 2381
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.


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
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

All times are GMT -5. The time now is 04:49 AM.

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