14.06.2014 Views

Unix Power Tools

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

14.15<br />

If you can move all the other files out of the directory, then you’ll probably be<br />

able to remove the leftover file and directory with rm –rf (14.16, 14.10). Moving files<br />

and removing the directory is a bad idea, though, if this is an important system<br />

directory like /bin. Otherwise, if you use the escaped name ls –b gave you, you<br />

might be able to remove it directly by using the system call unlink(2) in Perl. Use<br />

the same escape characters in Perl that ls –b displayed. (Or, if you needed to use<br />

od –c, find the filename in the od listing of the directory—it will probably end<br />

with a series of NUL characters, like \0 \0 \0.)<br />

—JP<br />

perl -e 'unlink("\t\360\207\005\254");'<br />

14.15 Removing a Strange File<br />

by its i-number<br />

If wildcards don’t work (14.12) to remove a file with a strange name, try getting<br />

the file’s i-number (14.2). Then use find’s –inum operator (9.9) to remove the file.<br />

Here’s a directory with a weird filename. ls (with its default –q option (8.12) on<br />

most versions) shows that the name has three unusual characters. Running ls –i<br />

shows each file’s i-number. The strange file has i-number 6239. Give the i-number<br />

to find, and the file is gone:<br />

% ls<br />

adir afile b???file bfile cfile dfile<br />

% ls -i<br />

6253 adir 6239 b???file 6249 cfile<br />

9291 afile 6248 bfile 9245 dfile<br />

% find . -inum 6239 -exec rm {} \;<br />

% ls<br />

adir afile bfile cfile dfile<br />

Instead of deleting the file, I also could have renamed it to newname with the<br />

command:<br />

% find . -inum 6239 -exec mv {} newname \;<br />

If the current directory has large subdirectories, you’ll probably want to keep<br />

find from recursing down into them by using the –maxdepth 1 operator. (finds<br />

that don’t support –maxdepth can use –prune (9.25) for speed.)<br />

—JP<br />

14.16 Problems Deleting Directories<br />

What if you want to get rid of a directory? The standard—and safest—way to<br />

do this is to use the <strong>Unix</strong> rmdir “remove directory” utility:<br />

% rmdir files<br />

272 Part III: Working with Files and Directories<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!