Search:  
Gentoo Wiki

TIP_Dealing_with_files_that_start_with_a_dash

This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

Introduction

Files starting with a dash (-) can be a real problem in the console. They can be difficult to manipulate, and can even cause problems when operating in the same directory as the dash file.

Problem

The problem is that if you have a file like -test in a directory, and you try to do something in that directory, like list all files and directories, you'll only get a strange error message:

Code: ls *
ls: invalid option -- e
Try `ls --help' for more information.

Try it for yourself. Do

echo ' ' > -test

to get a file called -test. Now try to remove it again!

Code: rm -test
rm: invalid option -- t
Try `rm --help' for more information.

Great.

As you may have noticed, the problem is that if you give this filename as an argument for a command like rm, rm will try to interpret it as an option (because it starts with a dash).

Solutions

One possibility is to use '--' to terminate the option list of any command you want to use on this file:

$ rm -- -test

Most commands understand this option.

An alternative way to deal with these files is to put a path in front of them. To create and remove a file named -test in the current directory, do the following:

$ touch ./-test
$ rm ./-test
Retrieved from "http://www.gentoo-wiki.info/TIP_Dealing_with_files_that_start_with_a_dash"

Last modified: Fri, 05 Sep 2008 23:15:00 +0000 Hits: 12,521