-2

The actual homework question is

  1. List all files/directories NOT owned by root and not created in July.

I can't find a way to use

ls

and

grep

to output a file ~/NotOwnedByRoot.txt

find / \! -user root -d -maxdepth 1 -exec ls > ~/NotOwnedByRoot.txt  {} +

I also tried

find / \! -user root -type d -maxdepth 1 > ~/NotOwnedByRoot.txt

I get the error

find: warning: you have specified the -maxdepth option after a non-option argument !, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • does "-d" want to be "-type d"? and by "list users not owned by root" did you mean "list directories not owned by root"? (guessing at the typo-corrections) – Jeff Schaller Mar 06 '19 at 16:51
  • $ find / ! -user root -type d -maxdepth 1 > ~/NotOwnedByRoot.txt find: warning: you have specified the -maxdepth option after a non-option argument !, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. – GettingStarted Mar 06 '19 at 17:05
  • Your title does not make sense, and does not match the 2nd line of you question. Read up on what a user, file, directory and a process is. – ctrl-alt-delor Mar 06 '19 at 17:36
  • The error is a warning, but do what it says anyway. It will help you understand the command. – ctrl-alt-delor Mar 06 '19 at 17:38
  • Which July? any July, or just this past one? – Jeff Schaller Mar 06 '19 at 17:39
  • Welcome to Unix & Linux! It is generally a really bad idea to parse the output of ls. You should probably look into either using find or simple shell globbing to get your list of files to process. Extensive further reading on the subject can be found here. – DopeGhoti Mar 06 '19 at 17:40
  • The instructor doesn't care. I think he just wants to know if we can do it. – GettingStarted Mar 06 '19 at 17:46

1 Answers1

-2

you can use ls and grep together its very easy and with the -v in grep option you can search for words or phrases that are NOT there, so it will output everything which is not in conjunction with your search.

While in the directory you want to do this in try: ls -l | grep -v root || July

The first part ls -l lists all the files in a long listing order

The | symbol is called a pipe and it takes the input of the previous command and uses it as an input in the next command, in this case ls -l would be used as an input into 'grep'.

The second part grep -v root uses grep with the -v option which, with the search term root, the -v option is used to look for all contents that do not have what you searched for, so it will look for everything that does not have 'root'.

Finally the last part || July , the || basically means 'and' in grep so your specifiying two or more search terms, the 'July' is the second search term.***

PS: I DO NOT THINK THE QUESTION IS ASKING YOU TO MAKE A FILE CALLED 'NOTOWNEDBYROOT' INSTEAD ITS ASKING YOU TO SEARCH A DIRETCORY FOR ALL FILES/SUB DIRECTORYS THAT ARE NOT OWNED BY ROOT OR MADE IN JULY

programmer
  • 1,017
  • Will straight-up fail to work as intended; if grep -v root throws a non-zero exit code, it will attempt to run the command July which is very likely not to be an internal or a binary in the PATH. – DopeGhoti Mar 06 '19 at 18:21
  • If you fix your code, and explanation of | then I will change my -1 to +1 – ctrl-alt-delor Mar 07 '19 at 09:15
  • but thats what a pipe is, it takes the output of a command as an input of a next command.......... – programmer Mar 07 '19 at 11:07
  • And July is not a command. Things that are not the same, partial list: ||, |. Either way, it's wrong here. – DopeGhoti Mar 08 '19 at 16:18
  • okay, what does | | and | mean then? – programmer Mar 08 '19 at 20:39
  • ||, as you used it, has nothing to do with grep. In a shell, command1 || command2 will run command2 if and only if the exit code from command1 is falsy (i. e. anything other than 0). command1 | command2 will run command1, and send its standard output into the standard input of command2. Since July is not a command, this will fail regardless of which one you use here as presented. If you mean grep -vE 'root|July', this will fail for other reasons (e. g. a file named root or July). – DopeGhoti Mar 11 '19 at 17:07