go to top

Venturing into the world of the command line…

Today at HackerYou we learned how to work with git in the command line (we also played around with a GUI yesterday, but that was far less fun). While we were doing so, we shared around this post from a HackerYou alum that everyone really enjoyed. It takes you through the initially daunting process of taking your command line from boring to awesomely tricked out. So I did that (I’m using the sorin theme). Great. But then I got home and told myself that I was going to do as many things possible in the command line and only the command line. Every time I didn’t know how to do something (make a new file, open a file, move a directory, etc etc etc) I’d look it up and figure it out. So I did, and that was awesome too. I had previously done some sort of “learn the command line” online course, so I got bit with the bug of wanting to push myself further. I then realized my shell said “zsh” and not “bash” and I wondered why.

…oh my goodness. So turns out zsh is not just some neat thing that lets me customize the shell with pretty (and useful) colors. I feel kind of silly now, admitting that that’s what I’d assumed, but that’s ok! Because now I know it’s in fact a totally different shell with lots of added functionality that I can play around with! This is also really important to know for two reasons: 1. I had no idea what a “shell” was until I got this comparison point and 2. lots of commands aren’t going to work in zsh when they would in bash.

What I learned:

How to Change Filenames to Lowercase Letters

After playing around with moving and creating new files and directories, I quickly moved on to my first task: taking all the files in an entire directory and changing every filename to all lowercase letters.

  1. Open up your command line (again, I’m using zsh for this), and navigate to the directory that contains the files you’d like to make lowercase. You use “cd” to do that. For example, if I wanted to change all the filenames in my “Cats” folder, which is on my desktop, I’d type: cd Desktop/Cats and hit enter*
  2. Now that you’re in the directory you can change the filenames. If you want, type ls and hit enter to see all of the files you have in that directory, just to be sure.
  3. To change the filenames, we’re going to use this friendly little zsh command called zmv. To use it, type in autoload -U zmv.
  4. Next, while you’re still in the directory of the files you want to change (type pwd and hit enter to make sure) type in the following: zmv ‘(*)’ ‘${(L)1}’ and hit enter.
  5. That’s it! All the filenames in your chosen directory should now be lowercase only. To check, type ls and hit enter again. If it didn’t work, most likely you accidentally applied the command to a different directory (probably the parent of the one you intended) or you need to double check that you typed in the command exactly as it’s written here.

There are some INSANE file renaming things you can do with zsh, it all gets pretty complicated, but another that I absolutely loved (since it drives me nuts to have to type a slash and space every time there’s a space in my silly file names) is replacing those spaces with a dash, or if you prefer, an underscore (and…I’m guessing probably whatever else you like if you’re crazy pants). So next up:

How to Replace Spaces in Filenames with Dashes

Once again, this is zsh specific.

  1. Just like before, use cd to target the files in the directory you want to change (eg. type in cd Desktop/TestFolder/SubTestFolder and hit enter). Type ls and hit enter to be sure you’re going to be editing the filenames you intend to.
  2. Next, also in your zsh shell, type the following: zmv ‘* *’ ‘$f:gs/ /_’ if you want UNDERSCORES to replace the spaces in your file names. See the little underscore right before the final ? Change that, and only that, to whatever symbol you want to replace the spaces with (I would highly suggest only using a dash if anything).
  3. Press enter. To check that your filenames have been changed, once again type ls and enter, and you should see the list of your files, with the words divided by dashes (or underscores, as you chose). For example, a file I had that was previously named “boo dogs.txt” will now be named “boo-dogs.txt”

That’s it! Congrats! If everything went swimmingly, you now have lowercase, dash-divided file names of great beauty and wonder. If anything went wrong, ping me at @AgentEmily and we’ll see if I can help. Or just google around and try to fix it!

Suggested Resource: definitely, definitely check out the awesome Commandlinefu.com for a whole bunch of commands you can use. And there’s an upvoting/downvoting system ala reddit or Stackoverflow, so you know when one command is probably better than another. They even have a twitter for all of the commands shared, and specific accounts for only those posts above 3 and 10 votes! How amazing is that!

*note: if your directory name contains (a) space(s), for example the folder name “Cats Rock”, you’d have to type cd Desktop/Cats Rock

I’m interested in hearing about front-end internships and jr. dev positions in companies that share my values, and I’ll be available for work in late June! Let me know if you have something in mind for me at eeporta@gmail.com.

This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.