Everything2
Near Matches
Ignore Exact
Full Text
Everything2

How to remove oddly named files on Unix systems

created by drinkypoo

(idea) by drinkypoo (1 wk) (print)   ?   I like it! Fri Sep 13 2002 at 17:28:50

Every so often you will find a file on a Unix system which has a very bizarre filename, consisting of one or more untypeable characters. Sometimes you can simply replace those characters with the wildcard ? which matches any one character, or replace a consecutive group of them with the * which matches zero or more of any character, but there is in fact a more reliable way to remove a single file.

I will provide examples as typed in cygwin which, for the purposes of this explanation, behaves as Unix would. (This is not true in all cases.) My example file also has a quite straightforward name, which does not invalidate the example at all.

First, do a ls -i in the directory. This will show you inode numbers for all files listed. The file we are interested in is called 'foobar'. Pretend it's got a name which is much harder (perhaps impossible) to type.

$ ls -i
 258452 TeXmacs-fonts-1.0/  62444571 foobar   191011 texmacs/

Now we know the inode number, 62444571. You can remove (or do something else to) files by inode using the find command. Find has a flag called -exec which will execute a command against all files found. For this purpose is it used thusly:

$ find . -inum 62444571 -exec rm '{}' \;

What does this mean? We can find out by looking at the synopsis in the manpage for find:

SYNOPSIS
       find [path...] [expression]

Our path is ".", or the current directory. Using the -inum flag in our expression means "find files with this inode number". Our expression uses the -exec flag to execute what comes after it. The token {} is replaced with the escaped name of the file which we have found, and the ; token ends the exec statement.

Note that we have enclosed the {} token in ticks, or single quotes. This is because the { and } symbols have special meaning to the shell. Specifically, in the bourne shell and its derivatives they are used to group commands or functions into a single function. We have also escaped the semicolon using the backslash, because semicolon is used to separate commands or functions which are on the same line as if they were on separate lines. The two are frequently used together.

Having deleted our file (ostensibly) we do an ls in the directory to list its contents and ensure that it has gone away.

$ ls
TeXmacs-fonts-1.0/  texmacs/

The file is gone! Had this been a real emergency, it would be over now.

An alternate method of removing these files is to use rm -i which asks you interactively which files you would like to delete. Hence in a directory with a small number of files it is possible to say "rm -i *" and rm will ask you which files you would like to remove. In a directory with a larger number of files, it is best to use the above method, which involves a more complicated command line, but less hitting of y or n followed by return.


References:

  1. Manual Page: find(1) from Cygwin.
  2. Chatterbox Message from mkb suggesting rm -i


printable version
chaos

How to Drink Whisky rm -rf / Using windows, temporarily How to remove "tagging" information from NT-based FTP sites
Cygwin Why the college fraternity and sorority system is ridiculous It is now kistomary to cuss the bride How to improve your chances of having sex
The Anti-Mac User Interface World famous in New Zealand Yes, dammit I am sure I want to delete it. While you're at it, empty the trash, too! 99 Cent Coin
The Original GNU Announcement glamourbombing win.ini Veritas
escape filename Wildcard Management
Ali linker autorun Linux is free only if your time has no value
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
What you are reading:
Coase Theorem
The Devil Went Down to Georgia
The Spanish Armada
I bottled out of the e2 picnic fearing for my life!
Overcoming arachnophobia, or how I learned to love the spiders with HUMAN HEADS!
Mensa
Kurt Vonnegut, Jr.
Why it seems you get good ideas when you're stoned
The fall and rise of online community
Schumann resonance
Dracaena
The Pig War
Somewhere there's a god who wants me
New Writeups
Ouzo
Goodwill Hunting, Thrift Store(ies)(log)
Pandeism Fish
How conatus compels divine ketosis through a radical kenosis(essay)
cryforhelp
Major dictionaries of the world(review)
Glowing Fish
The Uncanny X-Men and the New Teen Titans(thing)
WolfKeeper
Launch loop(idea)
TendoKing
Katana(person)
Wuukiee
Highly ornamental cultivars of brambles still have as many thorns as their wild counterparts(idea)
TheDeadGuy
Editor Log: May 2008(log)
everyday j.Lo
pray do not molest them(thing)
ammie
Bands Who Take Their Names from Eighteenth-century English Poetry and Prose(idea)
shaogo
Under My Thumb(review)
ammie
Rock On(person)
The Custodian
The Dresden Files(thing)
Ouzo
PETA becomes you, a proposed future(fiction)
Ereneta
Stone Soup, Part Two(fiction)
E2 is a by-product of the existence of The Everything Development Company