Linux Exercise: Shell Commands

file, cat, tac, head, tail, more, less

For this part of the exercise you are going to need a fairly large file: One that is at least about 100 lines. The file /etc/services is a good example of this.

  1. Copy the file you selected to use in this exercise to your home directory, renaming it myfile in the process.
    • $ cp /etc/services ~/myfile
  2. Use the file command to determine the type of contents of the file. Also, apply the file command to files such as /dev/null, /bin/ls and /tmp.
    • $ file myfile
    • $ file /dev/null
    • $ file /bin/ls
    • $ file /tmp
  3. Display the contents of the file myfile on your screen.
    • $ cat myfile
  4. Display the contents of the file myfile on your screen, last line first.
    • $ tac myfile
  5. Display the first five lines of the file myfile on your screen.
    • $ head -n 5 myfile
  6. Display the last five lines of the file myfile on your screen.
    • $ tail -n -5 myfile
  7. Display the last portion of the file myfile on your screen, beginning at line number 5.
    • $ tail -n +5 myfile
  8. Display the file myfile on your screen, page by page. Use both the more and less commands. What is the difference?
    • $ more myfile
    • $ less myfile
  9. (Challenge) Count the number of unique users that are currently logged in.
    • $ w | tail -n +3 | cut -d' ' -f 1 | sort | uniq | wc -l
  10. (Challenge) Count the number of unique users that are currently logged in. Simultaneously, save the full list of usernames to a unique temporary file for later analysis.
    • $ w | tail -n +3 | cut -d' ' -f 1 | sort | tee /tmp/intermediate_results.$$ | uniq | wc -l

Manipulating output

In this section we will use the /etc/passwd file as an example of a well-structured file, which allows us to manipulate our output.

  1. Display the file /etc/passwd on your screen, numbering all lines in the process.
    • $ nl /etc/passwd
  2. Display the file /etc/passwd on your screen. Use the cut command to only display columns 1 and 3.
    • $ cut -d: -f1,3 /etc/passwd
  3. Display the file /etc/passwd on your screen. Display only the lines describing the users that have /bin/bash as their default shell.
    • $ grep '/bin/bash$' /etc/passwd
  4. Display the file /etc/passwd on your screen. Display only the lines describing the users that do NOT have /bin/bash as their default shell.
    • $ grep -v '/bin/bash$' /etc/passwd
  5. Display the file /etc/passwd on your screen. Convert all lowercase letters to capitals.
    • $ tr '[a-z]' '[A-Z]' < /etc/passwd
  6. Count the number of lines, words and characters in /etc/passwd
    • $ wc /etc/passwd
  7. Count the number of users that have /bin/bash as their default shell.
    • $ grep '/bin/bash$' /etc/passwd | wc -l
  8. Display a list of usernames that have /bin/bash as their default shell. Number the lines.
    • $ grep '/bin/bash$' /etc/passwd | cut -d: -f1 | nl
  9. Display a list of usernames that have /bin/bash as their default shell. Sort the list in alphabetical order. Number the lines.
    • $ grep '/bin/bash$' /etc/passwd | cut -d: -f1 | sort | nl

type, which, whereis

  1. Use the type command to get information about the ls command.
    • $ type ls
  2. Use the which command to get information about the ls command.
    • $ which ls
  3. Use the whereis command to get information about the ls command.
    • $ whereis ls

dd

  1. Use the dd command to create a file of exactly 1 megabyte in size.
    • $ dd if=/dev/zero of=bigfile bs=1M count=1

sed

  1. Run the chfn command. This will ask you for your name, office, office phone and home phone. This will then be stored in the GECOS field of the /etc/passwd file, so others can use this information to reach you.
    • $ chfn Answer all questions.
  2. Take a look at the line in the /etc/passwd file that describes your user account. Can you see the information you put in?
    • $ grep <your name> /etc/passwd
  3. Use the sed command to display the file /etc/passwd. Change all occurrances of "/bin/bash" to "default".
    • $ sed 's/\/bin\/bash/default/g' /etc/passwd
  4. Use the sed command to display the file /etc/passwd. Use a regular expression to match and print columns 1 and 3 only.
    • $ sed 's/^\([a-zA-Z0-9-]*\):x:\([0-9]*\).*$/\1:\2/' /etc/passwd
      In order to help you understand this regular expression, build it up in steps:
      1. The first column is lowercase letters, uppercase letters, digits and the minus sign only. Then comes the column delimiter character, the colon. The second column contain an 'x' only, followed by another colon. The third column is numbers only, and the rest can be ignored. This means the regex becomes ^[a-zA-Z0-9-]*:x:[0-9]*:.*$
      2. We specifically want to filter out the first and third column, so we surround this by round braces. And these braces need to be escaped, so the regex now becomes ^\([a-zA=Z0-9-]*\):x:\([0-9]*\):.*$
      3. We can now construct the sed command. This consists of the s (substitute) command, followed by a slash (/) as identifier to start the expression, followed by the earlier regex, followed by another /, followed by the substitution and a final /. The substitution refers to the bits that were matched in the regex, so \1 and \2 separated by a colon.
  5. (Challenge) Use the sed command to construct a phonebook-like list of usernames from the /etc/passwd file. Only print the names of users that actually have a phonebook-like gecos field.
    • $ sed -n 's/^[^:]*:x:[0-9]*:[0-9]*:\([^,]*\),\([^,]*\),\([^,]*\),\([^:]*\):[^:]*:[^:]*$/\1 Office: \2 Office phone: \3 Home phone: \4/p' /etc/passwd

awk

  1. Make a long list of all files in your current directory.
    • $ ls -l
  2. Make a long list of all files in your current directory. Filter your output so that only the size and filename is shown.
    • $ ls -l | awk '{print $5 " " $9}'
  3. Make a long list of all files in your current directory. Filter your output so that only the size and filename of regular files is shown.
    • $ ls -l | awk '/^-/ {print $5 " " $9}'
  4. Make a long list of all files in your current directory. Filter your output so that only the size and filename of regular files is shown. At the end of the list, write the total filesize of all regular files combined.
    • $ ls -l | awk 'BEGIN { sum=0 } /^-/ {print $5 " " $9; sum+= $5} END { print "Sum: " sum}'
  5. Make a long list of all files in your current directory. Filter your output so that only the size and filename of regular files is shown. At the end of the list, write the number of files, the total filesize and the average filesize.
    • $ ls -l | awk 'BEGIN { count=0; sum=0 } /^-/ {print $5 " " $9; count++; sum+= $5} END { print "Count: " count " Sum: " sum " Average: " sum/count }'
  6. Make a long list of all files in your current directory. Filter your output so that only the size and filename of regular files is shown. At the end of the list, write the number of files, the total filesize and the average filesize. Also, write the number of directories you enountered.
    • $ ls -l | awk 'BEGIN { dircount=0; count=0; sum=0 } /^-/ {print $5 " " $9; count++; sum+= $5} /^d/ { dircount++ } END { print "Count: " count " Sum: " sum " Average: " sum/count " Directories: " dircount}'
  7. (Challenge) Ask the instructor to make the file /var/log/secure world-readable. In this file you will find the logging information of the sshd daemon, so you can see when a session was started ("sessioned opened for user...") and when a session was closed ("session closed for user..."). Using awk, parse this file and display the maximum amount of parallel connections.
    • $ awk 'BEGIN { cur=0; max=0 } /session opened for user/ { cur++; if( cur>max ){ max = cur} } /session closed for user/ { cur-- } END { print "Max sessions: " max }' /var/log/secure
End of exercise