Find the answer to your Linux question:
Results 1 to 6 of 6
How can I specify filenames starting with a dash "-" on the command line? Most apps like cp and touch interpret anything starting with a dash as an option....
  1. #1
    Just Joined!
    Join Date
    Apr 2006
    Posts
    97

    Command line: filenames starting with -

    How can I specify filenames starting with a dash "-" on the command line? Most apps like cp and touch interpret anything starting with a dash as an option.

  2. #2
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    Have you tried putting quotes around the filename?
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  3. #3
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Most commands allow you to explicitly end the options with --. So to copy --foo to bar:
    Code:
    cp -- --foo bar
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  4. #4
    Just Joined!
    Join Date
    Apr 2006
    Posts
    97
    Using quotes doesn't work, because Bash and most other shells I guess remove the quotes when the arg is passed. I even tried:

    Code:
    touch "\"-a\""
    But then the filename actually has quotes around it.

    Using the double dash did the trick:
    Code:
    touch -- -a
    I guess we just have to hope and pray that enough programs use the double-dash terminator. Thanks for the help.

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    And one very old method ... cheers, drl
    Code:
    #!/bin/sh
    
    # @(#) s1       Demonstrate one method to delete leading-dash filenames.
    
    echo
    echo " Creating file with leading dash."
    echo hi > -t1
    
    echo " Files currently extant:"
    ls
    
    echo
    echo " Attempt to remove -t1:"
    rm -t1
    
    echo
    echo " Remove by prefixing with ./"
    rm ./-t1
    
    echo " Files now here:"
    ls
    Resulting in:
    Code:
    % ./s1
    
     Creating file with leading dash.
     Files currently extant:
    -t1  s1
    
     Attempt to remove -t1:
    rm: invalid option -- t
    Try `rm --help' for more information.
    
     Remove by prefixing with ./
     Files now here:
    s1
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Just Joined!
    Join Date
    Apr 2006
    Posts
    97
    Ah, that method is good! I can just prefix with ./, easy. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...