I was recently working on a system that used a lot of flat-file db's. After the file was processed by #!perl or #!sh via cron, the file would be renamed filename~. When we would kill/restart processes, we would rename the files to their original. Pain when you have more than a few....
perl -e 'opendir(D,".");for (grep /~$/, readdir D){s/~$//;rename("$_~" +,"$_");}'

Replies are listed 'Best First'.
Re: remove tilda from filenames with 1 liner
by japhy (Canon) on Jan 22, 2002 at 02:47 UTC
    (Updated, thanks to screamingeagle.) Kill two birds with one stone: for (grep s/~\z//, readdir D) { rename "$_~" => $_ }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      correct me if I'm wrong, but shouldnt the last part of your example be :
      rename "$_~" => $_
      instead of
      rename $_ => "$_~"

      What's the difference between \z and $

      gav^

        You should really consult perlre for this. $ can match before a newline at the end of the string, whereas \z matches only at the absolute end of a string. If you have a file that ends in a newline, this is important.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: remove tilda from filenames with 1 liner
by Anonymous Monk on Jan 23, 2002 at 00:14 UTC
    2 seconds later: for i in $(ls|sed -n 's/\(.*\)~$/\1/p');do mv $i~ $i;done done
Re: remove tilda from filenames with 1 liner
by fundflow (Chaplain) on Feb 11, 2002 at 16:30 UTC