Testing file types

While testing for values, we can test for the existence of a file or file type. For example, we may only want to delete a file if it is a symbolic link. We use this while compiling a kernel. The /usr/src/linux directory should be a symbolic link to the latest kernel source code. If we download a newer version before compiling the new kernel, we need to delete the existing link and create a new link. Just in case someone has created the /usr/src/linux directory, we can test if it has a link before removing it:

# [ -h /usr/src/linux ] &&rm /usr/src/linux  

The -h option tests that the file has a link. Other options include the following:

  • -d: This shows that it's a directory
  • -e: This shows that the file exists in any form ...

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.