LinuxQuestions.org
Help answer threads with 0 replies.
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 06-29-2009, 10: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, 11:19 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 11:41 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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, 11:51 AM   #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, 12: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, 03:06 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Did you try the script I suggested ?
 
Old 06-29-2009, 03: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, 04:25 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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, 06: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 06:54 PM.
 
  


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

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

All times are GMT -5. The time now is 03:51 PM.

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