LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find and Rename files made by Windoze users (http://www.linuxquestions.org/questions/linux-newbie-8/find-and-rename-files-made-by-windoze-users-756810/)

wademac 09-21-2009 02:12 PM

Find and Rename files made by Windoze users
 
I have a windows user that creates csv files and ftp to a linux server but they never follow any case structure so the file maybe

ABC.csv or Abc.CSV or Abc.CSV etc..

I need to find and rename it to just abc.csv

rn_ 09-21-2009 02:26 PM

Please clarify, is your question on how to find the file, or how to rename the file?

You could use the 'find' (man find) command to find the files. The '-name' parameter can accept wild-cards (eg. '*.[Cc][Ss][Vv]')

and you can use the 'tr' (man tr) command to change the name to lower case (tr [:upper:] [:lower:]).

put these two into a loop or something and you will have a script that renames the file.

wademac 09-22-2009 08:33 AM

Quote:

Originally Posted by rn_ (Post 3691957)
Please clarify, is your question on how to find the file, or how to rename the file?

You could use the 'find' (man find) command to find the files. The '-name' parameter can accept wild-cards (eg. '*.[Cc][Ss][Vv]')

and you can use the 'tr' (man tr) command to change the name to lower case (tr [:upper:] [:lower:]).

put these two into a loop or something and you will have a script that renames the file.

The reason I need to find the file first because I dont know the case they upload the file in so a find that will ignore case and change just the file name to lower case

so find -iname file.csv and finds the file FILE.CSV than takes what if fines and runs the found file in a tr command something like

tr -s '[a-z][A-Z]' '[A-Z][a-z]' <FILE.CSV> file.csv

The problem is I not sure how to wrap this into one magic script :)

wademac 09-22-2009 08:43 AM

I think all I need to do is FIND the file and use a move command after it is found!

:) what do you think?

malekmustaq 09-22-2009 09:04 AM

wademac,

yes, if you can find the file you can change its name accordingly.
use "-atime n" switch command of "find" to access newly created file regardless of filename.
use the manual for command "find" to get acquainted to it.
user@host--$: man find

hope this helps.
good luck.

rn_ 09-22-2009 10:18 AM

Quote:

Originally Posted by wademac (Post 3692799)
The problem is I not sure how to wrap this into one magic script :)

you're close. i just didn't feel like ruining your fun in finding out for yourself how to do it. lot's of examples out there...

you sure you don't want to try for yourself first? ...


** !!WARNING!! ** spoiler ahead. stop here if you'd rather research it yourself ...



Code:

for fn in `find /dirname -iname '*.csv'`
do
    nfn=`echo $fn | tr [:upper:] [:lower:]`

    #Optional
    # if it's already in lower case, no need to rename
    [[ $fn = $nfn ]] && continue
    #

    echo renaming $fn to $nfn
    mv $fn $nfn
done



HTH.
-RN.


All times are GMT -5. The time now is 08:46 PM.