LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using ls on a directory prefaced by a minus sign (https://www.linuxquestions.org/questions/linux-newbie-8/using-ls-on-a-directory-prefaced-by-a-minus-sign-4175558251/)

NotionCommotion 11-07-2015 10:27 AM

Using ls on a directory prefaced by a minus sign
 
I would like to view all files located in a given directory using something like:
Code:

ls myDir -LRa | more
Problem is the directory name is "-2", and I can't seem to use ls at all.

How can I accomplish this? Thank you

Code:

-sh-4.1$ ls "-2"
ls: invalid option -- '2'
Try `ls --help' for more information.
-sh-4.1$ ls -2
ls: invalid option -- '2'
Try `ls --help' for more information.
-sh-4.1$ ls '-2' -LRa | more
ls: invalid option -- '2'
Try `ls --help' for more information.
-sh-4.1$

PS. It was not my intention to use such a silly directory name, and they were created by some faulty PHP code.

ondoho 11-07-2015 11:14 AM

rename the directory first. simple.
if you can't from the shell, you have to do it through php.

NotionCommotion 11-07-2015 11:20 AM

Thanks ondoho,

Can't use mv either. But I can if I use full file path, and for that matter, could use ls as well.

All good. Thanks

TobiSGD 11-07-2015 11:36 AM

Try it with
Code:

ls -LRa -- myDir | more
The double dash (--) tells the ls program that what follows behind them should not be parsed as an option. This works also for programs like mv, cp, ... , in case you want to rename or copy the file.

NotionCommotion 11-07-2015 11:49 AM

Thanks TobiSGD,

I already fond that the following would work, and after searching the directories, deleted them.

Code:

ls '/bla/bla/bla/-2' -LRa | more
But would rather do it the way you show should it happen again. Am I correct that the following would work?

Code:

ls -LRa -- -2 | more

jpollard 11-07-2015 12:28 PM

I usually precede the "unlistable" with a simple ./

Thus "ls ./-2" will work.

TobiSGD 11-07-2015 12:47 PM

Quote:

Originally Posted by NotionCommotion (Post 5446079)
Am I correct that the following would work?

Code:

ls -LRa -- -2 | more

Code:

[tobi@demon test]$ touch -- -2
[tobi@demon test]$ ls -LRa -- -2
-2
[tobi@demon test]$ rm -- -2
[tobi@demon test]$ ls
[tobi@demon test]$

Yes, it works.

Doug G 11-07-2015 04:40 PM

mc usually can rename unusual file/directory names easily.

NotionCommotion 11-07-2015 05:07 PM

Thanks jpollard, Yes your approach is simpler.

Thanks TobiSGD, I really should be less afraid of testing!

Thanks Doug, Never used Midnight Commander and don't have it currently installed, but maybe I will explore.


All times are GMT -5. The time now is 10:12 AM.