-3

I wanted to change to the log directory on ubuntu server, and check duplicity log, but I don't know what happened, it seems that all log files and sub-directories have gone missing! This is the command I used when I logged into the server:

sudo su | cd /var/log | cat duplicity.log | grep -i '. errors' | sort | uniq

I suppose I should have replaces first two pipes with &&. I have several questions, can I restore logs somehow? Will they continue to appear as they usually appear? Will deletion of all the log files create some server problems? Why did this happen?

Gitnik
  • 147
  • 2
  • 9
  • 1
    How do you come to the conclusion that the files are deleted? Do you have a backup? – Sven Nov 07 '18 at 11:42
  • 2
    What's preventing you from looking on your system to see if the log files are still there or not? Also, `sort | uniq` can be replaced with a simple [`sort -u`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html). – Andrew Henle Nov 07 '18 at 11:58
  • @Sven @Adrew after I executed the command, I was in the `/var/log`. Then I executed `ls` and nothing was listed. I've logged out from the server, and logged in again, and cd-ed right into /var/log, and when I do `ls` again, it seems that all my logs are there. Strange. I don't know what happened. – Gitnik Nov 07 '18 at 13:12
  • 1
    @Liliane: After the program executed, you were not in `/var/log`. – Sven Nov 07 '18 at 13:45

1 Answers1

5

When piping commands together, each will be executed in its own shell. Subsequent programs will not inherit prior programs environment and thus, things like cd are not present anymore.

So, if you are in your home directory, say /home/user and execute cd data | cat filename, what happens is that you execute cd data and pipe the output (nothing in this case) to a new process with a new environment, which will still be in /home/user and not in /data, and thus won't find filename when trying to cat it.

A better option would be to just say cat /var/log/duplicity.log and not cding in the first place.

Even better: You don't need cat in the first place when using grep:

sudo grep -i ". errors" /var/log/duplicity.log | sort -u 

should work like expected.

Sven
  • 98,649
  • 14
  • 180
  • 226