Unix Tool Challenge Activity
Complete each of the following challenges and append the commands that you use to the Live-log used for the Linux Tutorials homework. These are best completed using your Ubuntu install in VirtualBox. A solution to each challenge is given at the bottom of this webpage. Note that there are multiple possible ways to accomplish each of these challenges. If your solution is different, try running the instructor’s solution and see if it gives the same results as yours.
-
Give a command line using the
ps
command that displays all processes that are running on the system (should be more than a hundred). -
Give a command line using the
ps
,tail
andwc
commands, with pipes, to display just the number of processes that are running in the current terminal session (useps
alone to check your results). -
Give a command line using
grep
with your solution to the previous challenges to display the number of processes that are running whose programs live in the/usr/bin
directory. -
Open a new terminal window and run the
xeyes
command. Give a command line that will terminate (i.e. kill) thexeyes
process when executed in a separate terminal window. -
Learn about the
find
command using its man page. -
Give a command line using
find
that displays the location of all files namedpasswd
that reside within a depth of 5 sub-directories of the machine’s root directory. -
In the last challenge, there are many
permission denied
error messages written to standard error (similar to standard output and appears on the console). Give a command line that redirects standard error to the file/dev/null
so that these error messages are discarded and the results can be seen more easily. Note:/dev/null
is a pseudo file on Unix systems that simply discards all data written into it. -
Consider the
passwd
file in the/etc
directory from the previous exercise. This file contains one line for each valid user in the system (usecat
to examine it). Each line has a number of fields delimited by:
characters. The next to the last field gives the path to the user’s home directory (e.g./home/braught
). Give a command line that will write the path to the home directory for theroot
user into a file nameduserhome.txt
in the/var/tmp
directory. Hint:man cut
. Note:/var/tmp
is a directory on Unix machines that all users can write to and is usually used for temporary files. -
Give a command line that will append the path of the home directory of your user to the
userhome.txt
file. -
If there is more time remaining, try some of the following Hacker Rank challenges (you may need to learn about some additional Unix commands as you go):
Challenge solutions
The solutions are padded with plenty of space to make it less likely that you will accidentally see the answer to challenge n+1 when consulting the answer to challenge n.
-
ps -aux
-
ps | tail -n +2 | wc -l
-
ps -aux | tail -n +2 | grep "/usr/bin" | wc -l
-
ps -aux | grep "xeyes"
(to find the process id (PID) for xeyes)kill -KILL <PID>
-
none
-
find . -maxdepth 5 -name "passwd" -print
-
find . -maxdepth 5 -name "passwd" -print 2> /dev/null
-
grep -a "root" /etc/passwd | cut -f6 -d':' > /var/tmp/userhome.txt
-
grep -a "myuser" /etc/passwd | cut -f6 -d':' >> /var/tmp/userhome.txt