Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You can use sed like so:

    Code:
    sed 's/\./_/'
    This will do the following:

    Code:
    user@host:~$ echo "hello.world.txt" | sed 's/\.//'
    helloworld.txt
    Linux User #453176

  3. #3
    Just 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 "."

  4. #4
    Just 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

  5. #5
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    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

  6. #6
    Just 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

  7. #7
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You can use a for loop:

    Code:
    for filename in `ls` ; do mv $filename `echo $filename | sed s/localhost_access_log\.//` ; done
    Here is what happens when used:

    Code:
    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
    Make sure you have a backup before you run it incase something crashes
    Linux User #453176

  8. #8
    Just Joined!
    Join Date
    Mar 2009
    Posts
    6
    That did it! Thanks so much Kieren.

  9. #9
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    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

  10. #10
    Just Joined!
    Join Date
    Mar 2009
    Posts
    6
    Thanks Radoluv. A lot of great knowledge here.

    This is one for the files!

    Regards,
    Wayne

Posting Permissions

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