Special File Types   «Prev  Next»

Creating more targeted finds

For your convenience, here are the steps you followed to solve the system administration scenarios:
  1. You are logged on as user1, and are in your home directory. Use the ls -l command to view the files.
    Solution: ls -l
  2. Notice the large number of files and directories. Instead of spending time sifting through this huge list, issue a command that lists the 10 newest files.
    Solution: ls -t | head
  3. List the three oldest files in your home directory.
    Solution: ls -t | tail -3
  4. In conducting a quick audit of the /etc/ directory, you discover a new file that is publicly writable. You now wish to learn about other publicly writable files on your entire system. To enable the most thorough search, assume root privileges.
    Solution: Type su and enter the root password.
  5. Now that you have assumed root privileges, issue a find command that searches the entire system for all publicly writable files (that is, files that have write permission in the "other" block). Although in a real UNIX environment, the order of the find predicates don't matter, for this simulation use the permissions predicate first followed by the type predicate. Do not forget to end the command with the print predicate.
    Solution: find / -perm -002 -type f -print
  6. Your assistant has created a new directory in /usr/bin, but he is now unavailable. You need to identify the directory he created quickly. Issue a find command that discovers how many subdirectories exist in /usr/bin. Make sure that you use -print at the end of your command.
    Solution: find /usr/bin -type d -print
  7. A user with the account name user2 has gotten lost in her /home/user2 subdirectory. Before she called you, she created a file, but now she can't find it. She knows that the file was created today. Issue a find command that lists all the files user2 owns that were created in the last day. For the purposes of this simulation, specify the user predicate first, followed by the predicate that allows you to search for modification time, and then end your find command with the -print predicate. Finally, use the redirect operator to save this list into a text file named lost in the user's home directory.
    Solution: find /home/user2/ -user user2 -mtime -1 -print > /home/user2/lost
  8. You are still root, and are in the /home/user1 directory. You wish to confirm that the lost file was created and that it has the correct contents. Use the head command to read the lost file you created in the /home/user2 directory.
    Solution: head /home/user2/lost
  9. User2 can find her file by studying the file named lost. Now, you wish to search for and delete all empty text files in user2’s home directory. Issue a find command that searches for empty text files in the /home/user2 subdirectory. Don't forget to end the find command with the -print predicate.
    Solution: find /home/user2 -size 0 -print
  10. Now, modify the command you just entered so the system will find the empty files and then prompt you to see if you wish to delete each file.
    Solution: find /home/user2 -size 0 -print -ok rm {} \;
  11. Type y to delete the file named lostfile.
  12. Type n to keep the file named readme.
  13. Type n to keep the file named september141995.
  14. Exit the root subshell.