How can I read a dash file from the terminal other than delimiting it with ./
For example to read a - file
we can read it by
cat ./-file_name
Q: Is there an alternative way to achieve the same thing?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityUse double --
to mark end of options:
cat -- -<FILENAME>
Other programs such as touch
, rm
or git checkout
also follow this convention:
$ touch -- -file
$ ll
total 0
-rw-r--r-- 1 ja ja 0 Mar 10 13:13 -file
$ echo hi! >> -file
$ cat -- -file
hi!
$ rm -- -file
$ echo $?
0
WARNING: It's good practice to always use --
after rm
in scripts. An attacker could place --rf
file in a directory and rm *
would take it as run parameters. See this:
$ touch A
$ touch B
$ mkdir dir
$ touch dir/C
$ touch -- -rf
$ rm *
$ ll
total 0
-rw-r--r-- 1 ja ja 0 Mar 10 13:21 -rf
Oops, this is not we meant, we didn't want to remove directories. We should have used --
:
$ touch A
$ touch B
$ mkdir dir
$ touch dir/C
$ touch -- -rf
$ rm -- *
rm: cannot remove `dir': Is a directory
$ ll
total 4.0K
drwxr-xr-x 2 ja ja 4.0K Mar 10 13:22 dir
--
: many do but not all.
Mar 10, 2015 at 13:44
--
, I said showed some that do.
Mar 10, 2015 at 13:47
find . -name '-file_name' -exec cat {} \;
but there really isn't much point to it.
-
and nothing else? This came up in the wargame: overthewire.org/wargames/bandit/bandit2.html
May 12, 2017 at 17:29
For commands which get input from stdin, you can use redirection:
cat <-file_name
./
prefix. It's the most portable (cf the usenet era's Unix FAQ)find . -name "-filename" | xargs cat
will send ./-filename through the pipe. For your (simplified?) case a mistake, but it might help in large directories / scripts.