1

I'm following this guide to get some basic skills in Linux.

At the exercises of chapter 3 section, there are two exercises:

*Change to your home directory. Create a new directory and copy all the files of the /etc directory into it. Make sure that you also copy the files and directories which are in the subdirectories of /etc! (recursive copy)

*Change into the new directory and make a directory for files starting with an upper case character and one for files starting with a lower case character. Move all the files to the appropriate directories. Use as few commands as possible.

The first part was simple but I have encountered problems in the second part (although I thought it should be simple as well).

I did the first part successfully - that is, I have a copy of the /etc folder in ~/newetc - with all the files copied recursively into subdirectories.

I've created ~/newetc/upper and ~/newetc/lower directories.

My intention was to do something like mv 'find ... ' ./upper for example.

But first I thought I should make sure that I can find all the files with Upper/Lower case seperately. At this I failed.

I thought that find ~/newetc [A-Z].* (also tried: find ~/newetc -name [A-Z].*) to find all the upper case files - but it simply returns no results.

What's even stranger: find ~/newetc -name [a-z].*) returns only two files, although of course there are a lot more then that... any idea what am I doing wrong?

Thank you for your time!

Edit: (I have tried to read the Man for find command btw, but didn't come up with anything)

1
  • Read harder the documentation, everything is there. Maybe info find would be better than man find Commented Sep 5, 2011 at 8:07

6 Answers 6

4

The -name argument does not take a full regular expression by default. So [A-Z].* will match only if the second character is a dot.

Use the expression [A-Z]*, or use -regex and -regextype to match using a real regex.

Sign up to request clarification or add additional context in comments.

2 Comments

wouldn't [A-Z]* will return only words that are with 0 or more capital letters only? Also: -regex doesn't give any results not for lower nor for upper case pattern ([a-z].* nor [A-Z].*) and -regextype throws an error.
@Lost_DM: -name wants a shell globbing pattern, not a regex. The * in a globbing pattern is pretty much the same .* in a regex (probably because . shows up in file names a lot and no one wants to escape something so common).
2

You need to use quotes

find ~/new_etc -name "[A-Z]*"
find ~/new_etc -name "[a-z]*"

Comments

0

If you want to use regexp, then you must use -regex (or -iregex).

Comments

0

For finding stuff, the other answers tell you how to do it.

For moving the results of find, use the -exec flag (while being in newetc):

find -name "[A-Z]*" -exec mv {} upper/{} \;
find -name "[a-z]*" -exec mv {} lower/{} \;

Comments

0

The -name parameter takes a glob, not a regular expression (those are both very useful pages). So the dot does not have a special meaning for this parameter - It is interpreted as a literal dot character. Also, in a regular expression the * means "0 or more of the previous expression" while in a glob it means "any number of any character." So, as others have pointed out, the following should get you any files below the current directory which start with an uppercase character:

find . -name '[A-Z]*'

Comments

0

If you want to find all the name beginning with a capital letter you have to use

find . -name "[A-Z]*"

NOT

find [A-Z].*

otherwise yo will try to locate all the file that begin with a capital letter and have a . just after

5 Comments

@i0b0 please... before downvoting type the command in your shell... it actually works and i just tested it if u do not belive it i paste you the result.
here it is: "find [A-Z]*" returns LockDebug.log which is in the local folder! please reserve downvoting for when is needed!
I'm on Ubuntu, and it returns every single filename below the current directory. Did you try it in a directory with more files?
I used a distribution made by cern. and it works. however if it does not work under ubuntu i add a -name in my answer then... just to make it more precise. thanks than!
SLC4, which was the official CERN distro last I heard, is ancient, and SLC5, the latest, is several years old (mainly because of the certification process). I'm afraid it's not representative of software in general use today, and even most CERNies use some other distro when they can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.