Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-01-2011, 11:27 AM   #1
LQ Newbie
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
bash wildcard expansion length issue

Hi guys,

So I've got a directory with quite a few thousand files (+27000) in it. They aren't named anything stupid; only numbers and _s.

I'm using the bash shell.

When I try and manipulate the files, starting with a *, I get an error:

ls *.gif
ls: invalid option -- _
Try `ls --help' for more information.

I can use the wildcard in the middle of my query, just not at the start.

Same thing happens for cp, mv etc

I can get round it by:
ls -- *.gif

However, my question is: why on earth does this happen? I first thought maybe there was a character limit for the expansion... but I didn't see how the -- would help me.

Obviously google isn't particularly helpful when searching for '--'

Anyone have any ideas?
Old 11-01-2011, 11:40 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe there are files starting with an hyphen, so that the commands interpret these file names as options. The -- (double hyphen) in many linux commands serves to terminate the list of options, so that everything that follows is treated as a real argument (positional parameter). It is the right way to go, if you want to manage these files from the command line.
Old 11-01-2011, 12:32 PM   #3
LQ Newbie
Registered: Nov 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
You're right! I checked for that by

ls | less
ls | tail -10

thinking that '-' would either be sorted before numbers or after numbers - it turns out ls ignores the leading '-' and sorts by the next character.

I thought I ruled that out!
Thanks for the explanation


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
Wildcard/regex in bash command logicalfuzz Linux - General 2 10-27-2011 02:40 PM
SED: wildcard expansion in variables Gracie Belle Linux - Newbie 3 02-20-2008 07:43 AM
Variable expansion in BASH champak Programming 5 11-26-2007 02:44 AM
Bash variables expansion olaola Linux - Newbie 4 10-16-2006 11:45 AM
bash in ubuntu: save the wildcard expansion beebop Programming 0 03-12-2006 01:19 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:44 PM.

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