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 up the tree, we reach familiar directories like Desktop and Documents, while moving down towards the root 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.

> 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 in root, called mnt for mount. Data within different hard drives are in this directory.

cd and ls - change and list directories

To move up one directory, you can type in: > cd ..

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:

> ls
home

This is expected! Since we were at /home/scampit, we moved up one directory to /home.

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

# Move to root
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

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:

# Make an empty subfolder called HelloWorld
mkdir HelloWorld

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

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

While this tutorial is by no means a proper introduction to using Linux, these are commands that will help you get started using Git, and hopefully, 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

Bonus

Create shortcut commands using alias in .bashrc

If you find yourself frequently using a long command, you can create a new alias variable in a file called .bashrc. This file is always located inside your home directory (you should be able to see it by typing ls -a). The steps below describe the process of creating an alias variable called desktop as a shortcut for changing the current directory to the local machine Desktop:

# Open .bashrc file using the nano command
nano ~/.bashrc

# Scroll to the very bottom and type the following line
# (insert your own username for <username>)
alias desktop='cd /mnt/c/Users/<username>/Desktop'

# Save and quit (CTRL+X then Y+ENTER)

# Implement changes by running source command
source ~/.bashrc

Now you should be able to simply type desktop and this command should re-direct you to your Desktop folder.

Resources

Clone this wiki locally