Changing the File System

Changing the File System

Learn how to change your file system.

ยท

6 min read

Introduction

In the previous article, you learned how to navigate your file system.

Now that you know how to navigate the file system, you can start changing the file system.

In this article, you'll be making your own (mini) folder system. Yes, this is where the food-based teachings come in. ๐Ÿ˜

NOTE: You might want to be at a computer or laptop (to get the full experience).

mkdir

Let's start with mkdir. Use this command to create a new directory (aka folder).

Imagine that you're a baker who wants to plan out their menu and offerings. You figure the best way to do that is with a Git Bash project.

To start your Git Bash project, let's create a new directory.

NOTE: Make sure that you're in your home directory first. You can do that by typing cd into the terminal.

Since you'll be making a bakery-based folder tree, name your directory Bakery, like so:

mkdir Bakery

Now list the contents of your root directory using ls - Bakery should show up! You may need to scroll up to see it.

Mini-Test: Use what you've learned to create five more directories inside of Bakery. Name them each Cakes, Cookies, Cupcakes, Miscellaneous, and Pies.

rmdir

You then decide not you don't want to sell cupcakes. They're way too much work lol.

Perfect time to learn another command: rmdir ๐Ÿ˜„

You can use rmdir to remove directories.

First, verify that you're on the same path as the directory you'd like to delete. If you don't remember how to do this, see the cd and pwd sections of the previous article.

Then, remove the directory like so:

rmdir <directory-name>

In this case, make sure you're in the Bakery directory. Replace with Cupcakes.

Now list the contents of Bakery - Cupcakes should be gone! ๐Ÿง

NOTE: You can also use rm -rf to remove a directory with force (if it just wants to be stubborn).

touch

Since that's sorted out, you can start adding files. You decide to make each file a different recipe.

Creating new files is very easy, just do something like this:

touch <filename>

First, verify that you're in the directory you want to add files to. Let's start with Cookies.

Then add the files using the method shown above.

Mini-Test: Use what you've learned to complete the tree! You can even add real recipes to each file if you'd like. If not, please at least add some random text to each file - it'll play a key part later on. ๐Ÿ˜‰

rm

You decide that sugar cookies aren't a good idea (you haven't perfected them just yet), so you decide to delete sugar.txt from your plans.

Files can be deleted like this:

rm <filename>

First, verify that you're in the directory you want to delete files from. In this case, make sure you're in the Cookies directory.

Then delete the files using the method shown above.

Now list the contents of Cookies - sugar.txt should be gone! ๐Ÿช

NOTE: You can also use rm -force to remove a file with force (if it just wants to be stubborn).

mv

Moving out of our Bakery directory (for now), let's learn about mv.

This is a special command, that allows you to move AND rename files, depending on the arguments supplied. The table below demonstrates this:

UsageFunction
mv [directory] [existing directory]moves [directory] into [existing directory]
mv [directory] [new directory]renames [directory] to [new directory]
mv [file] [directory]moves [file] into [directory]
mv [file] [existing file]replaces [existing file] contents with [file] contents and deletes [file]
mv [file] [new file]renames [file] to [new file]

Mini-Test: Start by typing cd into the terminal (which takes you to your root directory). Then, create at least two new directories and at least two new files, naming them per your choice. Now, play around with the directories and files, using the table as a guide.

NOTE: Again, all of this should be done outside of the Bakery directory. You won't be using that again until the next article.

cp

Now that you know how to use mv, let's move on to cp. This command copies files and directories, and "pastes" them wherever the argument passed allows.

This, again, depends on the arguments supplied. The table below demonstrates this:

UsageFunction
cp [file] [existing file]copies [file] contents into [existing file]
cp [file] [new file]copies [file] contents into [new file]
cp [file] [second file] [existing directory]copies both [file] and [second file] into [existing directory]
cp -r [directory] [existing directory]copies the whole [directory] into [existing directory]
cp -r [directory] [new directory]copies [directory] contents into [new directory]

There are a few things to keep in mind about the above table: - when I say [existing file] or [existing directory], I mean a file/directory that was on your local machine before calling the command - cp [file] [new file] and cp -r [directory] [new directory] end up creating two identical files/directories - when it comes to cp [file] [second file] [existing directory], all parties MUST be existing - cp will not create them - when using cp -r [directory] [existing directory], the [directory] will become a sub-directory to [existing directory]

Mini-Test: Start by typing cd into the terminal (which takes you to your root directory). Then, create at least two new directories and at least two new files, naming them per your choice. Now, play around with the directories and files, using the table as a guide.

Lastly, here are some random (but helpful) commands to add to your toolbox. Again, but for the last time (I promise!), I present to you a table:

CommandUsageFunction
echoecho {filename} or {equation}prints file contents (or equation answers!) to the terminal
catcat {filename}reads, creates, and/or concatenates files
lessless {filename}reads files (without the risk of accidentally changing them)
clearclearclears the terminal (๐Ÿคฃ)
historyhistoryshows the history of your BASH commands
help{command} --helpbrings up a help menu for {command}

Mini-Test: Play around with these commands in your terminal!

Conclusion

In this article, you learned some commands to help you start changing the file system, like: - mkdir (used to create directories) - rmdir (used to delete directories) - touch (used to create files) - rm (used to delete files) - mv (used to move AND rename directories and files) - cp (used to copy directories and files) - ...and some random (but helpful) commands

Great job on completing the third part of the series!

This is the 3rd part of a 10 part series to teach Windows users about Git Bash. This series includes:

  • how to install Git Bash

  • how to navigate your file system

  • how to set up Git and GitHub

  • how to connect Git and GitHub

  • how to keep up good practices in Git

  • how to backtrack in Git

  • how to branch in Git

  • ...and more!!

Stay tuned for more - a new part will come out every Tuesday! ๐Ÿ˜Š

ย