Find the answer to your Linux question:
Results 1 to 5 of 5
hi, i'm quite new to all this and this problem has had me going for too long now so I thought I'd ask for some advice. I've got thousands of ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    2

    renaming problem

    hi, i'm quite new to all this and this problem has had me going for too long now
    so I thought I'd ask for some advice. I've got thousands of files that look like this

    atmPrf_CHAM.2005.004.17.22.G29_2007.1200_nc
    atmPrf_CHAM.2005.004.17.27.G28_2007.1200_nc
    atmPrf_CHAM.2005.004.17.34.G11_2007.1200_nc
    atmPrf_CHAM.2005.004.17.36.G09_2007.1200_nc


    The 2005.004.17.22 bit is the date and time. I want to remove the rest of the filename
    so I am just left with the date and time part.
    i.e so they all look like this

    2005.004.17.22
    2005.004.17.27
    2005.004.17.34
    2005.004.17.36

    I can't get this to work. Any ideas?

  2. #2
    Linux Newbie rudie_rage's Avatar
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    133
    I can imagine with a little bash scripting knowledge and some creativity you could write a small script to rename files in a directory.

    something that did something like:
    ls atm* > filelist.txt (make a file with all filenames you wish to change)
    then you would have to find out how many lines in that file... dunno how to do that. save it into a bash variable.
    then for 1-n lines, use head -n to pick out an individual filename, then use mv to rename it.

    Im horrible at bash scripting, never really got into it... But if there are commands that fill in the missing holes in this process it can be done.

    Also, a program like this would be easily changable to be used in any situation where you want to rename a bunch of files that follow a simple naming scheme. It would be a pretty useful little script if you ask me.
    Living the digital dream....
    Disclaimer: I may be wrong since I was once before.
    Breathe out so I can breathe you in ~~Everlong

  3. #3
    Linux User muha's Avatar
    Join Date
    Jan 2006
    Posts
    290
    This is an ugly way to do it with sed:
    Code:
    mv atmP* `ls atmP*|sed 's/^.\{12\}\(.\{14\}\).*/\1/g'`
    This is a better way to do it with bash:
    Code:
    for i in atmP*; do mv -i $i ${i:12:14}; done
    It asks before it might overwrite.
    Good luck!
    Now what? You have Linux installed and running. The GUI is working fine, but you are getting tired of changing your desktop themes. You keep seeing this "terminal" thing. Don't worry, they'll show you what to do @
    <~ http://www.linuxcommand.org/ ~>

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I'm also a fan of:
    Code:
    for file in atmP*; do mv "$file" "$(echo "$file" | cut -d. -f2-5)"; done
    The reason for this is that muha's solutions are specific to the length of your filenames. If you were doing this with a filename of a different length, they would not work. My solution is based on the .-separated fields, the number and order of which would remain the same.

    What mine does is that, for every file startming with 'atmP', it splits the file up into separate fields separated by the '.' character. It then pieces back together only fields 2-5, and moves the original file to this new string.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    Jul 2007
    Posts
    2
    that's awesome. much appreciated

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...