1

I am new and try to learning Terminal Command. Today I faced some problem with cp wildcard command option. I wrote in Terminal this command

cp Downloads/[:alnum:]  test/

Here, Downloads is the directory from where I want to copy alphanumeric character file to my test directory. But When I ran this command showed me:

cannot stat ‘Downloads/[:alnum:]’: No such file or directory

I want to know, how I can use above Wildcard Classes for cp command. And Also how I can use above Wildcard Character command.

Wildcards are

 * 
? 
[characters]
[!characters]
[[:classes:]]

Commonly used classes are

[:alnum:]-> Match any Alphanumeric Character
[:alpha:] -> Match any Alphabetic Character
[:digit:] -> Match any nummeral 
[:lower:] -> Matches any lowercase letter
[:upper:] -> Matches any Uppercase letter

How can I use those Wildcard and its Classes?

2 Answers 2

3

cp does not support any wildcards by itself. Expansion of wildcards and patterns is done by the shell, which is bash by default.

In any case, character classes are to be specified within [] - that excludes the [] already surrounding them. Like so:

cp Downloads/[[:alnum:]] test/

However, [[:alnum:]] will only match a single alphanumeric character. To match all filenames containing only alphanumeric characters can't be done with just the wildcards already listed. For more powerful filtering, look into the find command:

find . -regextype posix-extended -maxdepth 1 -regex '.*/[[:alnum:]]*'

Within bash, you can use extended globs:

shopt -s extglob
cp Downloads/*([[:alnum:]]) test/
1
  • 1
    Don't forget to mention that one can use -exec flag to call cp per each file Feb 8, 2016 at 19:52
2

[:alnum:] actually matches a single character from the set :, a, l, n, u, m i.e. given

$ ls
_?_  123  7  a  abc  :file  fi:le  m

then

$ ls [:alnum:]
a  m

To match a single character from the alphanumeric class [:alnum:] it needs to be [[:alnum:]]

$ ls [[:alnum:]]
7  a  m

To match a single alphanumeric character followed by zero or more arbitrary characters

$ ls [[:alnum:]]*
123  7  a  abc  fi:le  m

To match file consisting only of alphanumeric characters using shell globbing requires extended glob (extglob) syntax

$ ls +([[:alnum:]])
123  7  a  abc  m

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .