Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Rename Files That Start With a Special Character

July 13, 2023 — Gregg Szumowski

Suppose you find that you have a file with a special character and you want to delete it:

$ ls
-badfile.txt PrintHood reg57.txt
Favorites Recent scripts

$ rm -badfile.txt
rm: invalid option -- 'b'
Try 'rm ./-badfile.txt' to remove the file '-badfile.txt'.
Try 'rm --help' for more information.

$ ls *.txt
ls: invalid option -- 'e'
Try 'ls --help' for more information.

First, find the inode of the file by using ls -i on the command line:

$ ls -i
54804119 -badfile.txt 56634824 PrintHood
56634825 Recent 56634807 Favorites
54804251 reg57.txt 56634833 scripts

The “-i” flag will display the file’s inode:
54804119 -badfile.txt

The inode for the “bad” file is 54804119. Once the inode is identified, use the find command to rename the file:

$ find . -inum 54804119 -exec mv {} NewName \;

$ ls NewName
NewName

Now you can delete it.

$ rm NewName

Tags: cli, rename, find, inode, motd