Unix and the Land of the Lost

Has this ever happened to you? One day you come to work and you can't find the file you were working on with Unix from the day before. Maybe it wasn't in the directory it was supposed to be in because you made a mistake and changed directories before working on the file. Or worse yet, not only can you not find it, you can't remember what it was called and you can't remember what directory it was in!

I hate it when that happens. Fortunately, with Unix there are a variety of ways to figure out what was going on. Let's look at various scenarios one-by-one and see how to find your lost file.

  1. You have the right directory but can't remember the file name:

    A quick trick is to do a directory listing in reverse-time order. The latest files will show up at the end of the list for a quick view and you will, hopefully, recognize your file. The command is

    ls -lrt

    Often, if you can't remember the file name, you can identify some key words that would be in the file. Unix has a utility for checking the contents of files, called "grep". So, from the directory where the file should be, try

    grep "the words you want" *

    where "the words you want" is some phrase that should be in the file. Grep will report back the file name and all lines in the file where the phrase was found. Try not to use a common word or phrase since grep may report back many lines in many files. A unique word or phrase will be the least confusing. You need to use quotes only if you have spaces or other special characters in your search phrase. The character "*" is a wild card. It refers to all the files in the directory.
  2. You know the file name but it is not where it should be:

    This isn't much of a problem if you only have one or two directories, but any more than that and it is tedious to check them all. Go to your home directory and try this

    find . -name "filename" -print

    where "filename" is the name of the file you are seeking. You are telling "find" to locate any files called "filename" starting in the current directory, ".", and to list the name, "-print", when it finds one. "Find" will report the directory path to all files by that name.
  3. You don't know the file name and you don't know where it is, but you worked on it yesterday:

    This is not hopeless. "Find" can help you with this. The command would look like this:

    find . -mtime -1 -print

    Here you are telling find to list the files that were modified one day or less ago. You can increase the span of time that "find" will report back on or go to a more restricted time period. For instance, "-2" is within the last two days and "2" is two days ago.
  4. You know part of the file name but nothing else. Once again, "find" will do the job. Try this

    find . -name "*part*" -print

    where part represents the part of the file name you know. It must be surrounded by quotes.

    You can combine "-name" and "-mtime" if you need to.
  5. You just can't remember much except a phrase.

    This calls for a combination of grep and find. Let's take the case where you know that a word is in the file but you can't remember if it is in upper or lower case. Grep can do this, but it won't go out of the directory you are in. You can try this:

    grep -i word `find . -name "*" -print`

    where "-i" tells grep to ignore case (upper vs lower) when looking for word and the find command is entirely surrounded by the back quote "`". We gave a wild card for the file name in find so that all files will be searched in all directories. Put the commands together in this way, the results of "find" are given to grep as the files to be searched. You can structure the find command in different ways, just as was shown in the earlier examples. For instance you can restrict it to checking only recently changed files:

    grep -i word `find . -mtime -1 -print`

In these commands you have also seen the use of "wild cards", also known as "filename metacharacters." Some useful ones are:

  • * Match any string of zero or more characters
  • ? Match any single character
  • [abc . . .] Match any one of the enclosed characters; a hyphen can be used to specify a range (i. e. [a-z], [M-P], etc)
  • ~ your home directory

Now with all this, you should never lose your work again!

 

© 1998, The University of Alabama. The information included here is for the University of Alabama central computing facility as it was configured on the document date. It may or may not apply to other Unix systems.