Move files using wildcards ??? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Move files using wildcards ???

UNIX for Dummies Questions & Answers


Tags
linux

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-20-2001
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
 
Join Date: Nov 2001
Last Activity: 23 October 2011, 10:07 PM EDT
Location: Brisbane, Australia
Posts: 509
Thanks: 7
Thanked 0 Times in 0 Posts
Data Move files using wildcards ???

Hi all,

Would like to rename all files using wildcards - if at all possible!

As an example I have the following files:
Nov01_df
Nov02_df
Nov03_df
......
Nov28_df
Nov29_df
Nov30_df

I'd like to have these renamed as "df??" where ?? is the number from the original file name.

Any suggestions??
Sponsored Links
    #2  
Old 12-20-2001
rwb1959's Avatar
rwb1959 rwb1959 is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 16 October 2009, 2:04 PM EDT
Location: Virginia, USA
Posts: 438
Thanks: 0
Thanked 1 Time in 1 Post
The only practical way to do this is in a loop in a shell...

for fname in Nov*_df
do
# in here you build the new name
# then rename the $fname to the new name
done

...I left a "little" work for you to try
Sponsored Links
    #3  
Old 12-20-2001
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
 
Join Date: Nov 2001
Last Activity: 23 October 2011, 10:07 PM EDT
Location: Brisbane, Australia
Posts: 509
Thanks: 7
Thanked 0 Times in 0 Posts
rwb1959,

Thanks heaps for the reply.

The build should be:

Nname="df"`echo $fname | cut -c4-5`
rename $fname Nname


Is this correct?
I'm preparing this at home before I go back to workon Monday.

Again, many thanks.

BTW - Why is it that mv is unable to handle wildcards in a similar fashion to: mv Nov??_df df??.
    #4  
Old 12-21-2001
Jimbo
Guest
 
Posts: n/a
That looks OK, except that "rename" should be "mv" (slipping back into DOS mode, are you?)

The mv will clobber any existing files in the way, such as an older df01. If you want protection from this, then use mv -i, or your loop could check for existence before the mv.

Also, if you are going to cut exactly two columns, you should drive off of Nov??_df instead of Nov*_df. If you use asterisk, and you happen to have:

Nov01_df
Nov01b_df

you would rename the first entry to df01, then you would rename the second entry to df01 also, thus overlaying the first entry. Using Nov??_df will be consistent with cutting two characters.

Or if you do want to process Nov*_df files, you could use the following for your Nname assignment:

Nname=`expr $x : "Nov\(.*\)_df"`df

And an attempt at answering your BTW:

mv is simply not designed to rename a series of files into a series of new filenames. And I believe that the filename pattern matching expansion is done by the shell before mv ever sees it. Therefore:

mv Nov??_df mydir

would get changed to

mv Nov01_df Nov02_df Nov03_df mydir

before mv sees it. The mv command never gets to see the pre-expanded command.
Sponsored Links
    #5  
Old 12-21-2001
rwb1959's Avatar
rwb1959 rwb1959 is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 16 October 2009, 2:04 PM EDT
Location: Virginia, USA
Posts: 438
Thanks: 0
Thanked 1 Time in 1 Post
I think Jimbo answered your question about
mv and wild cards. In a nutshell, if the
file name "Nov*df" expands into more than one
file name, you could not logically move it
to a single target file name. Only a directory
name would be valid in this case.

Also, the line...
rename $fname Nname

...should be...
mv $fname $Nname

...don't forget that leading "$" when using
variables in your script (which of course is
not used in variable assignment).
Sponsored Links
    #6  
Old 12-25-2001
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
 
Join Date: Jul 2001
Last Activity: 16 June 2011, 4:50 PM EDT
Location: Portland, OR, USA
Posts: 1,626
Thanks: 2
Thanked 15 Times in 13 Posts
Well, on some systems (at least Linux systems), there IS a "rename" command. It is MUCH more portable to use "mv", however...

man rename #on a Slackware system:

Code:
RENAME(1)           Linux Programmer's Manual           RENAME(1)

NAME
       rename - Rename files

SYNOPSIS
       rename from to file...

DESCRIPTION
       rename  will rename the specified files by replacing the first occurrence of
       from in their name by to.

       For example, given the files foo1, ..., foo9, foo10, ..., foo278,  the  com-
       mands

              rename foo foo0 foo?
              rename foo foo0 foo??

       will turn them into foo001, ..., foo009, foo010, ..., foo278.

       And
              rename .htm .html *.htm

       will fix the extension of your html files.

SEE ALSO
       mv(1)

                          1 Januari 2000                        1

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
copying files with wildcards goldenone UNIX for Dummies Questions & Answers 7 02-15-2012 05:14 PM
Move same files and issue ls -al command on remaining files Trinimini UNIX for Dummies Questions & Answers 1 03-12-2011 06:11 PM
Perl, open multiple files with wildcards japaneseguitars Shell Programming and Scripting 6 06-14-2010 09:11 AM
rename multiple files with wildcards karthikn7974 Shell Programming and Scripting 5 10-02-2009 01:39 PM
Using cat to combine files using wildcards Enobarbus37 Shell Programming and Scripting 3 04-03-2008 09:08 AM



All times are GMT -4. The time now is 07:40 PM.