LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-07-2015, 11:27 AM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Rep: Reputation: Disabled
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.
 
Old 11-07-2015, 12:14 PM   #2
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,176

Rep: Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837
rename the directory first. simple.
if you can't from the shell, you have to do it through php.
 
Old 11-07-2015, 12:20 PM   #3
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-07-2015, 12:36 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
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.
 
Old 11-07-2015, 12:49 PM   #5
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-07-2015, 01:28 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
I usually precede the "unlistable" with a simple ./

Thus "ls ./-2" will work.
 
Old 11-07-2015, 01:47 PM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Quote:
Originally Posted by NotionCommotion View Post
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.
 
Old 11-07-2015, 05:40 PM   #8
Doug G
Member
 
Registered: Jul 2013
Posts: 596

Rep: Reputation: Disabled
mc usually can rename unusual file/directory names easily.
 
Old 11-07-2015, 06:07 PM   #9
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] The difference a minus sign makes phillyfilly Linux - Newbie 7 02-10-2014 06:35 AM
Minus sign in 'if' statement unclear keif Programming 3 10-18-2013 04:20 PM
[SOLVED] CTRL + Minus sign drManhattan SUSE / openSUSE 6 08-16-2011 10:20 AM
cd to a directory name - (minus sign) anjanesh Linux - General 4 12-29-2008 04:38 AM
Removing a directory beginning with a minus sign. harry edwards Linux - General 4 03-01-2008 06:44 PM


All times are GMT -5. The time now is 08:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration