A small trick to handle files prefixed with a specify character in BASH

From time to time, we might see some files on Linux which have strange leading characters. For example, a leading “-” dash or a leading “+” plus sign. Depending on which operations (which commands) you take, you may have different ways to handle those files — putting file name in a single or double quotes, escaping the special character with “\” etc so that commands can handle them properly — not treated as an option, but files to be processed.

[grid@eiagc1oda001 trace]$ ls -l +APX1_ora_1557.trc
-rw-r----- 1 grid oinstall 939 May  8 10:37 +APX1_ora_1557.trc
[grid@eiagc1oda001 trace]$ less +APX1_ora_1557.trc
Missing filename ("less --help" for help)

In BASH, there is actually an official way to disable further option processing. “man bash” will show the usage of “–“

— A — signals the end of options and disables further option processing. Any arguments after the — are treated as filenames and arguments. An argument of – is equivalent to –.

[grid@joeoda001 trace]$ ls -lrt -- *abc*
-rw-r--r-- 1 grid oinstall 0 May 11 15:25 -abc_test
[grid@joeoda001 trace]$ rm -- -abc_test

[grid@joeoda001 trace]$ less -- +APX1_ora_1557.trc

Examples above show files starting with “-” or “+” operated by “ls” and “less”.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s