Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am giving some interviews right now and recently I was asked this questions in Interview and I was not sure of the answer, in your opinion are this kind of questions worthwhile for Interview process and if yes than how would you go about approaching this kind of questions.

  • How to get number of files in directory without using wc ?
  • How to get all files in descending order on size ?
  • What is the significance of ? in file searching ?

Would appreciate if you can provide answers for this questions so that I could learn something about them as I am not sure for this questions.

share|improve this question
1  
Define "worthwhile". Yes, I think they're worthwhile if I want an estimate on the experience you have with a *nix system. No, they're not worthwhile, if you get one wrong and I automatically disqualify you. –  Stephen May 15 '10 at 22:09
add comment

migrated from stackoverflow.com May 15 '10 at 22:05

This question came from our site for professional and enthusiast programmers.

4 Answers

up vote 6 down vote accepted
  • How to get number of files in directory without using wc ?

    ls | grep -c .

    Doesn't count hidden files, works in every POSIX environment.

  • How to get all files in descending order on size ?

    ls -l -L | sort -n -k 5

    Ignores hidden files, follows symlinks, doesn't work for special files like devices, works in every POSIX environment.

share|improve this answer
1  
+1 can you start on Monday? –  Yar May 16 '10 at 2:11
    
@florian, can you explain more in detail of how did you obtained ls -l -L | sort -n -k 5 –  Rachel May 16 '10 at 4:05
    
@Florian: Let's say we want to find files in ascending order compared to descending order than what would be the approach ? –  Rachel May 16 '10 at 4:11
    
Awww, gee, I'm just being picky, but that's not in size order, it's maximum offset order. Depending on the interviewer, it may or may not be a good thing to go and riff on sparse files and the difference between max offset and file size on disk. –  mpez0 May 17 '10 at 19:38
    
@Rachel: ls -l -L lists the files with some information and follows symlinks; the size in the the 5th column. sort -n -k 5 sort its input according to the 5th column using numerical sorting. Add -r to the sort command to reverse the sorting order. –  Florian Diesch May 18 '10 at 20:46
show 2 more comments

First, read up the man pages on ls, grep and wc. The web links might be useful, but each system might have its own version. So try running man ls etc on the command line on the system you intend to run this.

Also, do you know about '|' (pipe)?

What would the command

ls | wc -l 

do?

share|improve this answer
3  
The question did say without using wc..... –  KTC May 15 '10 at 22:12
    
I understand the concept of grep, ls and wc separately but how to get list of all files in descending order of its size and also the number of files in directory without using wc ? –  Rachel May 15 '10 at 22:13
    
@Rachel: They're different questions. Look at the options for ls. There's size sorting. Done. Num files is trickier, I can think of a few things - maybe counting arguments? –  Stephen May 15 '10 at 22:29
add comment
  • How to get number of files in directory without using wc ?

Assuming bash, hidden files, no directories, fifos, etc.:

shopt -s dotglob
foo=(*)
echo "${#foo[@]}"

This enables wildcard matching on hidden files, puts all the filenames into an array, then echoes the length of the array.

  • How to get all files in descending order on size ?
ls -S
  • What is the significance of ? in file searching ?

In a glob pattern it matches exactly one character, no more, no less.

share|improve this answer
    
Note that ls -S requires GNU ls, POSIX doesn't define -S. So if the interview is about Unix your answer may be wrong. –  Florian Diesch May 15 '10 at 23:42
add comment
  • In your opinion are this kind of questions worthwhile for Interview process and if yes than how would you go about approaching this kind of questions.

    • This is a good series of questions if you want to encourage the candidate to take a look somewhere else on the job market. As it has been mentioned the behaviour of ls and practically any tool of this sort depends on the platform. Is this GNU or POSIX? Is this Solaris or Linux? Which version of the program are we using? There are some basic parameters that are the same everywhere, but such problems may occur like ls -l using tabs or spaces between the columns on different platforms...

    • These are critical details, when creating complex scripts and is easiest to overcome by trying them out in the shell and reading their man entries.

    • It is also possible that they were testing your problem solving skills and would prefer the answer "it depends". Then you could elaborate on all the above mentioned things and they can be sure that you are a thorough professional, who won't jump into guessing games when it comes to coding... :)

I would suggest not to worry in case you are asked such things. It should be enough to state that you would use a combination of ls, wc, grep, find and awk and you would have to check the man documentation of the given platform these need to be applied.

In my opinion there is no point in learning all the small details of these tools since they may differ in small details on different platforms. You have to have a basic understanding of what tools are given that could help you with this issue.

If the job is about being an operator on one single version of one single platform and finding and parsing stuff all day long then of course this could be a crucial piece of knowledge... :)

  • Last but not least:

    man <COMMAND>
    
    • This little command is a good friend of all system administrators and scripters

    • Also if you want to search in the content of the printout of man you can just press / and type what word or phrase you are looking for and press ENTER. n and b shall move to the next and previous search results.

  • For learning I would suggest BASH and browsing TheLinuxDocumentationProject

Good luck!

share|improve this answer
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.