Rename folder on Linux

Bits Lovers
Written by Bits Lovers on
Rename folder on Linux

Renaming folders on Linux is straightforward once you know a few commands. I’ll cover the tools I use daily: mv for simple renames and rename for batch operations.

I keep my filesystem organized, and knowing how to rename directories quickly saves time. Let me show you what works.

The mv Command

The mv command (short for “move”) handles both moving and renaming. The syntax is simple:

mv [OPTIONS] source destination

For example, to rename a directory from bitslovers to bitslovers-temp:

mv bitslovers bitslovers-temp

Just remember: the first argument is what you’re renaming, the second is the new name.

Here’s something that trips people up: if the destination already exists as a directory, mv moves the source inside it instead of renaming. So if bitslovers-temp exists, mv bitslovers bitslovers-temp puts bitslovers inside bitslovers-temp.

Finding and Renaming Directories

Sometimes I don’t remember exactly where a folder sits in the filesystem. The find command helps:

find / -depth -type d -name "backup"

That shows the full path, like /home/bitslovers/backup. Then I can rename it:

find / -depth -type d -name backup -execdir mv {} old-backup \;

The -execdir option runs mv from the directory containing the match.

Batch Renaming with a Loop

Renaming multiple directories at once? A simple for loop works well:

for a in *;
do if [ -d "$a" ]; then
 mv -- "$a" "${a}_$(date +%Y%m%d)"
fi
done

This appends today’s date to every directory in the current folder. Let me break it down:

  • The for loop iterates through everything
  • The if test checks if it’s a directory (-d)
  • The mv command renames with a date suffix

The -- after mv prevents filenames starting with - from being interpreted as options.

The rename Command

For more complex renaming, the rename utility is powerful. On Ubuntu or Debian:

sudo apt-get install -y rename

The rename command uses Perl regular expressions. It’s more flexible than mv but requires knowing regex basics.

To convert uppercase to lowercase:

rename 'y/A-Z/a-z/' *

This replaces all uppercase letters with lowercase in all files and folders.

You can target specific patterns. To rename directories ending in _config to _backup:

rename 'y/_config/_backup/' *

Or filter what gets renamed:

ls -d *_config | rename 'y/*_config/*_backup/'

The mmv Utility

Another option I like is mmv for batch operations:

sudo apt-get install mmv

Say I have files like picture-year-1.png through picture-year-100.png and want to change “year” to “2021”:

mmv picture-year\* picture-2021\#1

The #1 refers to the first wildcard (everything matching *). The result: picture-2021-1.png, picture-2021-2.png, etc.

To change file extensions:

mmv \*.data \#1.json

This converts all .data files to .json.

My Take

I mostly use mv for quick renames—it’s installed everywhere and does the job. When I need batch renaming, rename or mmv save time. The key is knowing which tool fits the situation.

The find and mv combination handles complex scenarios when you’re not sure where files are. Start with mv, then add the others as needed.

Sources:

Bits Lovers

Bits Lovers

Professional writer and blogger. Focus on Cloud Computing.

Comments

comments powered by Disqus