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 ...
- 07-12-2007 #1Just 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?
- 07-12-2007 #2
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
- 07-12-2007 #3
This is an ugly way to do it with sed:
This is a better way to do it with bash:Code:mv atmP* `ls atmP*|sed 's/^.\{12\}\(.\{14\}\).*/\1/g'`It asks before it might overwrite.Code:for i in atmP*; do mv -i $i ${i:12:14}; done
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/ ~>
- 07-12-2007 #4
I'm also a fan of:
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.Code:for file in atmP*; do mv "$file" "$(echo "$file" | cut -d. -f2-5)"; done
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
- 07-13-2007 #5Just Joined!
- Join Date
- Jul 2007
- Posts
- 2
that's awesome. much appreciated


Reply With Quote