How to escape files containing special characters like -, or spaces

3

The contents of a directory:

$ ls -l
total 122639
-rw-r--r-- 1 125578080 Aug 20 17:47 - Diana Krall, Stan Getz & Oscar Peterson.7z
drwxr-xr-x 4      4096 Aug 20 18:02 Java

I wish to use the file command in this directory, where a lot of various files are copied

$ file *

The result I expect:

$ file *
- Diana Krall, Stan Getz & Oscar Peterson.7z: 7-zip archive data, version 0.3
java: directory

The result I get:

$ file *
file: illegal option --
file: illegal option -- D
file: illegal option -- a
file: illegal option -- a
file: illegal option --
file: illegal option -- K
file: illegal option -- a
file: illegal option -- l
file: illegal option -- l
file: illegal option -- ,
file: illegal option --
file: illegal option -- S
file: illegal option -- t
file: illegal option -- a
file: illegal option --
file: illegal option -- G
Usage: file [-bchikLNnprsvz0] [--apple] [--mime-encoding] [--mime-type]
            [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ...
       file -C [-m magicfiles]
       file [--help]

I can spell out the filenames and escape things manually, but I want to use *:

$ file -- -\ Diana\ Krall\,\ Stan\ Getz\ \&\ Oscar\ Peterson.7z
- Diana Krall, Stan Getz & Oscar Peterson.7z: 7-zip archive data, version 0.3

$ file java
java: directory

How can I escape or protect * ?

Aubin

Posted 2013-08-20T18:10:20.883

Reputation: 184

Answers

5

Use just the following

file ./*

All is needed.

Valentin Bajrami

Posted 2013-08-20T18:10:20.883

Reputation: 168

1Simple, as expected ;-) – Aubin – 2013-08-20T18:59:16.200

4

You can tell file (and most other Unix command line tools) to stop looking for option switches by passing them -- as an argument. Thus, the following will work:

file -- *

(Of course, val0x00ff's suggestion of file ./* will work too, but will cause the ./ to appear prepended to the file names in the output.)

Ilmari Karonen

Posted 2013-08-20T18:10:20.883

Reputation: 1 509

1

Simple:

file -- *

-- Means end of options of command. Because file command think - in file name is part of your option.

Sepahrad Salour

Posted 2013-08-20T18:10:20.883

Reputation: 928