Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following bash command:

$(ls -l /var/ | grep www) > cat folder_exists

but it is not working properly. What I hope to achieve is a command, that checks to see if there is a www folder located inside the /var/ folder, and prints the result of that particular command to the file folder_exists. I have also tried to retype the command numerous times, example given

cat $(ls -l /var/ | grep www) > folder_exists

or even

cat $(ls -l /var | grep www) > folder_exists

just in case that slash did the damage, but to no avail.

My problem is that even though I am creating the file folder_exists, I fail to print the output to it. Could somebody please show me where I have gone wrong?

share|improve this question
up vote 8 down vote accepted

Better do that:

if [ -d /var/www ] ; then touch folder_exists ; else touch folder_absent ; fi

The first version of the command does the following: $( ... ) is first replaced by whatever was produced by .... Since you call ls -l | grep www, you are either getting a line with multiple words, or nothing. In the first case it is attempted to be executed, but that makes little sense. What will you get if you type

drwxr-xr-x 15 root root     4096 Aug 20 13:48 log

at the command prompt? There is no command called drwxr-xr-x. If you use the cat $( ls -l /var | grep www) form, then first the $( ... ) is replaced by what was produced by ..., then cat interpretes it as a list of files to cat (starting with drwxr-xr-x; most likely, there is no such file).

Moreover, if you attempt to do something like that:

command > cat file

What will happen is that cat is interpreted as a filename to store the output of command, and not as a command. To interpret cat as a command, you need a pipe sign, like you correctly used in ls | grep:

command | cat

However, if your purpose is to store the output of command in a file, then just use

command > file

As a matter of fact, you could do

ls /var | grep www > file

This file will be empty only if there is no www folder in ls. But beware! if there is a folder /var/ewwwocks, then the file will also contain a line. Better to use simply

ls /var/www > file

Long story short, use the conditional construct if ... ; then ... ; else ... ; fi

share|improve this answer
Thanks for the comprehensive answer. Clarified many things in my mind :) – NlightNFotis Aug 20 '12 at 18:25

Remove the parenthesis.

 ls -l /var/ | grep www > folder_exists

Works just fine.

share|improve this answer
That did it man, thank you! – NlightNFotis Aug 20 '12 at 18:20
Not really, if you want to check for "www" folder and not for any other folder that contains the string "www". – January Aug 20 '12 at 18:23

The -l argument to ls causes it to include a lot of extraneous information that will muck up grep. The correct command would be ls -a /var/ | grep www > folder_exists.

share|improve this answer
I do not think that there is an issue with the information provided by the -l flag, grep has gone through a mountain of information for me in the past without any issues. The only issue I could possibly have might be latency, but I do not suffer from such issues. – NlightNFotis Aug 20 '12 at 18:19
Though if one of the folders is owned by a user with www in their name then it will fail with the -l option. – Alex L. Aug 20 '12 at 18:25
I'll tell you what: I will thumbs you up if you could explain to me why it would fail, because I do not understand it. – NlightNFotis Aug 20 '12 at 18:33
I make a group called wwwgroup and create a folder in /var called something which is owned by group wwwgroup. If I run ls -l | grep www I get drwxr-xr-x 2 root root 4096 Aug 15 21:35 www/ \n drwxr-xr-x 2 root wwwgroup 4096 Aug 15 21:35 something/ – Alex L. Aug 20 '12 at 18:37
I see. Thanks for clarifying things. – NlightNFotis Aug 20 '12 at 18:42

I would suggest using find for such a task:

find /var -type d -name '*www*' -maxdepth 1
  • -name '*www*' matches anything named something with www, use -iname if the match should be case insensitive.
  • find is recursive by default, so the maxdepth option ensures that only /var/*www* are matched.
  • -type d specifies that it should be a directory.
share|improve this answer
Nice answer! I will definitely try your approach, as it may be useful in other case too. Thank you. – NlightNFotis Aug 20 '12 at 18:38

Your Answer


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.