LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 06-29-2009, 11:53 AM   #1
bowens44
LQ Newbie
 
Registered: Aug 2005
Posts: 25

Rep: Reputation: 15
renaming files with spaces and special characters.


I have several files that are dumped into a directory on a linux box on a nightly basis. I need to rename all of files the in the directory with a script that runs each morning.

Here is an example of the file names:
MYReport Report crtjan0921-31 (3797 found).csv

I would like files in this format to be renamed to:
crtjan0921-31.csv

any help will be greatly appreciated.
 
Old 06-29-2009, 12:19 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
The general answer is that you have to quote a filename that contains spaces (or any character that has special meaning to bash)

Here's a sample of what works and what does not:
Code:
[mherring@hplap play]$ touch "file name"
[mherring@hplap play]$ ls
file name
[mherring@hplap play]$ mv file name newname
mv: target `newname' is not a directory
[mherring@hplap play]$ mv "file name" newname
[mherring@hplap play]$ ls
newname
[mherring@hplap play]$ touch "file name"
[mherring@hplap play]$ mv file* newname2
[mherring@hplap play]$ ls
newname  newname2
[mherring@hplap play]$
Note that expansion (*) works even for a name with space in it.
 
Old 06-29-2009, 12:41 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
And here is something that might work:
Code:
for file in MYReport*
do
    target=$(echo $file | awk '{print $3}')
    if [ ! -f $target.csv ]
    then
        mv "$file" $target.csv
    fi
done
 
Old 06-29-2009, 12:51 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by bowens44 View Post
I have several files that are dumped into a directory on a linux box on a nightly basis. I need to rename all of files the in the directory with a script that runs each morning.

Here is an example of the file names:
MYReport Report crtjan0921-31 (3797 found).csv

I would like files in this format to be renamed to:
crtjan0921-31.csv

any help will be greatly appreciated.
If you have Python, you can try my script called File renamer (see my sig last link) eg usage:
Code:
# ls -1
MYReport Report crtjan0921-31 (3797 found).csv
# python filerenamer.py -p "^.* .* (crt.*) \(.*$" -e "\1.csv" -l "*.csv"
==>>>>  [ /home/MYReport Report crtjan0921-31 (3797 found).csv ]==>[ /home/crtjan0921-31.csv ]
it assumes your files have the same format every time.

alternatively,
Code:
ls -1 *csv | awk 'BEGIN{FS="[. ]";q="\047"}
{
 newname = $3"."$NF
 cmd="mv "q $0 q" "q newname q
 system(cmd)
}'
 
Old 06-29-2009, 01:54 PM   #5
bowens44
LQ Newbie
 
Registered: Aug 2005
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
If you have Python, you can try my script called File renamer (see my sig last link) eg usage:
[code]
# ls -1
MYReport Report crtjan0921-31 (3797 found).csv
# python filerenamer.py -p "^.* .* (crt.*) \(.*$" -e "\1.csv" -l "*.csv"
==>>>> [ /home/MYReport Report crtjan0921-31 (3797 found).csv ]==>[ /home/crtjan0921-31.csv ]
I tried using your script but apparently I'm doing something wrong.

I enter this;
# python filerenamer.py -p "^.* .* (crt.*) \(.*$" -e "\1.csv" -l "*.csv"

I get the following output but file names are not changed. Is there something that I have to specify in order to actually update the files name?

==>>>> [ /var/www/kb_file/newdata/MYReport Report crt2009apr1-10 (3613 found).csv ]==>[ /var/www/kb_file/newdata/crt2009apr1-10.csv ]
==>>>> [ /var/www/kb_file/newdata/MYReport Report crt2009feb1-10 (4115 found).csv ]==>[ /var/www/kb_file/newdata/crt2009feb1-10.csv ]
==>>>> [ /var/www/kb_file/newdata/MYReport Report crtjan0911-20 (3156 found).csv ]==>[ /var/www/kb_file/newdata/crtjan0911-20.csv ]
==>>>> [ /var/www/kb_file/newdata/MYReport Report crt2009jan1-10 (2478 found).csv ]==>[ /var/www/kb_file/newdata/crt2009jan1-10.csv ]
==>>>> [ /var/www/kb_file/newdata/MYReport Report crtjan0921-31 (3797 found).csv ]==>[ /var/www/kb_file/newdata/crtjan0921-31.csv ]



Thanks for your help!!
 
Old 06-29-2009, 04:06 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
Did you try the script I suggested ?
 
Old 06-29-2009, 04:55 PM   #7
bowens44
LQ Newbie
 
Registered: Aug 2005
Posts: 25

Original Poster
Rep: Reputation: 15
Just tried the script you suggested...

Quote:
Originally Posted by jlliagre View Post
And here is something that might work:
Code:
for file in MYReport*
do
    target=$(echo $file | awk '{print $3}')
    if [ ! -f $target.csv ]
    then
        mv "$file" $target.csv
    fi
done
This works but I have no idea how. If you have the time,could you please explain it to me?

Thank you very much
 
Old 06-29-2009, 05:25 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
for each file whose name starts with MYReport,
extract the third word from the file name and make that the "target" variable.
If no file already exists with "target" as its name and ".csv" as its suffix,
rename the original file to it.
 
Old 06-29-2009, 07:52 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by bowens44 View Post
I tried using your script but apparently I'm doing something wrong.

I enter this;
# python filerenamer.py -p "^.* .* (crt.*) \(.*$" -e "\1.csv" -l "*.csv"

I get the following output but file names are not changed. Is there something that I have to specify in order to actually update the files name?
you need to remove "-l" to commit changes.
Code:
# python filerenamer.py -p "^.* .* (crt.*) \(.*$" -e "\1.csv" "*.csv"
use -h option to see more info. If you find you have problems after renaming, you can use -r to restore your changes(manually)

Last edited by ghostdog74; 06-29-2009 at 07:54 PM.
 
  


Reply


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
Replacing lines in files that contain special characters arizonagroovejet Linux - General 3 06-22-2009 10:19 PM
not showing files or folders with special characters, like umlauts pinknyunyu Slackware 16 05-06-2009 10:16 AM
Copying files with special characters cornish Linux - Newbie 13 12-21-2007 12:22 PM
Searching for files with special characters Yig Linux - Newbie 4 11-08-2007 06:53 PM
Solution: renaming multiple files with spaces HawkeyeCoug Linux - Newbie 0 03-26-2004 11:57 AM


All times are GMT -5. The time now is 06: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration