LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-14-2006, 10:50 AM   #1
sancho1980
Member
 
Registered: May 2006
Location: Leipzig, Germany
Distribution: Kanotix 64
Posts: 45

Rep: Reputation: 15
Question ls only files starting with upper case character


does anyone know how to use ls so that it only returns those files which start w/ an uppercase character?
thanx,
martin
 
Old 05-14-2006, 11:11 AM   #2
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian
Posts: 2,159

Rep: Reputation: 334Reputation: 334Reputation: 334Reputation: 334
Code:
ls | grep "^[A-Z]"
 
Old 05-14-2006, 11:22 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
To display only files and directories that begin with an uppercase character:
Code:
ls |grep '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]
 
Old 05-14-2006, 11:24 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
Sorry, forgot to close the quote.
 
Old 05-14-2006, 11:34 AM   #5
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
There's no need to use grep, the shell can handle this simple pattern matching:

Code:
ls [A-Z]*
That will dispaly only uppercase ASCII characters of course.

Code:
ls [[:upper:]]*
With this, what qualifies as an "uppercase" character will be locale dependent .

However, if you have a _lot_ of files, this could result in a 'command line too long' error, in which case you can use the grep suggestion above (or find, etc).

Also note that you'll get an error if there are no matching files, unless you set the nullglob shell option.

Last edited by ioerror; 05-14-2006 at 11:38 AM.
 
Old 05-14-2006, 01:13 PM   #6
sancho1980
Member
 
Registered: May 2006
Location: Leipzig, Germany
Distribution: Kanotix 64
Posts: 45

Original Poster
Rep: Reputation: 15
thanx for all ur replies
 
Old 05-14-2006, 05:02 PM   #7
sancho1980
Member
 
Registered: May 2006
Location: Leipzig, Germany
Distribution: Kanotix 64
Posts: 45

Original Poster
Rep: Reputation: 15
[:upper:]

By the way, what is upper? Is that a system variable? And why the two colons?
 
Old 05-14-2006, 06:06 PM   #8
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
It is a regular expression pattern called a "character class", and has the form '[:class:]'. It is always specified inside the character matching metacharacters [].

There are a number of "classes", alpha (any alphabetic character in the current locale), upper, lower, digit and so on. You can find the full list of classes in the bash man page (searching for 'alnum' will take you straight there) or any text on regular expressions (the grep man page has a short introduction to regex).

Last edited by ioerror; 05-14-2006 at 06:10 PM.
 
Old 05-14-2006, 06:32 PM   #9
sancho1980
Member
 
Registered: May 2006
Location: Leipzig, Germany
Distribution: Kanotix 64
Posts: 45

Original Poster
Rep: Reputation: 15
ah, thanx
i was playing around a little with the bash pipe functionality and now i'm starting to wonder:

if

ls | grep ^[[:upper:]]

gives me all files starting with an uppercase character then why does the following NOT work:

ls | grep ^[[:upper:]] | cp /directory

..meaning to copy all these files into /directory

martin
 
Old 05-14-2006, 07:52 PM   #10
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,544
Blog Entries: 4

Rep: Reputation: 57
You could use find to do the whole thing:
Code:
find . -maxdepth 1 -type f -name '[[:upper:]]*' -exec cp {} /directory \;
 
Old 05-15-2006, 04:23 AM   #11
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
It doesn't work because cp doesn't read from stdin by default, so there a special option to indicate that it should do so, namely --target-directory=...
 
Old 05-15-2006, 06:26 AM   #12
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
command1 | command 2 uses standard output of command1 as standard input for command2. Arguments are different from standard input. grep parses the standard input and put the result in the standard output. cp doesn't read standard input by default, but uses arguments instead.
Anway, the simplest way is :

cp [[:upper:]]* /directory

or, if the argument list is too long,

for filelist in $(ls | grep ^[[:upper:]])
do
cp $filelist /directory
done
 
Old 05-15-2006, 10:30 AM   #13
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian
Posts: 2,159

Rep: Reputation: 334Reputation: 334Reputation: 334Reputation: 334
Quote:
Originally Posted by ioerror
Code:
ls [A-Z]*
One would think this would work, but it doesn't. At least not on Debian Sarge and Sid. I would call this a bug myself. Funny thing, it doesn't work, but it doesn't work in a different manner than I can explain (see it filter aa below, but not bb or cc!!!)
Code:
$ mkdir tmp
$ cd tmp
$ >aa
$ >Aa
$ >bb
$ >Bb
$ >cc
$ >Cc
$ ls [A-Z]*
Aa  bb  Bb  cc  Cc
$ ls
aa  Aa  bb  Bb  cc  Cc
$
Perplexed, I tried this same test on Debian Sid. Same results. Next test, ... on to Solaris.

Solaris got it right:
Code:
$ mkdir tmp
$ cd tmp
$ >aa
$ >Aa
$ >bb
$ >Bb
$ >cc
$ >Cc
$ ls [A-Z]*
Aa  Bb  Cc
$ ls
Aa  Bb  Cc  aa  bb  cc
$

Last edited by haertig; 05-15-2006 at 10:37 AM.
 
Old 05-15-2006, 11:04 AM   #14
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Quote:
Originally Posted by haertig
One would think this would work, but it doesn't. At least not on Debian Sarge and Sid. I would call this a bug myself. Funny thing, it doesn't work, but it doesn't work in a different manner than I can explain (see it filter aa below, but not bb or cc!!!)
Code:
$ mkdir tmp
$ cd tmp
$ >aa
$ >Aa
$ >bb
$ >Bb
$ >cc
$ >Cc
$ ls [A-Z]*
Aa  bb  Bb  cc  Cc
$ ls
aa  Aa  bb  Bb  cc  Cc
$
Perplexed, I tried this same test on Debian Sid. Same results. Next test, ... on to Solaris.

Solaris got it right:
Code:
$ mkdir tmp
$ cd tmp
$ >aa
$ >Aa
$ >bb
$ >Bb
$ >cc
$ >Cc
$ ls [A-Z]*
Aa  Bb  Cc
$ ls
Aa  Bb  Cc  aa  bb  cc
$
Are you sure you don't have an ls alias messing that up?
 
Old 05-15-2006, 11:15 AM   #15
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian
Posts: 2,159

Rep: Reputation: 334Reputation: 334Reputation: 334Reputation: 334
Quote:
Originally Posted by Agrouf
Are you sure you don't have an ls alias messing that up?
I don't believe so. I am old school and don't like the fancy colors ls can do, so I had commented out all aliases in .bashrc (originally it had alias ls='ls --color=auto', but I wiped that out long ago). Calling /bin/ls directly does same thing:
Code:
$ /bin/ls [A-Z]*
Aa  bb  Bb  cc  Cc
$
Good thought though. After you said this, I went and verified that I didn't have any aliases getting in the way. Just to be sure.
 
  


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
upper case letter moonz Linux - General 3 09-18-2005 06:58 AM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 04:09 PM
Lower case to upper case letter sudhasmyle Programming 1 12-03-2004 04:15 AM
K3B problems upper case Veteq Linux - General 2 03-05-2004 11:42 PM
Apache and upper or lower case. dsiguy Linux - General 3 02-04-2003 11:02 AM


All times are GMT -5. The time now is 02:24 PM.

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