 About a month ago. I did a poll on the community tab asking if people would like to see more scripting on the channel and the Response was kind of overwhelmingly. Yes, and that was really surprising because I'm very much a newb when it comes to scripting So it's taken me about a month to decide that I'm actually going to dive into some more scripting videos And I've decided to start off a little simple So today I'm going to be creating a bash script that will delete your trash from your computer Now when I'd say trash I mean things that were put in the trash folder not necessarily other things But this could be used for deleting your your cash or whatever if you wanted to do that It'd be fairly simple to adapt But we're gonna focus on the trash folder today now this script is actually very simple So it this shouldn't be a very long video before I jump in I'd like to ask you to so if you haven't already subscribe to the channel hit the Like button and comment all that kind of stuff really does help the channel out So thanks everybody who's already done that and if you haven't already done that make sure you do it So let's go ahead and jump in so the first thing we're going to do is locate the Trash folder now on Linux if you're using like I'm going to assume that you're using Linux Your trash folder is likely in Dot local share trash and that is the case for me It's possible that your trash folder is in your home directory in a hidden foot file called trash I don't have a file like that, but you may have that file It'll be in one of the two places. I believe So once you've located the file, you don't have to do anything. You just have to know that You just have to know that path So let's go ahead and create our script file. So what we're going to do is touch Trasher sh we're going to call this script trasher and then we're going to chmod Trasher sh plus x so we're going to make it executable and then we're going to Vim into trasher That sh okay, and all this is is a blank file now And then you need to go ahead and get into insert mode and we're going to put in a shebang so Pounce line exclamation point slash bin slash bash and you can use slash sh if you wanted to I'm just going to use bash because that's the one that I'm most familiar with Technically if you use slash sh all you're doing is looking to the systems Default shell because that's just a sim link or something So and then we'll put in a couple comments here. So this is just I'm just going to put script by Matt Weber and then version 1.0, okay, and then I'll put a link to my github. So HTTPS colon Slash slash github calm Slash MTWB 47. Okay now once we've got that done I'm going to show you two ways to do this and they're very simple We're going to do one way that is an if statement and one way that is kind of an if statement So the first way is going to be multiple lines long So what we're going to want to do is check to see if that folder that we just found the path of exists so we're going to do that with an if statement and basically what I want to do is create some brackets here and then do minus D and quotation marks and Then the path of the folder that you want to see if exists. So in this case we want to do dollar sign home slash Dot local Slash share Slash trash Okay, and then you want to put a space at the end of this make sure there's a space between the brackets and that okay I'm not sure if it would work without this the brackets, but I don't think it will and Then we need a semicolon and then then Okay, so if so basically what this is saying is if this file here exists Then we want to do whatever comes next and in this case we want to just remove the trash file So we want to do remove dash RF and then Dollar sign home slash dot local slash share slash trash Okay, and Then else Exit I guess is what we could do technically Or you could do we could do Echo I actually will probably be doing print f I Think it's actually single quotes, but I'm not actually sure what we'll have to test it No trash folder found Okay, and then end the statement or end if statement we do fi. Okay, and basically that's the entire script Okay, and Really that's very simple the Problem is that this is like what four or five lines or whatever you can actually do this in all in one line So if we do it this way so basically what we're doing is we just do taking this so Do it brackets to create a space do minus D dot and then I Don't even think you need the quotation marks, but we're just gonna go ahead and do the Actually, let me check and see because I don't think My research I Don't think you need the quotation marks We're gonna try it without that quotation marks because I don't think you need them up here I'm not sure if I needed them or not Like I said new bit scripting, but I think it'd go either way actually, but we'll see we'll try this without So basically what you want to do is dollar sign home Slash dot local share Trash, okay, and then again make sure you're putting in the space and then what you want to do is and and so basically what this is all this does is It checks if this file exists. It doesn't need an if statement at all. Okay, and then you want to do RM Dash RF and then you want to do the file name again. So in this case dollar sign home Slash local share and Then trash and then you want to I'm actually forgetting about something you don't want to delete this whole file I'm sorry if you're following along you want to actually do slash star you want to do everything that's in the file You don't want to delete the file itself I knew I forgot something the same thing here slash Astros just means delete everything that's in the file. Okay, because you want to keep the file. Otherwise things will break Okay, and once that's done and do echo you can do printf. I'm gonna use echo here printf I think and Then do single quotes Trash empty, okay, and we'll do we'll add that up here as well and and Print f trash trash emptied Okay So that is the two ways of doing the scripts now I'm gonna run this both ways and make sure it works because it's possible that I've messed up printf Because I've never actually used printf 4 That was spur of the moment. I've done no research on it. So we're gonna comment this second part oops We're gonna comment the second part out make sure we're actually at the beginning of the line And then we're gonna save this and quit and we're gonna do dot slash Trasher dot sh and if I hit enter Trash emptied. Okay. I don't know why there's a a a Percentage sign at the end. We'll have to see what that is. So if we go now to if we go if we now if we Navigate to dot local Share trash and we do an LS. There's nothing in there. So that actually really worked Which is awesome, right? So let's see you back into the home director here And I'm gonna go through and move something to the trash think so there's something else in there So let me go do that real quick Okay, now if I do if I actually CD back into there again and do an LS I Now I have stuff in there again. So Let's clear out now. Let's try the second way of doing it So if we then back into our trash or script and we comment out all this stuff Once again, I'm not sure why there was a percentage sign after that trash emptied That has something to do with Printf and the way it works. I guess I don't know but anyways Let's go ahead and save this and make sure it works again. So save and quit and then dot slash trash.sh Again, and then we'll CD into here and do an LS and that actually didn't work Now that's an interest. That's interesting I'm wondering why it didn't work this time. So let's um, I Mean check my notes. Maybe I typed something wrong. It's entirely possible So let's see back here and them into trash.sh and make sure we did everything right So dollar sign home slash dot local slash share slash trash and and remove.rf Dollar sign home Local share trash and and oh, you know, did you did you spot what I did here? I didn't put the period in here Okay, so whenever you're doing scripting make sure you prove free because It just messes things up. Okay, so we'll now save this again and Now we'll do dots and then we'll CD into here and do an LS that worked. Okay So that right there is two ways of doing the script Which one is better in terms of you know bash scripting etiquette? I Don't really have an opinion on that. I think some people will say well This is the proper way of doing it. Some people will say that this is the proper way of doing it Personally, I think that they both work just fine Again, I'm not sure why that the the print f thing had a percentage sign at the end of it That might be something to do with lacquerty. I'm not actually sure But that is the entire script. That's all you have to do and except one line and You're done and there's no extraneous, you know sub processes or sub shells or whatever So I didn't go through and go crazy in terms of piping things into said this time So it actually worked just the way I'd hope it would you know outside of you know Not actually putting in the hidden file there. So So that is the script and I'm actually shocked that this video was this long. So There'll probably be some of it that I'll cut out But thanks for watching you can follow me on Twitter at the Linux cast You can follow me on Facebook at the next casting you support me on patreon at patreon.com slash Linux cast if you Would like to support the channel and you'd like early access to videos. That is something that happens occasionally now I I'd only manage it one time this week, which is disappointing. I'm trying to do two or three times a week for early access to for my patrons But it's just been crazy real life lately So that is definitely something that I'm going to be working better on patreon.com slash the next cast before I go I'd like to take a moment to thank our current patrons Devon Marcus Meglin Donnie Sven Merrick camp and Mitchell I'm pretty sure I did that without looking actually Devon Marcus Meglin Donnie Sven Merrick camp Mitchell Yep, I got that right. All right. Anyways, thanks for watching. I'll see you next time