 One of the most common file management tasks that you will do on your Linux system is renaming files and Renaming a single file is rather a simple task Most people know how to do this both at the command line or in your graphical file manager But what happens when you need to do more advanced file renaming activities? For example book file renaming or maybe you want to do some file renaming on Search patterns and regular expressions and things like that. Well today I'm gonna show you several methods for doing file renaming all of these methods I have used in the past. So let me switch over to my desktop and let me go ahead and open a terminal and Let me zoom way in here And let me switch to the bash show because by default my terminals open up in the fish show And I may do a little bit of simple scripting So it's good for me to go ahead and just switch over to bash now I am in my home directory an LS my home directory and what I want to do is I want to create a test directory to play around in So I'm gonna run MK DI R space test for make directory test and now I've made that directory Let me CD into test I do it LS of course. It's an empty directory. Let me clear the screen So now what I want to do is I want to create a bunch of files that we can play with and do some renaming stuff With so I could use the touch command and do something like touch space one dot txt Two dot txt or you know, whatever But if I'm gonna create ten hundred a thousand files That would be tedious a better way if I'm gonna do consecutive numbers would be to do something like sequence ten And it gives me a sequence of numbers one through ten what I could do is I could just pipe that into XR So I'm gonna do XR space dash capital I space the opening and closing curly braces space and then the command to actually Create those files which again is the touch commands I'm gonna do touch space and then the opening and closing curly braces dot Txt so that should create one through ten dot txt for us Let me LS just to verify now the most basic renaming of a file is renaming a single file And typically what most people would do is they would use the move command MV So you could do MV for move space the file you want to move so in this case I'll move one dot txt So I'm gonna move it to a file that doesn't exist Just give it a name that doesn't actually exist like 11 dot txt and what it does it moves one dot txt to 11 dot txt it essentially renames it if I do an LS you see one Txt is not there 11 txt is now there Now what I just showed you there with the move command that works when you're moving a single file So you could move and then the source file space and the destination So the new name essentially is the way that command typically works But what if you have a whole bunch of files that you want to move for example, maybe you had You know three files that you wanted to move well in that case the very last Argument doesn't need to be a file name. You need to actually give a directory to move those So assume I had a sub directory in this directory called best That's how you would end that command if you were moving multiple files Now I assume most people probably know about the move command It's a really simple command But where things get interesting is when you use move in other tools For example, I could use the move command and a for loop and that's what I think I want to do I think I want to use bulk renaming using a for loop So the way a for loop works in bash is you do for and then a variable it could be anything I'm just going to use x and just pick a character. I'm going to do for x in and then I'm going to do asterix dot txt so for every file in this directory that ends in dot txt and then semicolon and then do space and then whatever command you want to run on each of those files and then Semicolon done. That's the basic format of a for loop So let me just take this example and replace command with an actual command Let's do mv move and then I'm going to do space dash dash now the dash dash works for a move It actually works for most bash commands dash dash signifies that hey Everything after this is not a flag or an option because some of our file names that we're moving They could include dashes they could include things that the move command might interpret as flags or options So by giving get the dash dash we're letting it know hey everything that follows is not a flag or option And then what are we moving? Well, I think we should move x right so we assigned x as the variable So we're going to move x and then I'm going to do double quotes and then dollar sign and then opening and closing Curly braces here and inside the braces. I'm going to go ahead and do x parentheses dot txt and what I want to do is I want to rename All the files that end in dot txt to dot txt So let me do done and if I do an ls you see that is exactly what happened Now that is just one method of doing this the for loop I think is the simplest method a lot of people like using the find command because the find command is a basic good new utility And it allows you to pass on the results of the find command and execute that as an argument in another command For example, let me do find in this directory And I'm going to do dash name. So we're going to find by name and we're going to find asterix dot txt because that's what all the files are ending in now And I'm going to do space exact space sh for shell dash c And then we're going to pass along the following command. So we're going to do it x equals and then let's do the braces again The braces are going to need to be double quoted and then a semicolon and then move space dash dash space You know a lot of this actually it's just basically the for loop, right? It's very similar. It's just a different method Again the find command that we're doing right now and the for loop method Very similar. So this time we're going to rename dot txt back to dot txt and the ending double quote the ending single quote for that whole command And then finally we need to do the backslash and then the semicolon and if we formatted that correctly and I did a ls You see we actually renamed everything back to dot txt again by using the move command in conjunction with the find command Now the find command here. It looks a little more convoluted. I think most people can grasp Exactly what the for loop was doing what this is doing. It's finding a pattern That's essentially the find command right there find in this directory everything with the name Anything that ends in dot txt and then the rest of it is execute the shell command, right? And then everything in these single quotes here That is the shell command to actually execute. Now, let me clear the screen Some other methods for doing some bulk renaming would be to actually use the rename command Now there are two very common commands rename commands on linux. So you need to make sure Which rename command your linux distribution uses because the one here on arch if I do a man Rename is rename one and this is a simpler Rename tool the more common one that's available on most linux distributions Is one that's actually written in pearl on arch. It's actually called pearl dash rename You have to install that particular program rename is usually installed by default, but pearl Rename I forgot to add the man to it. Let's do the man on the see it's really just rename Is also the name of that command but on arch you get you actually have to type pearl dash rename And this is a lot more powerful of a tool than the other rename tool a lot more flags and options And it allows you to do some pearl expressions. For example, you can do some substitutions and some translations Let me quit out of that. So let me show you this simple rename tool the one that was here by default The one that's not the pearl rename the way this works is you do rename and then some pattern and then The change you want to make happen and then The name of the file for example dot Txt for example ashrics dot txt right is how that would work So in this case what I'll do is the pattern we want to search for is Actually txt because that's the only pattern I think that's in all the files and we want to change that to capital txt And if this works correctly and let me ls You see we just changed all the files to dot capital txt now for those of you that are using a distribution that uses the pearl rename tool How that would work is you would do Just the word rename. I've got a type pearl dash rename But on most distributions it'll just be rename and then how this would work is you know It's going to look like a lot of the standard like said substitution commands And you know your vim substitutions and things you'll do single quotes s for substitution slash And then the pattern to search for i'm going to do capital txt slash And then what we want to change it to lowercase txt and then the ending slash and then we need to tell it What files to do this on once again i'll do asterix dot capital txt And then if I do an ls you see we changed everything back Now let me remove everything in this directory So i'm going to do a backslash rm because I have removed a list to always Ask me for confirmation. I don't want to do the confirmation So i'm going to do a rm and i'm going to do asterix for everything in this directory ls It's now an empty directory and then i'm going to do uh That sequence command that I did earlier. So this time instead of creating one through 10 dot txt I'm going to add a word before that we'll do foo One through 10 dot txt if I do an ls So now if I up arrow back to the pearl rename and once again we'll match Dot txt, but now let's Change the patterns obviously if you wanted to change foo to bar You know you could do that and if you wanted to instead of a Substitution you could actually do a translation. So instead of s do y And what a translation is it finds every instance of that character and changes it for example If I wanted to change every a and x to capital a capital x And do an ls You see the a and bar is now capitalized the x and txt is now capitalized and you could get even more creative here What you could do is you know, I could give it a range for example I could tell it to translate every lowercase a through z and translate that to capital a through z And if I do an ls we just capitalized everything in those names And of course getting back to the lowercase version is rather simple in this case We'll just reverse the pattern capital a through z and then lowercase a through z And we might need to make sure that it's searching for everything that ends in capital txt And now everything is back to right So those were several command line ways of doing some renaming of files Now some other things that you could do is you could actually use a terminal file manager that allows you to do bulk renaming For example, I like vifm. So this is a file manager that allows bulk renaming So for example, I'm in the test directory here what I could do a simple file manager, but it takes vim commands So if I did shift v for visual mode I'm in visual mode now. I just go and select all the files So I'm going to select all 10 of those files and if I hit c for change, you know standard vim command Then it's going to ask me what to do next Do I want to change file permissions if so hit p or do I want to rename the files if so hit w So I'll do w and now I am in a vim buffer And what I could do is I could do control v to get into visual character mode And I could just select bar and then hit c and I can name that back to foo hit escape hit write Hit colon q to quit and I've bulk renamed all of those inside the vifm file manager. Let me q to quit out of vifm another thing I could do is I could actually do all of this in emacs for example I could let me get into the test directory here in dear ed Which is the file manager inside emacs So inside dear ed what I could do is I could do a shift v to get into visual line mode because I'm using Evil mode here in dear ed and then once I select all the files that I want to move for example I could do em to mark the files So what dear ed does when you type em it marks a file for you to run a command on it later For example capital r shift r is rename. That's essentially Like doing a bulk mv. It's going to ask me. Hey these 10 files that you marked Where do you want to move them to and then you just Pick a new directory to move those 10 files now. I'm going to decline that I'm not going to move those Let me unmark these files. Let me get to the top here and just do you you you you you you you So that is a unmark command And of course if I wanted to you know, I could select blocks of Text here inside dear ed for example I could select all the foo here and if I wanted to see for change And then once again, I'll type bar hit escape and I've changed all of those inside this dear ed buffer inside emacs So those are just several different ways that you can rename files on linux I've used all of these in the past and of course, I was only manipulating 10 files in this case What happens when you're dealing with hundreds or thousands? That's where these tools really come in handy because you don't want to individually have to rename 100 different files Or a thousand different files. That's where throwing something in a for loop Really comes in handy or if it's a simple edit like everything's named with the same pattern Then doing it inside at emacs buffer or a vim buffer really makes sense Now before I go, I need to thank a few special people I need to thank the producers of this episode devon gave james matt michael mitchell paul scott west alan armadrag and chuck commander angry Diochai delin george lead linux ninja maxim michael yon alexander peace archon fedora polytech red prophet Stephen and willy these guys they're my high steered patrons over on patreon without these guys This quick tutorial on some book renaming tools on linux It wouldn't have been possible the show is also brought to you by each and every one of these fine ladies and gentlemen All these names you're seeing on the screen These are all my supporters over on patreon because I don't have any corporate sponsors It's just me and you guys the community you like my work You want to see more videos about linux free and open source software command line utilities? Please subscribe to distro tube over on patreon. All right guys peace Emacs the answer is always emacs