Morgan Stanley Interview Question: A few questions on basic comm... |

Interview Question

Software Engineer Interview Montreal, QC

A few questions on basic command-line syntax in Unix

  shells: 1. How would you log output and error messages from a command to a file? 2. How would you run the same command on every file in a directory? 3. How would you find the PID of a named process (say if you wanted to kill it)?

Interview Answer

6 Answers


1. command >file 2>&1
2. cd dir; for i in *; do command; done
3. ps | grep processname or ps -C processname

Interview Candidate on Feb. 28, 2012

#3 I disagree, more like ps aux |awk '$0 ~ /ProcessName/ && $0 !~ /awk/ {print $2}'

If you want the PID

Gregg Leventhal on Mar. 3, 2012

#3 To find the PID:

pgrep -x

Kenroy Harrison on Aug. 24, 2012

#3 - to find the PID
Or simply use: pidof

Santanu on Apr. 30, 2013

Opps!! there is typo; it should be: pidof

Santanu on Apr. 30, 2013

1. ./ &> output.txt :- to capture output and error message
suppose have below script:
cat file1
cp file2 file3

lets assume there's no file2 exists in the current directory, returns an error message

2. from any current directory run the below line by line.

for i in `find -maxdepth 1 -type f`
cat $i >> output.txt

in for loop i stores all the files (type f means normal file type) in a current directory (maxdepth 1), suppose you want to consider all subdirectory files remove this part.
then redirecting all the files to output.txt

3. ps -ef | grep -i "*process name*

ps displays running processes, -e utility here displays all the processing shell processes without ignoring any process, f gives the full text/all info of processes.

if you are not sure on the exact process name give * as above by giving part of the process name that you can remember

Prasad M on Aug. 3, 2020

Add Answers or Comments

To comment on this, Sign In or Sign Up.