ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    file rename + syntax

    IT Discussion
    4
    19
    1.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      pattonb
      last edited by

      struggling with renaming 35k files. they all have a '~' at the end of the name, ie. blahblah.xls~, blahblah.docx~, etc.

      I thought rename would do the job, but I have something incorrect with the syntax, or I don't know what I am doing.
      The files are in many sub directories, all differing in depth. I tried the following, any suggestions ?

      find . -iname ".~" | rename -n 's/.~$//' . mmm

      weird, the window wasn't showing the last part of the command, so I added the 'm', so the '.' would show up ignore the m's

      but no action. sigh

      1 Reply Last reply Reply Quote 1
      • DustinB3403D
        DustinB3403
        last edited by

        I wrote a powershell script which would pull stupid characters out of file names. Let me find it.

        1 Reply Last reply Reply Quote 0
        • DustinB3403D
          DustinB3403
          last edited by

          Here ya go

          (Get-ChildItem -Path "G:\Some-Folder" -Recurse | Rename-Item -NewName {$_.Name -replace '•','' -replace '®','' -replace '™','' -replace '','' -replace '','' -replace '~','' -replace '$','' -replace '','' -replace '','' -replace '','' -replace '/','' -replace '|','-'} -ErrorAction SilentlyContinue -ErrorVariable daError) if ($daError.Exception.Message -match " Source and destination path must be different.") { Write-Output "ERROR - There was an error. Pay Attention : [$daError]" }

          Just delete all of the other replace,'box' and just leave one with the tilde.

          Obviously YMMV and no warranty is implied.

          P 2 Replies Last reply Reply Quote 0
          • P
            pattonb @DustinB3403
            last edited by

            @DustinB3403 thanks, but this is on a linux server.

            DustinB3403D P stacksofplatesS 3 Replies Last reply Reply Quote 0
            • DustinB3403D
              DustinB3403 @pattonb
              last edited by

              @pattonb said in file rename + syntax:

              @DustinB3403 thanks, but this is on a linux server.

              doh.

              Well crap. here I thought I had that solution.

              1 Reply Last reply Reply Quote 0
              • P
                pattonb @pattonb
                last edited by

                @pattonb if need be I could transfer the files, ( about 50gb), but would rather not.

                P DustinB3403D 2 Replies Last reply Reply Quote 0
                • P
                  pattonb @pattonb
                  last edited by

                  @pattonb it is a solution, and I appreciate the input, and I just may, if the frustration gets too high.
                  thank you

                  1 Reply Last reply Reply Quote 0
                  • DustinB3403D
                    DustinB3403 @pattonb
                    last edited by

                    @pattonb said in file rename + syntax:

                    @pattonb if need be I could transfer the files, ( about 50gb), but would rather not.

                    Have you looked at this?

                    P 1 Reply Last reply Reply Quote 0
                    • P
                      pattonb @DustinB3403
                      last edited by

                      @DustinB3403 I looked at similar. I had to do this a few years back and saved the command lines, then I started 'boning' up again, (today), I thought I had it. but obviously, I did something wrong. I was taking the easy route ( so I thought). The brain isn't working as well as I want today. LOL

                      DustinB3403D 1 Reply Last reply Reply Quote 0
                      • DustinB3403D
                        DustinB3403 @pattonb
                        last edited by

                        @pattonb said in file rename + syntax:

                        @DustinB3403 I looked at similar. I had to do this a few years back and saved the command lines, then I started 'boning' up again, (today), I thought I had it. but obviously, I did something wrong. I was taking the easy route ( so I thought). The brain isn't working as well as I want today. LOL

                        It is daylight savings, so no harm. My brain is shot too.

                        P 1 Reply Last reply Reply Quote 0
                        • P
                          pattonb @DustinB3403
                          last edited by

                          @DustinB3403 I just may transfer to a windows box, and use your script, just for the powershell experience.
                          I will let you know. thanks again

                          1 Reply Last reply Reply Quote 0
                          • GreyG
                            Grey
                            last edited by

                            https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7

                            P 1 Reply Last reply Reply Quote 1
                            • P
                              pattonb @Grey
                              last edited by

                              @Grey interesting, Has anybody tried it ? It seems to me that powershell syntax is much longer than linux,
                              my powershell knowledge is quite limited, but may not stay that way........

                              thank you

                              GreyG 1 Reply Last reply Reply Quote 0
                              • GreyG
                                Grey @pattonb
                                last edited by Grey

                                @pattonb said in file rename + syntax:

                                @Grey interesting, Has anybody tried it ? It seems to me that powershell syntax is much longer than linux,
                                my powershell knowledge is quite limited, but may not stay that way........

                                thank you

                                I used it. In fact, I had a similar situation to yours on a linux box and I knew the answer in pwsh terms, but not sh. So, I installed, performed my action and got on with life.

                                You might ask the @climagic guy on twitter for a linux solution or check out climagic.org for the archives.

                                P 1 Reply Last reply Reply Quote 0
                                • P
                                  pattonb @Grey
                                  last edited by

                                  @Grey where does the destination path go from the above script by DustinB3403

                                  GreyG 1 Reply Last reply Reply Quote 0
                                  • GreyG
                                    Grey @pattonb
                                    last edited by

                                    @pattonb said in file rename + syntax:

                                    @Grey where does the destination path go from the above script by DustinB3403

                                    There is none. He's using rename-item. It's a direct rename.

                                    Get-ChildItem -Path "G:\Some-Folder" -Recurse
                                    

                                    This part searches a folder a subdirectories to create all the file objects for enumeration.

                                    | Rename-Item -NewName {$_.Name -replace '•','' -replace '®','' -replace '™','' -replace '','' -replace '','' -replace '~','' -replace '$','' -replace '','' -replace '','' -replace '','' -replace '/','' -replace '|','-'}
                                    

                                    Pipe the list of files to the rename cmdlet that has the -newname function, which acts on the list of -replacements.

                                    The rest handles errors. The parens encaps it to work as a single command line, with error handling.

                                    1 Reply Last reply Reply Quote 0
                                    • P
                                      pattonb @DustinB3403
                                      last edited by

                                      @DustinB3403 I went to a windows box, had some errors, sorted all of them except this one. I put in the command I used.
                                      any assistance is greatly appreciated.

                                      the error message first
                                      At line:1 char:154

                                      • ... '-'} -ErrorAction SilentlyContinue -ErrorVariable daError) if ($daErr ...
                                      •                                                            ~~
                                        

                                      Unexpected token 'if' in expression or statement.
                                      + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
                                      + FullyQualifiedErrorId : UnexpectedToken

                                      and the command I used is,

                                      (Get-ChildItem -Path "D:\bradford_temp" -Recurse | Rename-Item -NewName {$_.Name -replace '~','-'} -ErrorAction SilentlyContinue -ErrorVariable daError) if ($daError.Exception.Message -match " Source and destination path must be different.") { Write-Output "ERROR - There was an error. Pay Attention : [$daError]" }

                                      1 Reply Last reply Reply Quote 0
                                      • stacksofplatesS
                                        stacksofplates @pattonb
                                        last edited by stacksofplates

                                        @pattonb said in file rename + syntax:

                                        @DustinB3403 thanks, but this is on a linux server.

                                        Is this a Fedora derivative? If so you need prename not just rename.

                                        This should work:

                                        find . -name "*~" -exec prename -n 's/~$//' {} \;
                                        
                                        P 1 Reply Last reply Reply Quote 2
                                        • P
                                          pattonb @stacksofplates
                                          last edited by

                                          @stacksofplates

                                          works exactly as intended, my sincere appreciation.. Now I have to try and understand, the syntax
                                          and what I missed. All I had to do was change prename to rename ( Debian distro)
                                          thanks, kindly

                                          1 Reply Last reply Reply Quote 0
                                          • 1 / 1
                                          • First post
                                            Last post