 This video is part of a series, be sure to check out the description of this video for a link to the full playlist. In the previous video, we used the program command rename to remove some spaces from file names. Today we're going to do the same thing, but since the rename command is not on all systems, but the said command tends to be on a majority of systems, we're going to use said to accomplish the same thing. It's going to be a longer command. We're going to have to use a while loop here. I'm going to do it all on a one-liner, but then show it to you in a script. Again, if I list out here, you can see I have a number of files with spaces in the name, which is a bad habit to get into, and to make it even clearer, I'll put it in a list so you can actually see where one file ends and another begins, because it's hard to see up here. You've got two spaces between file names, but it's just, it's hard to see. It's another good reason not to put spaces in your file names. So let's go ahead and start. So we know we can do list to list out our files, and then we can do list, and we can put that into a while loop. So we're going to loop through the output of our list command, so each file name. We're going to say to read each file name, and then we've got to give it a variable that we're going to use. It can be anything. I'm going to call it file, since that's what we're working with. Then semicolon and do. So this is what we're going to do. Anything between the do and the done is going to be looped over for each file name. So now you may know if there's no rename command, how do I rename files? Well, traditionally, and especially just in most cases on a Unix or a Unix like system such as Linux, you just use the move command. You move one file to the same folder it's in, but give it a new name. So we're going to loop through this using the move command. We're going to say take our file that we're looping through one at a time, and then we're going to rename it. So before we actually start moving, let me just go ahead and say echo, just to make this a little clearer. So here, I'm just going to echo out each file name, boom, boom, boom, boom. Okay. So now what I want to do, let's say I want to rename or get that name of that file without the spaces. I'm going to echo out the file name, and I'm going to pipe that into set, and I'm going to say dash E. And then inside my single quotes here, I'm going to go S, 4 slash 4 slash 4 slash G. S means substitute, G means global, just as it did with rename. And here I'm going to say backslash space, and then this area I'm going to say underscore. If I enter here, you'll see what happens. So we're taking the file name, and we're putting it through said, and we're saying substitute all spaces. So the backslash is saying, look at this space, it's a little different than when we were using the rename command. And replace all spaces in that file name with an underscore. And that's what we did here. But we have not renamed our file yet. We just, if we list out, you can see our files are still have their spaces. We just got what we want to rename them to. So now we need to do is take our file and rename it to what we've generated here. Now, if you put a command inside quotations, dollar sign, and parentheses, it's saying to just display the output of that command. Here I hit enter, it's saying, OK, you tried to run a command called this. So that's what we did there. But we want to use that inside a command. So here I'm going to say, MV for move, dollar sign file, which is our file name, and then our output here. So let's look at this again. So we're saying, OK, list out our files, we're doing a while loop, and we're reading each file name and putting the name of the file into a variable called file. Then between the do and the done, we're looping. So everything between the do and the done is being done each time, one at a time for each file name. And we're going to say, move the file name, dollar sign file, because we created the variable here and the dollar sign indicates a variable. And where are we going to rename it to? We're going to rename it to whatever's between this quotation mark and this quotation mark, which is going to be the output. So the dollar sign parentheses means the output of these commands. And as we saw, the output of those commands is taking the file name, piping it into said and replacing all spaces with underscores. So if I hit enter now, we can list out and you can see that we have renamed all of our files with underscores. Now again, be careful while doing this because you can have, you might end up accidentally deleting some files if you don't do things properly. And I am going to now show you how to write this all out inside a script. So let's go ahead and I'm going to highlight my code here. I'm going to go into Vim in my text editor here, and I'll call this my rename, sure. And I give it .sh. It doesn't need the .sh. What really tells it what it is is the shebang line. This is a shell script. I use center click after highlighting to paste, or you can right click, copy right click paste seems like overkill to do. I've talked about that in previous videos. I'm going to say do move and done. So this is a little bit cleaner, easier to read. So we're listing, looping through file name, we're moving into this. So I can save this. Now the current folder we're in, we already have renamed these files. I'm going to move my script up one directory, and I'm going to move up one directory. And if I list out here you can see that I have my file names with the spaces still in them. And I also have my shell script here. So what I'm going to do is I'm going to say change mod plus x to make my script executable to give it permission to run. It's a security thing. You don't want random programs running on your computer. If you wanted that you would be running Windows. So now you're like, okay, I don't want to have to type this every time I want to rename files. Well, now we can call this script. Now I'm running the script from within my current directory, but I can put it in a certain place which I'll go over in a future video in a system path in a folder that I can call from anywhere. So just like I can call grep or said without doing forward slash, if I put this script in certain places on my system, I can call it without having to type forward slash each time or have it in that directory. But for simplicity at this point, let me clear the screen. I'm just going to dot slash and run the name of my script. I'm going to hit enter. And you know, one thing it said that it couldn't move the directory to itself. And we couldn't rename the current script because it's the same. So any file that doesn't have spaces in the name, it's going to give you that output saying I wasn't able to rename it because it's already called that is basically what that says. But if I list out all my files in this directory, any of them that had spaces now have underscores. And so now that you've created the script, this command, you can put it in a function or leave it in a script, call it anytime. And you can easily quickly rename all files that have spaces in your current directory and remove those spaces and replace them with underscores with one simple command. So I thank you for watching. I hope you found this useful. And if you did, be sure to like, subscribe, comment, and share if you think about becoming a supporter over at patreon.com forward slash milx 1000s. There's links to that as well as my website filmsbychrist.com. That's Chris the K in the description and a link to the full playlist in the description. Thanks for watching. Have a great day.