Re: rename and move

From: jpd (read_the_sig_at_do.not.spam.it)
Date: 07/20/04


Date: Tue, 20 Jul 2004 05:12:14 +0000 (UTC)

On 2004-07-20, Marshall McCabe <mccabe@omsc.net> wrote:
> Is there a way to rename and move a file by executing a script?

Yes. Have the script execute a mv(1) on the file. You can also do
it directly, without a script but from the command line.

> We
> need to shorten long file names and them move the newly named file to
> a different directory. An example of the current names is:
>
> MB4010A1835FFP.20040716.115956.1548
>
> we would like to nename to 20040716.115956
>
> All of the files have the same characters until the date (20040716)

Of course you can do that. I'd write an one-liner but for the sake of
readability I'll provide something script-like:

#!/bin/sh
for file in MB4010A1835FFP.*
        # strip off the first bit
        newfile="${file##MB4010A1835FFP.}"
        # strip off the last dot and four digits
        newfile=`echo $newfile|sed 's/\.[0-9][0-9][0-9][0-9]$//'`
        # if there were more files with the same date and subsecond
        # suffix, only the last will survive
        mv "$file" "$newfile"
end

You could delete the first step and integrate it into the sed statement.
You could rewrite the sed statement to use esed and/or a repeat count.
This will not work for subdirectories, but you could use find(1) in a
variety of ways to compensate for that. To move files to a new directory,
insert the directory in the apropriate place.

If you need to do this kind of thing more often, I'd suggest getting
a book on shell programming and the use of sed and regexpen. This is
a fairly basic question for anyone familiar with un*x.

-- 
  j p d (at) d s b (dot) t u d e l f t (dot) n l .


Relevant Pages

  • Executing script in Windows XP in the background.
    ... I recently wrote a script that emails me if any of my x-10 sensors is tripped. ... I like the starpack version as it is smaller and for some reason it looks easier for me to work on starkit. ... What I want is once I hit that exe file or my x-10 sensor launch it, for the script to execute in the background. ... Right now I can see the a black screen open and the script execute. ...
    (comp.lang.tcl)
  • Re: Executing a Script every hour
    ... > Is there a simpe way to have a script execute at shorter time ... I'd like to run the swendeleter.pl script automatically ... these are parts of the system crontab file. ... "Theoretical politics is interesting. ...
    (Debian-User)
  • Re: Logoff script!
    ... So if I can't do that, can you explain to me when does the logoff ... does it execute whenever any user logoff, ... Actually I'm new in scripting with visual basic script ...
    (microsoft.public.windows.server.active_directory)
  • Re: Kickstart script on server?
    ... > actually user select's category from a list on an asp-page and types his ... Then you can have multiple rows and have the script execute once for each ...
    (microsoft.public.inetserver.asp.general)
  • Re: Script to Rename Computer Name in Domain
    ... i looking a script to rename computer name in domain server 2003 ... To rename a computer you bind to the parent OU/Container of the computer ... you would need to also prompt for the current name. ... ' Bind to the parent OU/container of computer object. ...
    (microsoft.public.windows.server.scripting)