0

As a part of my homework exercise, i am finding it difficult to execute the following two commands. Can you please rectify the error in it. Thanks in advance

Write a program, that will take a file which has a set of directories, and will look into these directories and their sub directories for any files named core.* and *.o and will print a report of all such files found. (you can use find)

For the above program i attempted to do like this.

cat prog1 | xargs find /$0 -name 'core.*' or '*.o'

But I am getting the following error. (prog1 is a file containing list of directories)

find: paths must precede expression: perl
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

The next question is:

Write a script that will go through the /etc/passwd file, and from this print a list of users in the system, their home directory and the shell that they are using

For which I tried the following:

awk '{print "USER \t\t HOME \t\t BASH \n split($0,a,":")}
     {print $a[1] \t\t $a[6] \t\t   $a[7]}' /etc/passwd

I tried googling and seeing the man entries but I don't understand the error. Also can you provide some useful links to shell scripting for beginners?

2 Answers 2

1
Write a program, that will take a file which has a set of directories, and will
look into these directories and their sub directories for any files named core.*
and *.o and will print a report of all such files found.  (you can use find) 

I suggest following oneliner:

find `paste -s -d' ' directories.list` -type f -name 'core.*' -or -name '*.o'

Where directories.list contains directories names, one by line.

Write a script that will go through the /etc/passwd file, and from this print a
list of users in the system, their home directory and the shell that they are
using

Try this:

awk -F: 'BEGIN{print "USER\t\tHOME\t\tSHELL"}{printf("%s\t\t%s\t\t%s\n", $1, $6, $7)}' /etc/passwd
Sign up to request clarification or add additional context in comments.

4 Comments

thank you :) it worked.Pls can you tell me what was the error in my approach
@chid 1) When xargs invoke find it adds your path to end of command, but find expected it before expressions 2) (as @ignacio-vazquez-abrams already said) find doesn't have or option (use -or or -o instead)
"adds your path to the end of command" - can you please explain this part?
@chid when xargs build command to run it append lines from stdin to command which your specify. In result it executes command like following: find / -name 'core.*' or '*.o' path1 path2 path3 `
0

Hint 1: find does not understand "or". The operator follows the same pattern as the other options.

Hint 2: awk can use a custom delimiter/field separator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.