0

Where is the log directory in WSL?

There seems to be no such directory as /var/log/.

My aim is to run this *nix cron daily command:

0 0 * * * cat /dev/null > /var/mail/* /var/log/*
  • There is a /var/log in WSL. But you can't use a directory as stdout. What do you want to do with your daily command? – muclux Jan 14 '18 at 19:03
  • The command would be rm -r /var/mail/* /var/log/* but I am not so sure if this is a good idea. – muclux Jan 14 '18 at 19:09
  • 1
    Redirecting to a list of files (and directories in your case, as /var/log contains subdirectories) will not work. You could use find /var/log -type f -exec > {} \; to empty all files in /var/log. – muclux Jan 14 '18 at 19:14
5

/var/log is at /var/log (at least in the version 16.04.3 LTS that I just installed in my Windows system).

$ ls -l /var/log
total 304
drwxr-xr-x 0 root root    512 Sep 22 18:15 apt
-rw-rw---- 1 root utmp      0 Sep 22 18:15 btmp
drwxr-xr-x 0 root root    512 Jul 19 01:29 dist-upgrade
-rw-r--r-- 1 root root  12794 Sep 22 18:15 dpkg.log
drwxr-xr-x 0 root root    512 Sep 22 18:13 fsck
-rw-rw-r-- 1 root utmp 292292 Jan 14 19:58 lastlog
drwxr-xr-x 0 root root    512 Aug 23 04:06 lxd
drwxr-x--- 0 root adm     512 Aug  1 04:46 unattended-upgrades
-rw-rw-r-- 1 root utmp      0 Sep 22 18:15 wtmp

But you won't be able to redirect your output to a list of files and subdirectories (as I told you in the comments).

You can empty all log files by the following cronjob (for root):

find /var/log -type f -exec truncate -s 0 {} \;

| improve this answer | |
  • Sure, input cannot be redirected to a directory but to a file in a directory. I need to ask your apologize, I confused the /var/log dir (which is available it my WSL also) with a file - I treated it as if it were a file. I can indeed do rm -rf, for example, to remove these directories even though a sysadmin recommended me to redirect /dev/null into the files inside them. Something I really wish to do. – Arcticooling Jan 14 '18 at 20:24
  • 1
    Your find command won't work the way you think it will. > will be handled by the shell before find is started, so the command executed will be find /var/log -type f -exec echo -n \; with the entire stdout redirected to a file named {}. You probably want something like -exec truncate -s 0 {} \; or -exec sh -c 'echo -n > "$1"' _ {} \; – muru Jan 15 '18 at 7:12
  • @muclux ah, looks like I edited my comment after you saw it, it should be truncate -s 0 {} \; (Sorry!) – muru Jan 15 '18 at 7:18
  • @muru My fault, I should have verified how truncate works (I didn't know this command). – muclux Jan 15 '18 at 7:27
  • Thanks both of you. In the end I used find /var/mail/* /var/log/* -type f -exec truncate -s 0 {} \; based on your example, @muclux. – Arcticooling Jan 15 '18 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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