The above command would result in quite a lot of output (much of it indicating permission denied), but would include output similar to: /dev/hidraw6 With the help of the -type option, we can do that like so: find / -type c Now, suppose you want to locate all block devices (a file that refers to a device) on your system. What if you’re not so concerned with locating a file by name but would rather locate all files of a certain type? Some of the more common file descriptors are: So the new command looks like: find / -iname linux.odt Find by type If, however, you were to alter the command by using the -iname option, the find command would locate your file, regardless of case. Remember, Linux is very particular about case, so if you’re looking for a file named Linux.odt, the following command will return no results. Because we are attempting to find a file by name, we’ll use one of two options: However, we are starting from the beginning, so let’s make it easy. As with most Linux commands, you have a number of available options. The next bit of the command is the option. In the name of efficiency, it is always best to have at least an idea where to start searching. Entering that as your path would take find longer to process - because it has to start from scratch - but if you have no idea where the file is, you can start from there. Well, the path for the file could be the root of your drive so / would be a legitimate path. You may be thinking, “If I know the path to the file, I’d already know where to find it!”. The most simplistic structure of the find command should include a path for the file, an option, and the filename itself. Let’s break down that basic command to make it as clear as possible. The most basic syntax of a basic find command would look like this: find /path option filename For ease of understanding, let’s simplify that. Naturally, if you’re unfamiliar with how man works, you might be confused about or overwhelmed by that syntax. If you issue the command man find, you’ll see the syntax of the find command is: find Little did I know there was more to the command than that. It seemed simple issue the command find FILENAME (where FILENAME is the name of the file) and the command was supposed to locate the file and report back. When I first glimpsed Linux, back in 1997, I didn’t quite understand how the find command worked therefore, it never seemed to function as I expected. Let’s get up to speed with the find command so you can take control of locating files on your Linux servers and desktops, without the need of a GUI. That’s not only convenience, that’s power. To make the understanding of this command even more enticing, once you know it, you can start working it into your Bash scripts. If your default desktop doesn’t - because this is Linux - you can always install an app to make searching your directory hierarchy a breeze.īut what about the command line? If you happen to frequently work in the command line or you administer GUI-less Linux servers, where do you turn when you need to locate a file? Fortunately, Linux has exactly what you need to locate the files in question, built right into the system. It goes without saying that every good Linux desktop environment offers the ability to search your file system for files and folders. For more great SysAdmin tips and techniques check out our free intro to Linux course. Note, though, that this would give you strange results if any filename contains embedded newline characters.This is a classic article written by Jack Wallen from the archives. If you need to do this recursively, then you may use find like so: find. The above loop does not consider hidden names. The basename utility outputs the filename portion of the pathname given as the first argument, and if a second argument is given, that string is stripped off from the end of the name. Assuming you want to show the names matching the pattern *.sh from the current directory, without the.
0 Comments
Leave a Reply. |