Skip to content

2. Minimal Linux for Git

Scott Campit edited this page Feb 21, 2020 · 4 revisions

There are so many Linux tutorials out there that cover introductory Linux usage much more concisely than I could, and you should definitely read other resources to learn more about Linux. However, I wrote an essentials only guide to using Linux for git specifically, so you can read this in 5-10 minutes and get to using Git immediately.

Moving through your file system in the Linux terminal

In any computer, folders (the technical term directories) are organized in a tree-like structure. As we go down the tree, we reach familiar directories like Desktop and Documents. Likewise, moving up the tree towards the root (often denoted with the back-slash symbol /) directory, we encounter folders that are essential to the Windows configuration that you generally will never modify (unless you absolutely have to).

To navigate and choose specific files for version control, we need to be able to move through our file system using Linux.

pwd - Print working directory

To see where you are currently working in within the Linux terminal, type in pwd.

# To view the current directory path we are in:
> pwd
/home/scampit

The left-most backslash is the root directory, containing all the important folders. home is a folder above root, and scampit is the user name folder. Each level is separated by another backslash.

While in Linux distributions, this would be our main directory, in Windows Subsystems for Linux, this folder does not contain our documents. Instead, we have to navigate to a directory from root, called mnt for mount. Your hard drives (ie C:) are mounted to the computer, and the data from these hard drives can be accessed from this directory.

cd and ls - change directory and list directory

To move up one directory, you can type in:

# To view the current directory path we are in:
> pwd
/home/scampit

# To move one directory up
> cd ..
> pwd
/home

cd stands for change directory, while . means the current directory. .. is the directory before the current directory.

To run a sanity check and see if we are in a different directory, you can use pwd to see if the path is different, or you can use ls to check the folders that are in your current working directory:

# To view the current directory path we are in:
> pwd
/home

# To list the directories and files that are in the current directory
> ls
scampit

This is expected! Since we were at /home/scampit, we moved up one directory to /home. In the /home directory, there is the file scampit that contains all of the user-specific data, including Downloads, Documents, Desktop, etc.

Now you're ready to move through your windows file system. Follow the tutorial to reach the Desktop folder:

# Move to the root directory
> cd /

# All the hard drives are mounted in the mnt folder
> cd mnt

# You should see the different hard drive positions. Mine is in the C:// slot:
> cd c

# We should now see a list of all directories belonging to this drive. We'll move into the Users folder:
> cd Users 

# We can now see a list of all registered users. I'll go into my own folder:
> cd scampit

# This is more familiar territory, where you'll see folders like Downloads, Documents, etc. Let's finally go to Desktop
> cd Desktop

So far, we have covered the pwd command for printing the current working directory, the ls command to list files and folders in the current working directory, and the cd command to change directories. Next, we will go over ways to make new files and folders.

File and folder manipulation commands

Rather than going into each command individually, let's try learning through example how the following code works for basic file handling. We will make an empty file in Desktop called test.txt, a subfolder to put the text file in called HelloWorld, and move test.txt into HelloWorld.

# Print current directory
> pwd
/mnt/c/Users/scampit/Desktop

# Make an empty subfolder called HelloWorld
> mkdir HelloWorld

# List current directory content
> ls
HelloWorld

# Make an empty file called test.txt
> touch test.txt

# List current directory content
> ls
HelloWorld touch

# Move test.txt into the folder HelloWorld
> mv test.txt HelloWorld

# List current directory content
> ls
HelloWorld

# List HelloWorld content
> ls HelloWorld
test.txt

# Re-name HelloWorld to GoodbyeWorld
> mv HelloWorld GoodbyeWorld
> ls
GoodbyeWorld

While this tutorial is by no means a proper introduction to using Linux, these are commands that will help you get started using Git. With the aid of Google, you can start to manage your Git/GitHub projects using the command line!

Summary

  • /mnt/c/Users/username usually contains our files of interest if you've installed the Ubuntu WSL.
  • pwd prints the current working directory
  • ls lists the files and folders in a directory
  • cd changes your current working directory
  • mkdir makes a new directory
  • touch makes a new empty file with the file name as an argument
  • mv moves files and folders with a given destination

Create shortcut commands using aliases

If you find yourself frequently using a long command (ie cd /mnt/c/Users/scampit/Desktop), you can create a new command that is much shorter. These new, shorter commands are called bash aliases.

In the /home directory, there is usually a hidden file called .bashrc that contains many configurable settings for the Linux terminal. To list out hidden files in linux, you can type a modified form of ls with the flag -a, which will show the hidden files.

> pwd
/home

# Show files, including hidden files
> ls -a
... .bashrc ...

The steps below describe the process of creating an alias variable called desktop as a shortcut for changing the current directory to the Desktop directory on your local Windows 10 machine. This will require us to use the nano text editor, which is a built-in Linux terminal text editor.

# Open .bashrc file using the nano command. Note that the '~' symbol is an alias for the 'home' directory
nano ~/.bashrc

# The terminal will change, and we will be able to see the contents of the .bashrc file. Scroll to the very bottom and type the following line to add a new alias command:
alias desktop='cd /mnt/c/Users/<username>/Desktop' # (insert your own username for <username>)

# To save and quit press CTRL+X, and then press Y+ENTER

# To re-load the .bashrc file with your changes, run the following line using the source command
source ~/.bashrc

Now you should be able to simply type desktop while in any directory, and this command should re-direct you to your Desktop folder.

Summary

  • .bashrc is a file in the home directory that contains the configurable options for the Linux WSL
  • nano is a built-in text editor for Linux
  • To make a new command, use the following syntax to edit the .bashrc file:
    • alias command = '{INSERT YOUR COMMAND}'
  • To reload the .bashrc file with your changes, you can use the source command.

Resources