Find and rename %20 from file names with a space in terminal

Find and rename %20 from file names with a space in terminal

At times, there are differences between your local or staging and production environment in handling URL encodings. In strict environments (likely production), HTML links to files with encoded characters in their file name will result to a 403 (not found) error. Therefore, you must rename the file names in your directory.

Install rename utility

In MacOS, use homebrew to install the rename utility.

$ brew install rename

Find and rename files

  1. Open the terminal app
  2. Change to the directory of your project
    $ cd [path/to/directory]
  3. Find (recursively) and rename files in the directory using the pattern and replacement
    $ find . -iname "*[pattern]*" -exec rename -f -n 's/[pattern]/[replacement]/g' '{}' \;
    For example, find and rename %20 from file names with a space
    $ find . -iname "*%20*" -exec rename -f -n 's/%20/ /g' '{}' \;

Rename switches

-f - Force renaming files (overwrite if exists)

-n - Dry run or print results of renaming

Enter rename --man to see full list of switches and how to use.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.