Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I seriously can't understand where I'm going wrong with his question. The first part is.

List all the file and directory names in /etc which have 1 or more numbers in their name. Remember to use "-d" in the ls command, and do not descend into subdirectories (i.e. no recursion). Save this list in caine's home directory as a file called "digit". Do the "ls" command when CDed to /etc, thus your names avoid having "/etc/" stuck in front of them.

Which is have done with the following command:

ls -d *[0-9]* > digit

However, the second part of the question is;

Redo the previous question, except this time repeat the exercise when CDed in /home/caine. Save the output in digit2. "digit" and "digit2" should be identical, except in "digit2" all the filenames will begin "/etc/".

I thought the following code would work;

ls -d ../../etc/ *[0-9]* > digit2

When I cat the contents of the file, it appears to have ignored the parameters I have set using the *'s and numbers, I have tried moving the statement around and I am stumped as to why this isn't working.

Any advice, or pref an explanation as to where I am going wrong would be much appreciated.

I know this could be achieved with grep, but even though I've moved on to that and am learning it, I'd still really like to know how I can't get the right syntax for this!

share|improve this question
add comment

1 Answer

You added an extra space, which split your glob into two separate arguments:

ls -d ../../etc/ *[0-9]* > digit2
#               ^ space

It should be

ls -d /etc/*[0-9]* > digit2

No need for the ../.. unless you actually like seeing all those dots in the file.

share|improve this answer
    
Ah thanks, I put the extra dots in because I am CDed into a different directory and thought it would try and look in the current directory for /etc/ rather than go up two levels. Thanks for pointing out I don't need to space between the input destination and the wildcards –  Sirus Nov 10 '12 at 19:03
2  
@Sirus Paths that start with / are absolute; it doesn't matter what your current directory is. So ls etc is going to look for etc in your current directory; ls /etc is going to look for the etc in the root –  Michael Mrozek Nov 10 '12 at 19:11
    
Michael, thank you very much for that input, so if I'm deep in a directory somewhere, and I want to do a ls, I can use absolute paths which will start looking from the top (root) of the directory tree? –  Sirus Nov 10 '12 at 19:18
    
@Sirus Yes absolute paths are written with a leading / and by definition start from the root /. –  jw013 Nov 13 '12 at 20:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.