 Welcome back everyone. Today we're answering a question from a viewer how to sort through all of the files by file extension from the command line. So how to make a list of all of the files in a suspect drive, for example, and sort those files by file extension. So I've mounted up an image and we have the partition mounted at Mount C. So if I go into slash MNT C, then you can see I have a Windows file system here. And now I can basically search through all of the suspect data. One useful tool for trying to find things is the find command. And if I just do find period, then I'm going to find all files essentially in the current directory and it will be recursive. So let's go ahead and just start that. And you can see that it's listing out all the files and directories here. So find is very useful if you want a quick recursive list of all of the files and directories from your current position. Let's say that I want to find a file, but I want to find only the JPEG images. What I usually use is find and then pipe the result into grep. Now find itself has a lot of different options that you can use to not only find particular files and match particular patterns, but also do some type of execution on those files. So find is an interesting program by itself and we could probably do all this just with find, but I tend to pipe everything into grep anyway just because that's what I'm comfortable with. So let's say that I want to find all the files that have a JPEG extension. So if I go ahead and hit enter, you can see the files aren't going by as quickly, but there are quite a few of them. We're doing fairly good because most of these actually do have a JPEG extension. However, this could also match files where I have JPG anywhere in the name. So if we had a directory, for example, called JPG, then it would match all of the files underneath that particular directory. What if I want to find JPEGs and BMP files? So not only things with JPEG in the name, but also BMP. What we can do is grep-E and then put a single quote around JPEG. Put another pipe between JPEG and BMP and then another single quote. What this will do is take all of our files that we're finding from find and then use grep to filter them. And we will use essentially a regular expression to match either JPEG or BMP in the file name. Okay, so let's go ahead and run that and let's see if we found any BMPs. Okay, yeah, so one of the first things we actually found was a BMP file and a JPEG. You can see here the JPEG is actually in the name of this DLL file. This is not a JPEG image. All right, well, what if I just want extensions that are JPEG or an extension that's BMP? So basically the extension is at the end of the file name. Well, what I could do, the code for an extension or the, let's say, last part of the file name is a dollar sign. So I can do JPEG dollar sign and BMP dollar sign. What that essentially says is that JPEG has to be the last part of the file name or BMP has to be the last part of the file name. Let's go ahead and run that again. And let's see what we got at the very beginning. I think we had some matches. We see that we don't have those DLLs matching anymore. Everything that matches is essentially just at the end of the file name. Okay, so with JPEG dollar sign, BMP dollar sign, we are essentially matching only those files with that particular file extension. I might also want to add JPEG because it sometimes shows up there. All right, so now we have a couple of file extensions that we're looking for. And it's not that that's actually a JPEG file or a BMP file. It could have the incorrect extension and we will find it using this particular method. So if you use grab dash E and then build up this regular expression inside the quotes, then you can start to filter out for very, very specific things. I highly recommend learning how to use regular expressions. So what happens here is we have a BMP file and then we have a JPEG, JPEG, JPEG, BMP, JPEG, BMP. But what if I want to group all of the JPEGs together and all of the BMPs together? I am going to first do rev, which is reverse all of the results. Okay, so let's go ahead and see what that gives us. We're finding everything in the current directory, listing everything, and then using this grep to filter only for the lines that end with JPEG, JPEG, or BMP. And then I'm using reverse. And you can see what reverse does is actually literally reverse every character on the line. So now BMP turns into PMB and we have our JPEG, which is GPJ. Yeah, that's it. All it does, all reverse does is literally reverse the characters. And you might be thinking, well, that doesn't seem very interesting. Well, we can use reverse with sort. What that does is essentially the sorting by the first character. And then we can just reverse again. And let's see what we get. It's taking a lot longer because it's doing that sort process. It has to get all of the files in the, in all of the directories that we're going through and then sort them and then present the results. Okay, so now we've returned and you can see the first group is JPEG, JPEG. And then we had one JPEG. We have a JPEG. So apparently we only have one JPEG in the entire image. Everything else looks like it was a JPEG. So let's go ahead and skip down here. Everything is JPEG because it's sorted properly. And then at the very end out of all of these files, we should eventually come to BMPs. Yep. So BMPs are at the very end. We have find, which lists all of the files from the current directory recursively. We're piping that output into grep to do filtering. We're using dash E, which is similar to eGrap or treat everything like a pattern. And then we have inside the quotes, we have what we're searching for. So JPEG and the dollar sign tells me it's going to be at the end of the line or JPEG at the end of the line or BMP at the end of the line. Then whatever results from that, we take that output, send it to reverse, basically reversing every character on that line and then sorting. And that sort is essentially taking the first character on the line and then sorting it, taking second character and sorting it. So what we're doing is sorting by file extension at the end of the line. Then we're reversing it. That way it's again human readable and we can actually see what's going on. And then we're just outputting it. This more command basically just lets me do paging. So I'll run it just with one reverse. If I do more, you can see that it has this more command. And if I hit space bar, then I can go down another page essentially. So it's just easier to read. So that's it. If you want to sort by file extension, once you're searching over an entire suspect disk, this is a very easy way to do it and relatively quick. Just change for the extensions that you want. Add any other regular expression pattern in there that you want. And that's it. So that's it for today. Thank you very much.