Results 1 to 10 of 10
Thank you for any help offered.
I have a directory of log files with the following name structure: (inherited this mess!)
localhost_access_log.2009-03-09.txt
localhost_access_log.2009-03-10.txt
I need to ftp these files to ...
- 03-10-2009 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 6
Rename multiple files with two "."
Thank you for any help offered.
I have a directory of log files with the following name structure: (inherited this mess!)
localhost_access_log.2009-03-09.txt
localhost_access_log.2009-03-10.txt
I need to ftp these files to another system but because of the two "."s, ftp is reporting "Unknown File Extension".
Can anyone help with the rename command that can change the first "." to a "_" and still keep the date integrity? I would like to end up with the following:
localhost_access_log_2009-03-09.txt
localhost_access_log_2009-03-10.txt
If there is a posted solution that would help as well.
Regards,
Wayne
- 03-10-2009 #2
You can use sed like so:
This will do the following:Code:sed 's/\./_/'
Code:user@host:~$ echo "hello.world.txt" | sed 's/\.//' helloworld.txt
Linux User #453176
- 03-10-2009 #3Just Joined!
- Join Date
- Mar 2009
- Posts
- 6
Just realized that it may be simplier if the new file name was just:
2009-03-09.txt
2009-03-10.txt
That is, just pull the name from everything after the first "."
- 03-10-2009 #4Just Joined!
- Join Date
- Mar 2009
- Posts
- 6
Thanks Kieren. Sorry to admit that I'm still pretty much a linux novice and will need a bit of hand-holding. Is your code issued from the command line? That's what I'm hoping to find. If so could you provide more detail?
Thanks,
Wayne
- 03-10-2009 #5
sed can do that too:
Code:user@host:~$ echo "localhost_access_log.2009-03-10.txt" | sed 's/localhost_access_log\.//' 2009-03-10.txt
Linux User #453176
- 03-10-2009 #6Just Joined!
- Join Date
- Mar 2009
- Posts
- 6
Is there a way to build that into a statement so it can work for all files in the directory? I have close to 400 files and dread the thought of the manual work to change them one by one!
Appreciate your comebacks.
Wayne
- 03-10-2009 #7
You can use a for loop:
Here is what happens when used:Code:for filename in `ls` ; do mv $filename `echo $filename | sed s/localhost_access_log\.//` ; done
Make sure you have a backup before you run it incase something crashesCode:kieren@mouse:~/temp$ ls localhost_access_log_2009-03-09.txt localhost_access_log_2009-03-10.txt localhost_access_log_2009-03-11.txt kieren@mouse:~/temp$ for filename in `ls` ; do mv $filename `echo $filename | sed s/localhost_access_log\.//` ; done kieren@mouse:~/temp$ ls 2009-03-09.txt 2009-03-10.txt 2009-03-11.txt
Linux User #453176
- 03-10-2009 #8Just Joined!
- Join Date
- Mar 2009
- Posts
- 6
That did it! Thanks so much Kieren.
- 03-10-2009 #9
If you have the rename utility:
Code:rename 's/localhost_access_log.//' localhost_access_log.*.txt
Or zsh:
Code:autoload -U zmv zmv 'localhost_access_log.(*.txt)' '$1'
Otherwise with Perl:
Code:perl -e' map { ($n = $_) =~ s/[^.]+\.//; rename $_, $n } glob "localhost_access_log.*.txt" '
Or pure shell:
Code:for f in localhost_access_log.*.txt; do mv -- "$f" "${f#*.}" done
- 03-10-2009 #10Just Joined!
- Join Date
- Mar 2009
- Posts
- 6
Thanks Radoluv. A lot of great knowledge here.
This is one for the files!
Regards,
Wayne


Reply With Quote