CIT050 Index > Assignment: find (1)

Assignment: find (1)

The most up to date version of this assignment is at Moodle.

Read about find

Read pages 804-809 in the book (688-694 in the old edition). Also read this tutorial. In the tutorial, the author uses a \ to prevent shell wildcards from being expanded. Thus, to find all files in the current directory and its subdirectories that start with foo and end with txt, he would type:

find . -name foo\*txt

Another way to keep the shell wildcards from beign expanded is to enclose the argument in single quotes. I find this method easier, but you may use whichever method you prefer. Thus, I would type:

find . -name 'foo*txt'

The Assignment

Note: when I say “in the current directory,” I mean “in the current directory and its subdirectories (if any),” because find searches through subdirectories automatically.

  1. Copy file ~linux199/exfind.tgz to your home directory and un-tar it.

  2. Type script at the prompt to create a file named typescript.

  3. Type cd exfind to enter the directory that you untarred in step 1.

  4. Write a find command that will find all files in the current directory that begin with the letter m

  5. Without changing directories, write a find command that will find all files in the /etc directory that begin with rc and end with .d

    For example, this would find a file like rc6.d or rcontrol.d in the /etc directory. When you run this command, you will get some "Permission denied" messages, as there are files in that directory that you don't have access to. Don't worry about those error messages.

  6. Find all files in the current directory that are symbolic links

  7. Find all files in the current directory that are more than 8K bytes in size.

  8. Find all files in the current directory that end with .pl and were modified more than 30 days ago.

  9. Use the touch command to set the modification date of some of the files in the current directory and/or its subdirectories to the current day and time. Then find all files in the current directory that are less than 8K bytes in size and were modified less than 30 days ago. (If you don't use touch, it won't find anything, as all the files were created more than 30 days ago.)

  10. Type this command:

    find . -name '*.xml' -exec 'ls' '-l' '{}' ';'

    Everything following the -exec is (if you take off the quote marks) a Linux command. What this command does is to find all the files that end with .xml, and every time it finds a file, it executes the command:

    ls -l {};

    where the {} is like a “fill-in-the-blank” with the filename that it just found. You must put a ';' at the end of the command, or it won’t work properly.

  11. Another way to do the same thing is this–try it and find out:

    find . -name '*.xml' | xargs ls -l

    This will find the file names and pipe them to the xargs command. xargs is followed by a command (possibly with options) that will be executed for each of the files that find found.

  12. Using this knowledge, find all files in the current directory that begin with the letter c and execute the file command on each of them.

  13. Type exit to end the script.

  14. Rename the typescript file in the form lastname_firstname_find.script and upload it to Moodle.

Hint

You can do the following command when you are in the exfind directory:

ls -lR

(That is a lowercase letter “L” after the dash.) This command gives you a long listing of all files, Recursively entering all subdirectories. You may want to copy and paste this into a file on your local machine so you can print it out. With this listing, you can check for yourself to see if your find commands have found the proper files.