Finding Files

Sometimes you just don’t know where to look for a file. Maybe you’ve misplaced it, or maybe you’re like me and you save everything you’ve ever done and have lost track of the file several months ago. GUI interfaces usually have some tool that can search through your disk and find files. A tool on the CLI does that too, and it’s the appropriately named find command.

With find, you can locate files by name, last modified date, or even by the content they contain.

In Creating and Reading Files, you created a file named greetings.txt in the ~/Desktop directory. Let’s use the find command to look for that file in the Desktop folder:

 $ ​​find​​ ​​~/Desktop​​ ​​-name​​ ​​"greetings.txt"
 /home/brian/Desktop/greetings.txt

The find command displays the full path to the file. The -name argument lets you specify the filename you’re looking for.

If you don’t know the entire name of the file, you can use the wildcard character (*) in the filename, like this:

 $ ​​find​​ ​​~/Desktop​​ ​​-name​​ ​​"gr*.txt"
 /home/brian/Desktop/greetings.txt

This searches the Desktop folder for any files that start with gr and end with .txt.

The find command searches a directory and its subdirectories by default. Use find to display all the files with the .log extension in the /var/log folder:

 $ ​​find​​ ​​/var/log​​ ​​-name​​ ​​"*.log"

This displays a longer list of results.

Filenames can contain both uppercase and lowercase letters, but the -name argument is case-sensitive. You can use -iname instead if you to find anything that matches, regardless of case. Search for files in the Desktop folder that start with GR using the -name flag. It’ll return no results:

 $ ​​find​​ ​​~/Desktop​​ ​​-name​​ ​​"GR*.txt"
 $

But if you use the -iname flag, the greetings.txt file will show up:

 $ ​​find​​ ​​~/Desktop​​ ​​-iname​​ ​​"GR*.txt"
 /home/brian/Desktop/greetings.txt

The find command searches directories recursively, so it can take a considerable amount of time if you start your search on a directory structure with lots of subdirectories, like your home directory or the filesystem root. You can tell find to only search a certain depth with the -maxdepth argument:

 $ ​​find​​ ​​~​​ ​​-maxdepth​​ ​​2​​ ​​-iname​​ ​​"GR*.txt"
 /home/brian/Desktop/greetings.txt

The -maxdepth option has to come before you test for matching filenames.

You can also use find to look for files that recently changed. Let’s look for files in the /var/log folder that changed in the last 30 minutes. This could be a nice way to figure out what logs on your operating system were recently active.

 $ ​​find​​ ​​/var/log​​ ​​-mmin​​ ​​-30

The mmin argument looks for things that were modified a specified number of minutes ago. Using a minus sign in front of the number makes the search look for files from the current time backward to the specified number of minutes. If you leave the minus sign off, you’d be looking for files that changed exactly that many minutes ago. If you used a plus sign, you’d be looking for things that changed more than 30 minutes ago.

When you look for files, you might want to see their attributes like you would when you run the ls command. The find command provides the ls argument to do just that. Try it out by finding anything in the /var/log directory that changed within the last 30 minutes:

 $ ​​find​​ ​​/var/log​​ ​​-mmin​​ ​​-30​​ ​​-ls

You’ll get a long listing of the files similar to what you’d get if you ran the ls -l command. If you’re picky and want to see the file sizes in sizes other than bytes, you’ll have to use find’s exec argument, which can execute a command based on the results of the search. Try this command out, which performs the same search but gives you a slightly different directory listing:

 $ ​​find​​ ​​/var/log​​ ​​-mmin​​ ​​-30​​ ​​-exec​​ ​​ls​​ ​​-ldh​​ ​​{}​​ ​​;

A lot’s going on with this command. ls -ldh is the command you execute for each file. The -d switch tells ls to treat a directory as a file rather than listing its contents. Since find may also return directories, you don’t want ls to list the contents of directories it finds; you just want to see the names of those directories. The {} is a placeholder for the file that gets passed to the program. The ; at the end tells find to run the command on each file it finds. You have to use the backslash to escape the semicolon.

Run that command, and you’ll see something like this:

 drwxrwxr-x 11 root syslog 4.0K Mar 2 12:10 /var/log
 -rw-r--r-- 1 root root 1.3M Mar 2 12:08 /var/log/dpkg.log
 -rw-r----- 1 syslog adm 7.2K Mar 2 12:17 /var/log/auth.log
 -rw-rw-r-- 1 root utmp 286K Mar 2 12:11 /var/log/lastlog

If you change the ; to a +, the find command will attempt to group the arguments into a single call. This can be more efficient:

 $ ​​find​​ ​​/var/log​​ ​​-mmin​​ ​​-30​​ ​​-exec​​ ​​ls​​ ​​-ldh​​ ​​{}​​ ​​+
 drwxrwxr-x 11 root syslog 4.0K Mar 2 12:10 /var/log
 -rw-r--r-- 1 root root 30K Mar 2 12:08 /var/log/alternatives.log
 -rw-r--r-- 1 root root 113K Mar 2 12:08 /var/log/apt/history.log
 -rw-r----- 1 root adm 52K Mar 2 12:08 /var/log/apt/term.log
 -rw-r----- 1 syslog adm 7.2K Mar 2 12:17 /var/log/auth.log
 -rw-r----- 1 root adm 984 Mar 2 12:12 /var/log/cups/access_log

In this example, the ls command runs only once, instead of once for each file in the list. Since all the files and directories are passed to the ls command, the output looks prettier; the columns are lined up because only a single command ran.

You can use find to find files for specific users, groups, and more. You can even use it to delete files it finds. But before you see how to modify files on the filesystem, let’s look at how to find information about disks and free space.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181