Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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).
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=...
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
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
$
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.