|
INTERVIEW QUESTIONS
OPERATING SYSTEMS
UNIX PROGRAMMING
DETAILS
Question: How do I construct a shell glob-pattern that matches all files except "." and ".." ?
Answer: You'd think this would be easy.
* Matches all files that don't begin with a ".";
.* Matches all files that do begin with a ".", but this includes the special entries "." and "..", which often you don't want;
.[!.]* (Newer shells only; some shells use a "^" instead of the "!"; POSIX shells must accept the "!", but may accept a "^" as well; all portable applications shall not use an unquoted "^" immediately following the "[")
Matches all files that begin with a "." and are followed by a non-"."; unfortunately this will miss "..foo";
.??* Matches files that begin with a "." and which are at least 3 characters long. This neatly avoids "." and "..", but also misses ".a" .
So to match all files except "." and ".." safely you have to use 3 patterns (if you don't have filenames like ".a" you can leave out the first):
.[!.]* .??* *
Alternatively you could employ an external program or two and use backquote substitution. This is pretty good:
`ls -a | sed -e '/^.$/d' -e '/^..$/d'`
(or `ls -A` in some Unix versions)
but even it will mess up on files with newlines, IFS characters or wildcards in their names.
|
|
|
Category |
Unix Programming Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 7108 users |
Added on |
8/26/2014 |
Views |
65595 |
Rate it! |
|
|
Question:
How do I construct a shell glob-pattern that matches all files except "." and ".." ?
Answer:
You'd think this would be easy.
* Matches all files that don't begin with a ".";
.* Matches all files that do begin with a ".", but this includes the special entries "." and "..", which often you don't want;
.[!.]* (Newer shells only; some shells use a "^" instead of the "!"; POSIX shells must accept the "!", but may accept a "^" as well; all portable applications shall not use an unquoted "^" immediately following the "[")
Matches all files that begin with a "." and are followed by a non-"."; unfortunately this will miss "..foo";
.??* Matches files that begin with a "." and which are at least 3 characters long. This neatly avoids "." and "..", but also misses ".a" .
So to match all files except "." and ".." safely you have to use 3 patterns (if you don't have filenames like ".a" you can leave out the first):
.[!.]* .??* *
Alternatively you could employ an external program or two and use backquote substitution. This is pretty good:
`ls -a | sed -e '/^.$/d' -e '/^..$/d'`
(or `ls -A` in some Unix versions)
but even it will mess up on files with newlines, IFS characters or wildcards in their names. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
How do I tell inside .cshrc if I'm a login shell?
|
View Answer
|
|
How do I redirect stdout and stderr separately in csh?
|
View Answer
|
|
How do I {set an environment variable, change directory} inside a program or shell script and have that change affect my current shell?
|
View Answer
|
|
Why do I get [some strange error message] when I "rsh host command" ?
|
View Answer
|
|
How do I rename "*.foo" to "*.bar", or change file names to lowercase?
|
View Answer
|
|
How do I get the current directory into my prompt?
|
View Answer
|
|
How do I get a recursive directory listing?
|
View Answer
|
|
How do I remove a file with funny characters in the filename ?
|
View Answer
|
|
How do I remove a file whose name begins with a "-" ?
|
View Answer
|
|
What happened to the pronunciation list that used to be part of this document?
|
View Answer
|
|
What are some useful Unix or C books?
|
View Answer
|
|
How does the gateway between "comp.unix.questions" and the "info-unix" mailing list work?
|
View Answer
|
|
What does {some strange unix command name} stand for?
|
View Answer
|
|
When someone refers to 'rn(1)' or 'ctime(3)', what does the number in parentheses mean?
|
View Answer
|
|
Who helped you put this list together?
|
View Answer
|
|
Describe about awk and sed?
|
View Answer
|
|
Explain the basic forms of each loop?
|
View Answer
|
|
Explain about the case statement.
|
View Answer
|
|
What is a FIFO?
|
View Answer
|
|
Brief about the directory representation in UNIX
|
View Answer
|