Linux and Git Unplugged

Start your journey — Conquer your fear of the terminal

Amit Kumar
The Startup
13 min readSep 15, 2020

--

Image Source: Pinterest

Let’s face it. We’ve all faced mild criticism from our colleagues for being a “Non-Linux” user or for not using the command line, whether it be in the college or at our place of work :’). Some of us have become so used to the Windows OS that we fear the transition, as just the thought of remembering the Linux commands gives us chills or the sight of a blinking cursor makes us feel uneasy.

Why should I switch to Linux when I’m happy with the good old Windows OS? — You may think.🤔 Well…

How about the annoying and scary blue screen of death!?

Image Source: Pinterest

Not a good enough reason yet? Okay; I’m going to present some reasons that will help you with making your choice and may lead you to consider switching whether you’re a developer or not.

  • Linux is Open Source — Linux is released under an open-source license, which prevents restrictions on the use of the software, anyone can run, study, modify, and redistribute the source code, or even sell copies of their modified code, as long as they do so under the same license. And you’re probably thinking you’ve never paid a dime for an OS. You have. When you purchase a laptop or desktop, the cost of Windows is included and many of its applications have an associated cost too.
  • Linux doesn't get in your way — Windows is full of bloatware and begs for attention. It’s like the kid in school who desperately wants to be noticed and is borderline belligerent about it. “What would you like me to do,” cries Cortana. “Hi, please make Microsoft Edge your default browser. It is totally better than Chrome” insists the Edge browser. “Help us to help you! We can’t sign in to your Microsoft account!”. “Windows update is ready. Restart now!”. *Ignores*. A few seconds later, “Restart right now!”
  • Skill Development for Industry— For people studying to become computer professionals, having Linux skills is already highly desirable, and it could, in fact, eventually become virtually mandatory. With regard to careers, it is becoming increasingly valuable to have Linux skills rather than just knowing how to use Microsoft Windows. This is because the role of Linux will continue to expand — and many industry experts expect it to become the dominant operating system for some, or most, types of applications. Moreover, Linux skills are becoming increasingly important for many other fields as well, ranging from business to biotechnology to industrial design.
  • Linux is ideal for the Public Cloud — Due to its modularity, power efficiency, reliability, scalability to support critical workloads, security efficiency, Linux has become the natural technology for cloud computing. Having all these strengths as an operating system, it has a great influence on the market. All the major public cloud providers Amazon Web Services (AWS) to Microsoft Azure and Google Cloud Platform (GCP) use different versions of Linux.

Hmm… That sounds really convincing, doesn’t it?! Well, if you’re down for it, let’s get started with some basics of Linux.

Linux Basics

Let’s get started by hopping onto a Linux system. Here are some ways to begin:

Terminology Alert!!!

  • Root — The basic word used mostly everywhere. root is the parent folder of any Linux. It contains different folders such as usr, bin, mnt folders for containing different types of files, software, and user data. The root user has the superuser permissions and he can do any changes or modifications to files and permissions.
  • Sudo grants superpowers of a hero. In Linux, sudo is used for executing commands as superuser when you are logged in as any user. Sudo is created such as to take care of accidentally changing the core files. Sudo makes sure that core files are not accessed or changed by a user, changing may unstabilize or break OS. You need to use it wisely.
Image Source: Google Images
  • Absolute/Relative Pathnames — Directories are arranged in a hierarchy with root (/) at the top. The position of any file within the hierarchy is described by its pathname. Elements of a pathname are separated by a /. A pathname is absolute, if it is described in relation to root, thus absolute pathnames always begin with a /.
  • Home Directory — The directory in which you find yourself when you first login is called your home directory. More on Linux directory structure.
  • Directories . (dot) and .. (dot dot) — The filename . (dot) represents the current working directory; and the filename .. (dot dot) represents the directory one level above the current working directory, often referred to as the parent directory.
  • Symbolic link — A symlink is a type of file in Linux that points to another file or a folder on your computer. Symlinks are similar to shortcuts in Windows.

Now that you’re ready, here’s an overview of commonly used Linux Commands

  • man <command-name> is your best friend when you are wondering what a command does and how to use it.
  • echo prints a text of line provided to it.
  • pwd is to find out the path of the current working directory (folder) you’re in. The command will return an absolute path.
  • cdhelps navigate through Linux files and directories as cd <dirname>. You can go to your home dir anytime using (cd ~ or cd), where ~ indicates the home dir. To go to your last dir, you can use (cd -). To go the parent dir of the current dir, use (cd..).
  • lshelps to view the contents of a dir (ls <dirname>). By default, this command will display the contents of your current working dir. You can also add flags. Wondering which flags to use!? Remember your best buddy ‘man’. Try out ls -a, ls -R and ls -al. What do you observe?
  • mkdir <dirname> is used to create directories. You can specify the -p option to the mkdir command to create a parent directory.
  • touch <filename> is used to create files.
  • rmdir <dirname> is used to delete a directory. However, rmdir only allows you to delete empty directories.
  • rm <filename/dirname>is used to delete directories and the contents within them. If you only want to delete the directory — as an alternative to rmdir — use rm -r.

☣HAZARD💀

Any idea what happens when you give ‘sudo rm -rf *’? It is the code that deletes everything on the computer. rm means to remove, -rf flag deletes the entire folder forcefully. * stands for every file/folder on the Linux system.

Image Source: Reddit
  • cp <file/directory path> <destination path> is used for copying files and directories from one location to another.
  • mv <file/directory path> <destination path> is used to rename files or directories. It also moves a file or directory to another location in the directory structure.
Image Source: Google Images
  • chmod [reference][operator][mode]file...is used to change the access mode of a file. With the chmod, User U, Groups G, and Others O can be granted varying permissions such as Read, Write or Execute to files and directories. Read has a value of 4, Write of 2, Execute of 1. Add the values of required permissions and assign them in the corresponding position for UGO. More on file permissions.
  • grep [options] pattern [files]filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. Options and files args are optional. Some of the options description:
    -c: This prints only a count of the lines that match a pattern
    -i: Ignores, the case for matching
    -l: Displays a list of filenames only.
    -n: Display the matched lines and their line numbers.

Input-Output Redirection in Linux

  1. Overwrite
  • ”>” standard output; So whatever you will write after running this command, all will be redirected and copied to the “file.txt”. This is standard output redirection.
cat > file.txt
First time: Check your file after typing this in the terminal
cat > file.txt
Second time: Check your file after typing this in the terminal
  • ”<” standard input is to copy the input and give the output to the screen.
cat < file.txt

2. Append

  • ”>>” standard output; This is used when we want to append some lines to the existing content of the file.
cat >> file.txt
First time: Check your file after typing this in the terminal
cat >> file.txt
Second time: Check your file after typing this in the terminal
  • ”<<” standard input; What could be different with this? Test it out for yourself.
cat << file.txt

3. Piping is a form of redirection that lets you use two or more commands such that the output of one command serves as input to the next. You can make it do so by using the pipe character ‘|’. Pipes are unidirectional i.e data flows from left to right through the pipeline. Syntax: <command_1 | command_2 | command_3 | .... | command_N>

Here are some examples for you to try:

  • Listing all files and directories and give it as input to more command:
$ ls -l | more
  • Use head and tail to print lines in a particular range in a file: This will print the last 5 lines from the first 7 lines in the file
$ cat file.txt | head -7 | tail -5

Here are 20 amusing Linux commands to have fun with the terminal:

What is Cron?

The software utility cron is a time-based job scheduler. Cron schedules jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. You could set it to download a movie every Friday evening (Write a script to download the latest movie and assign it to cron).

Need for Bash shell scripting

A shell script is a sequence of commands for which you have a repeated use. This sequence is typically executed by entering the name of the script on the command line. Alternatively, you can use scripts to automate tasks using the cron facility. Here’s a lifesaver for you:

Git Basics

Image Source: Google Images

Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. So ideally, we can place any file in the computer on version control.

It allows one to revert changes, revert the entire project back to a previous state, review changes, see who last modified something that might be causing a problem, who introduced an issue and when, and more.

So, what is Git?

Git is a version-control system for tracking changes in computer files and coordinating work on those files among multiple people. A user “clones” a copy of a repository (a collection of files) and has the full history of the project on their own hard drive. This clone has all of the metadata of the original while the original itself is stored on a self-hosted server or a third-party hosting service like GitHub.

Terminology Alert!!!

Image Source: Crio.do
  • Working directory contains all the files you see on your project directory.
  • Staging index is a file in the Git directory that stores the information about what is going to be included in your next commit.
  • Master Branch: When a commit is made in a repository it’s added to the branch you’re currently on. By default, a repository has a branch called master. It is useful to create a separate branch that acts as a safe isolated environment from your last commit.
  • Local repository is the one on which we will make local changes, typically this local repository is on our computer.
  • Remote repository is stored on a code hosting service like GitHub or on an internal server that all team members use to exchange their changes.

Generate/check your machine for existing SSH keys

Why you ask? Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username or password at each visit.

Now that you’re ready, here’s an overview of commonly used Git Commands

Create or Clone a repository

  • Create a repository from scratch: git init
  • Clone an existing repository:git clone <https://github.com/>...
  • Clone repository and use different name:git clone <https://github.com/>... new_name

Branch

  • List all branches:git branch
  • Create a new branch:git branch <branch-name>
  • Delete branch:git branch -d <branch-name>
  • You cannot delete a branch you’re currently on.
  • You cannot delete a branch if it contains any commits that aren’t on any other branch.
  • To force deletion:git branch -D <branch-name>
  • Switch between branches:git checkout <branch-name>

View Information

  • Determine a Repo’s status:git status
  • Display a Repo’s commits:git log
  • Display a Repo’s commits in a compact way:git log --oneline
  • Viewing Modified Files:git log --stat
  • Viewing file changes: git log -p

Add

“Staging” means moving a file from the Working Directory to the Staging Index.

  • Staging Files:git add <file1> <file2> … <fileN>
  • Unstaging files:git rm --cached <file>...
  • Stage all the files:git add .

Commit

Take files from the Staging Index and save them in the repository.

  • Commit staged files (This will open the editor for commit message):git commit
  • Commit files without opening the code editor:git commit -m "Commit message"
  • Undo the changes made in a commit:git revert <SHA of commit>
  • Reset commit (This will erase commits):git reset <reference to commit>

Push

  • Sends the recent commit history from your local repository up to GitHub. git push origin <branch-name>

Fetch

  • git fetchThis will download the latest info about the repo from origin (such as all the different branches stored on GitHub). It doesn’t change any of your local files — just updates the tracking data stored in the .git folder.

Merging

  • git merge <branch to merge in> This will take all commits that exist on the other-branch-name branch and integrate them into your own current branch.

Pull

  • git pull origin masterIt grabs any changes from the GitHub repository and merges them into your local repository. As a shortcut, you can use the pull command to both fetch and merge all in one step.

Diff

  • See changes that have been made but haven’t been committed yet:git diff

It seems you’ve mastered the basics of Linux and Git. Here’s a challenge for you to try and a solution for the same.

Build your own Google Drive

Image Source: Crio.do

With the help of our knowledge on Linux and Git, the task is to simulate the working of Google Drive — backup and sync. Learn more about it over here:

I hope you made an attempt. Here’s the solution

Assuming you learned bash scripting, add this bash file to your repository.

Let’s say your files are located in /home/username/myGoogleDrive and we name the file below /home/username/myGoogleDrive/AUTOUPDATE.

#!/usr/bin/env bash

cd $(dirname ${BASH_SOURCE[0]})
DATE=$(date)if [[ -n $(git status -s) ]]; then
echo "Changes found. Pushing changes..."
git add -A && git commit -m "updated at $DATE " && git push
else
echo "No changes found. Skip pushing."
fi

Then, add a scheduled task, crontab to run this script as frequently as you want your GitHub to be updated.

  • It will check if there are any changes first and only commit and push all changes if there are any changes. Pretty efficient, right!?
*/60 * * * * /home/username/myGoogleDrive/AUTOUPDATE
  • This will run the script every 60th minute. Though you can set it as per your requirements.

Don’t forget to give this file execute permission with chmod +x /home/username/myGoogleDrive/AUTOUPDATE

  • This will always push the changes with the commit message of “updated <date-time>” with the exact date and time when the changes were committed.

Surely, this isn’t the only possible solution. There are several intuitive ways to go about implementing this.

Do you think it’s possible to implement this without crontab? Here’s a little hint for you:

You’re probably wondering by now how I conquered my fear of the terminal; how I managed to acquire the skills to pen it down in this blog. Well…

Time to roll down the curtain 🎭

Image Source: Crio.do

I was a part of the #IBelieveInDoing September Edition 10-Day challenge. It was a great opportunity for me to work on my weak Linux skills and polish them to the point where I was able to share what I learned through my very first medium tech blog.

Image Source: Crio.do

The challenge started off with this fun and interactive kick-off call where we were introduced to a whole bunch of amazing developers and participants. The challenge mainly had 3 parts to it. The first part was to learn the essential developer skills (Linux and Git) through a series of Bytes well presented on Crio.do. The second part was the bonus challenge which revolved around Simulating Google Drive using our knowledge on Linux and Git and the third part is the blog.

Thank you to the entire Crio family for a wonderful experience. I had a great time learning and I’m looking forward to more challenges. Kudos to all the amazing participants! :)

Cheers,

Amit Kumar

--

--