LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   renaming files with spaces and special characters. (https://www.linuxquestions.org/questions/linux-newbie-8/renaming-files-with-spaces-and-special-characters-736430/)

bowens44 06-29-2009 10:53 AM

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.

pixellany 06-29-2009 11:19 AM

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.

jlliagre 06-29-2009 11:41 AM

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


ghostdog74 06-29-2009 11:51 AM

Quote:

Originally Posted by bowens44 (Post 3590194)
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)
}'


bowens44 06-29-2009 12:54 PM

Quote:

Originally Posted by ghostdog74 (Post 3590272)
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!!

jlliagre 06-29-2009 03:06 PM

Did you try the script I suggested ?

bowens44 06-29-2009 03:55 PM

Just tried the script you suggested...
 
Quote:

Originally Posted by jlliagre (Post 3590263)
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

jlliagre 06-29-2009 04:25 PM

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.

ghostdog74 06-29-2009 06:52 PM

Quote:

Originally Posted by bowens44 (Post 3590362)
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)


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