Skip to content

A practical cross-platform command-line tool for safely batch renaming files/directories via regular expression

License

Notifications You must be signed in to change notification settings

shenwei356/brename

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

brename -- a cross-platform command-line tool for safely batch renaming files/directories via regular expression

Built with GoLang Go Report Card Cross-platform Latest Version Github Releases

brename is a cross-platform command-line tool for safely batch renaming files/directories via regular expression.

Table of Contents

Features

  • Cross-platform. Supporting Windows, Mac OS X and Linux.
  • Safe. By checking potential conflicts and errors.
  • File filtering. Including and excluding files via regular expression. No need to run commands like find ./ -name "*.html" -exec CMD.
  • Recursively renaming both files and directories.
  • Supporting dry run.

Installation

brename is implemented in Go programming language, executable binary files for most popular operating systems are freely available in release page.

Method 1: Download binaries

brename v2.1 Github Releases (by Release)

OS Arch File, (mirror为中国用户下载镜像链接) Download Count
Linux 32-bit brename_linux_386.tar.gz, (mirror) Github Releases (by Asset)
Linux 64-bit brename_linux_amd64.tar.gz, (mirror) Github Releases (by Asset)
OS X 32-bit brename_darwin_386.tar.gz, (mirror) Github Releases (by Asset)
OS X 64-bit brename_darwin_amd64.tar.gz, (mirror) Github Releases (by Asset)
Windows 32-bit brename_windows_386.exe.tar.gz, (mirror) Github Releases (by Asset)
Windows 64-bit brename_windows_amd64.exe.tar.gz, (mirror) Github Releases (by Asset)

Just download compressed executable file of your operating system, and decompress it with tar -zxvf *.tar.gz command or other tools. And then:

  1. For Linux-like systems

    1. If you have root privilege simply copy it to /usr/local/bin:

       sudo cp brename /usr/local/bin/
      
    2. Or add the current directory of the executable file to environment variable PATH:

       echo export PATH=\$PATH:\"$(pwd)\" >> ~/.bashrc
       source ~/.bashrc
      
  2. For windows, just copy brename.exe to C:\WINDOWS\system32.

Method 2: For Go developer

go get -u github.com/shenwei356/brename/

Usage

brename -- a cross-platform command-line tool for safely batch renaming files/directories via regular expression

Version: 2.1

Author: Wei Shen <[email protected]>

Homepage: https://github.com/shenwei356/brename

Attention:
  1. Paths starting with "." is ignored.
  2. Overwriting existed files is not allowed.
  3. Flag -f/--include-filters and -F/--exclude-filters support multiple values,
     e.g., -f ".html" -f ".htm".
     But ATTENTION: comma in filter is treated as separater of multiple filters.

Usage:
  brename [flags]

Examples:
  1. dry run and showing potential dangerous operations
      brename -p "abc" -d
  2. dry run and only show operations that will cause error
      brename -p "abc" -d -v 2
  3. only renaming specific paths via include filters
      brename -p ":" -r "-" -f ".htm$" -f ".html$"
  4. renaming all .jpeg files to .jpg in all subdirectories
      brename -p "\.jpeg" -r ".jpg" -R   dir
  5. using capture variables, e.g., $1, $2 ...
      brename -p "(a)" -r "\$1\$1"
      or brename -p "(a)" -r '$1$1' in Linux/Mac OS X
  6. renaming directory too
      brename -p ":" -r "-" -R -D   pdf-dirs

  More examples: https://github.com/shenwei356/brename

Flags:
  -d, --dry-run                       print rename operations but do not run
  -F, --exclude-filters stringSlice   exclude file filter(s) (regular expression, case ignored). multiple values supported, e.g., -F ".html" -F ".htm", but ATTENTION: comma in filter is treated as separater of multiple filters
  -i, --ignore-case                   ignore case
  -f, --include-filters stringSlice   include file filter(s) (regular expression, case ignored). multiple values supported, e.g., -f ".html" -f ".htm", but ATTENTION: comma in filter is treated as separater of multiple filters (default [.])
  -D, --including-dir                 rename directories
  -p, --pattern string                search pattern (regular expression)
  -R, --recursive                     rename recursively
  -r, --replacement string            replacement. capture variables supported.  e.g. $1 represents the first submatch. ATTENTION: for *nix OS, use SINGLE quote NOT double quotes or use the \ escape character.
  -v, --verbose int                   verbose level (0 for all, 1 for warning and error, 2 for only error)
  -V, --version                       print version information and check for update

Examples

Take a directory for example:

$ tree
.
├── abc
│   ├── A.JPEG
│   ├── B.HTM
│   └── B.JPEG
├── a.jpeg
├── b.html
└── b.jpeg
  1. Recursively renaming all .jpeg files to .jpg in all subdirectories (-R/--recursive). A dry run is firstly performed for safety checking (-d/--dry-run).

     $ brename -p "\.jpeg" -r ".jpg" -R -d
     [INFO] checking: a.jpeg -> a.jpg [ok]
     [INFO] checking: b.jpeg -> b.jpg [ok]
     [INFO] 2 paths to be renamed
    
     $ brename -p "\.jpeg" -r ".jpg" -R
     [INFO] checking: a.jpeg -> a.jpg [ok]
     [INFO] checking: b.jpeg -> b.jpg [ok]
     [INFO] 2 paths to be renamed
     [INFO] renamed: a.jpeg -> a.jpg
     [INFO] renamed: b.jpeg -> b.jpg
     [INFO] 2 paths renamed
    
     $ tree
     .
     ├── abc
     │   ├── A.JPEG
     │   ├── B.HTM
     │   └── B.JPEG
     ├── a.jpg
     ├── b.html
     └── b.jpg
    
  2. Dry run and only showing operations that will cause error (-v/--verbose)

     # default value of -v is 0
     $ brename -p a -r b -R -d
     [INFO] checking: a.jpeg -> b.jpeg [new path existed]
     [INFO] checking: abc -> bbc [ok]
     [ERRO] 1 potential errors detected, please check
    
     $ brename -p a -r b -R -D -d -v 2
     [INFO] checking: a.jpeg -> b.jpeg [new path existed]
     [ERRO] 1 potential errors detected, please check
    
  3. Ignoring cases (-i/--ignore-case)

     $ brename -p "\.jpeg" -r ".jpg" -R -i
     [INFO] checking: abc/A.JPEG -> abc/A.jpg [ok]
     [INFO] checking: abc/B.JPEG -> abc/B.jpg [ok]
     [INFO] 2 paths to be renamed
     [INFO] renamed: abc/A.JPEG -> abc/A.jpg
     [INFO] renamed: abc/B.JPEG -> abc/B.jpg
     [INFO] 2 paths renamed
    
     $ tree
     .
     ├── abc
     │   ├── A.jpg
     │   ├── B.HTM
     │   └── B.jpg
     ├── a.jpg
     ├── b.html
     └── b.jpg
    
  4. Using capture variables, e.g., $1, $2 ...

     # or brename -p "(a)" -r '$1$1' in Linux/Mac OS X
     $ brename -p "(a)" -r "\$1\$1" -i
     [INFO] checking: a.jpg -> aa.jpg [ok]
     [INFO] 1 paths to be renamed
     [INFO] renamed: a.jpg -> aa.jpg
     [INFO] 1 paths renamed
    
     $ tree
     .
     ├── aa.jpg
     ├── abc
     │   ├── A.jpg
     │   ├── B.HTM
     │   └── B.jpg
     ├── b.html
     └── b.jpg
    
  5. Renaming directory too (-D/--including-dir)

     $ brename -p "a" -r "A" -R -D
     [INFO] checking: aa.jpg -> AA.jpg [ok]
     [INFO] checking: abc -> Abc [ok]
     [INFO] 2 paths to be renamed
     [INFO] renamed: aa.jpg -> AA.jpg
     [INFO] renamed: abc -> Abc
     [INFO] 2 paths renamed
    
     $ tree
     .
     ├── AA.jpg
     ├── Abc
     │   ├── A.jpg
     │   ├── B.HTM
     │   └── B.jpg
     ├── b.html
     └── b.jpg
    
  6. Only renaming specific files via include filters (regular expression) (-f/--include-filters)

     $ brename -p "^" -r "hello " -f ".htm$" -f ".html$" -R
     [INFO] checking: Abc/B.HTM -> Abc/hello B.HTM [ok]
     [INFO] checking: b.html -> hello b.html [ok]
     [INFO] 2 paths to be renamed
     [INFO] renamed: Abc/B.HTM -> Abc/hello B.HTM
     [INFO] renamed: b.html -> hello b.html
     [INFO] 2 paths renamed
    
     $ tree
     .
     ├── AA.jpg
     ├── Abc
     │   ├── A.jpg
     │   ├── B.jpg
     │   └── hello\ B.HTM
     ├── b.jpg
     └── hello\ b.html
    
  7. Excluding files via exclude filters (regular expression) (-F/--exclude-filters)

     $ brename -p b -r c -d
     [INFO] checking: b.jpg -> c.jpg [ok]
     [INFO] checking: hello b.html -> hello c.html [ok]
     [INFO] 2 paths to be renamed
    
     $ brename -p b -r c -d -F '.html$'
     [INFO] checking: b.jpg -> c.jpg [ok]
     [INFO] 1 paths to be renamed
    

Contact

Create an issue to report bugs, propose new functions or ask for help.

License

MIT License