brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: find -inum deletes a file whose name you can't type

Find
~/tips/tip-find-inum-weird-filename

ls -i

Removes a file with a strange name by its inode number instead of its name.

Sometimes a file ends up with a name that the shell fights you on: a leading dash, invisible characters, a broken encoding. Every rm attempt fails. You can skip the name and use the inode number.

First get the number with ls -i, which prints it in front of each name:

ls -i
422637 -weird name

Then let find look up that number in the current folder and delete it:

find . -maxdepth 1 -inum 422637 -delete

It works fine on a file called “-weird name”. Run it with -print instead of -delete first, to see that it picks the file you expect.

Inode numbers are only unique within one filesystem. That’s why -maxdepth 1 is there: a search that crosses into a mounted disk could find a completely different file with the same number. Also, if the file has hard links, they share the inode, and find will delete all of them.

For the leading dash alone there is a simpler fix: rm -- '-weird name' or rm ./-weird\ name. I’d keep the inode trick for names you really can’t type.

Further reading: find(1) manual page, ls(1) manual page

More Find tips find / -maxdepth 1 -fstype proc find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null find /srv/downloads -type f -size +100M -size -1G -exec ls -lh {} + find /srv/data -depth -print find /srv -xdev -samefile /srv/downloads/movie.mkv All Find tips (46) All topics

Link