Americas

  • United States
sandra_henrystocker
Unix Dweeb

Unix tip: Cleaning up your files

How-To
Dec 13, 20163 mins
Data CenterLinux

Here are some ways to deal with troublesome files on your Unix or Linux system.

Oops! Wrong location!

If you accidentally extract files from a tar file into the wrong location and want to reverse the process, here’s a fairly easy way to do that.

Say you just extracted some files meant for /var/app/bin into /bin instead. Oops! You can spend a lot of time looking through a listing of the archive you just extracted from or you can remove the files with a command that contains a variation of the extract command. If, for example, you used the command tar xvf bin.tar.gz to extract the contents, then you could use this command to remove the files.

$ rm -f `tar ztf bin.tar.gz`

What does this command do? The backticked section creates a list of all the files in the archive and rm -f command then removes them.

The command won’t replace files that might have been overwirtten by same-named files on the archive you extracted from (unlikely to have happened) and it won’t remove files that just happen to have blanks in their names as the remove command will assume a file named “my favorite file” is actually three separate files.

Clearing out files that are open

Removing files that are rapidly consuming disk space but still open by some executable can at times be critical. The command shown below removes the contents by essentially cat’ing /dev/null to the file — even if the file is still in use and growing.

> filename

Removing files that start with a hyphen

Removing files with odd characters in their names can be troublesome. Filenames that begin with a hyphen confuse the shell because it assumes that the name is really a list of options. The easiest way to get past this problem is by adding a double hyphen after rm as shown in the command below.

rm -- -myfile

Removing or renaming files that contain odd characters

Removing files with odd characters in their names is also fairly easy. You can put the name of the file in quotes.

$ ls my*
myfile  my file  my-file  my*file  myfile.why
$ rm "my*file"
$ ls my*
myfile  my file  my-file  myfile.why

You can use a backslash character to keep the shell from translating the special character.

$ ls my*
myfile  my file  my-file  my*file  myfile.why
$ rm my*file
$ ls my*
myfile  my file  my-file  myfile.why

If you have a file with a blank in its name, either of these commands will work as well.

rm "my file"
rm my file

Removing files by inode number

You can also remove files by using their inode number. You just have to first use the ls -i to get the inode num and then use this information in a find command that also removes the file.

$ ls -i my*
413215 myfile  413234 my-file  413538 my*file  261319 myfile.why
$ find . -inum 413538 -delete
$ ls -i my*
413215 myfile  413234 my-file  261319 myfile.why
sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.