The Ghost in the Shell awakens. Prepare to Bash or be trampled by him.
Let's Bash that penguin.
Okay, so enough of that number system stuff, complements and all that.
Code Chronicle is presenting to you: TUX - the penguin. Literate beings refer to it as Linux.
Don't faff around... get your Hoodies on, and dive in!
Tux's bio
The "OS" drama began with Unix.
UNIX is an operating system — a collection of programs that act as an interface between a user and a machine. [The term "interface" means a place where two independent systems (human and machine) meet and communicate with each other.] It was developed in 1969 at the AT&T Bell labs.
Linux is a variant of UNIX.
An OS comprises a kernel and a shell. A kernel is just a bunch of programs that allocates and coordinates the hardware resources. We can interact with the kernel (in turn control the hardware) through a program called shell, like Bash (Bourne Again Shell) for Linux. Bash (or any other shell) offers a plethora of commands that we can utilize to make the computer do something.
N.B. Commands come with options, generally appended after its name with a hyphen. Simply put, options modify the scope of a cmd, e.g., the ls
cmd lists the contents of a directory, but ls -l
gives a detailed list of the contents.
Bourne Again!
The following is a labelled snapshot of Bash shell:
** I'm using Ubuntu WSL, one of the many flavors of Linux.
** If there's a #
character instead of a $
after the ~
, it means the user has "root privileges."
Your wish is my command!
I'm discussing a few of the 'interesting' bash commands, ignoring the usual ls
mkdir
cd
rm
etc.
Keep pushing
Essentially, pushd
and popd
are navigation commands in Bash. In a way, they allow us to “bookmark” directories, so that we can get into them faster.
pushd
At a basic level, pushd
works the same as cd
, it's just that pushd
also echoes the current and the last working directories.
It creates something called a stack. A stack refers to a collection of elements, which, in this context, are directories visited via pushd
. Every time we change our working directory, it's added to the stack along with the previous one.
Create a folder-tree, say 5 levels deep: ~/one/two/three/four/five
. cd
into one
, and then use pushd
to move to four
. It echoes the final and the initial locations, as shown below:
:~/one$ pushd two/three/four/
~/one/two/three/four ~/one
Now let’s jump to two
, then to five
, and back to three
. The last dir change echoes all the dirs visited using pushd
, which means we’ve created a stack.
:~/one/two/three/four$ pushd ../..
~/one/two ~/one/two/three/four ~/one
:~/one/two$ pushd three/four/five/
~/one/two/three/four/five ~/one/two ~/one/two/three/four ~/one
:~/one/two/three/four/five$ pushd ../..
~/one/two/three ~/one/two/three/four/five ~/one/two ~/one/two/three/four ~/one
To view the stack, use pushd +0
or dirs -v
. The current working directory is at the top of the stack.
:~/one/two/three$ dirs -v
0 ~/one/two/three
1 ~/one/two/three/four/five
2 ~/one/two
3 ~/one/two/three/four
4 ~/one
Now to move into dir one
(say) at the bottom of the stack (index 4), enter pushd +4
. The working dir (one
) is at the top of the stack, followed by three
– the last location.
:~/one/two/three$ pushd +4
~/one ~/one/two/three ~/one/two/three/four/five ~/one/two ~/one/two/three/four
:~/one$ dirs -v
0 ~/one
1 ~/one/two/three
2 ~/one/two/three/four/five
3 ~/one/two
4 ~/one/two/three/four
popd
The stack is, obviously, mutable. Just like we can add dirs to it, we can also remove dirs by using popd
. Consider the above stack. To remove five
(say) from the stack, enter popd +2
, 2 being the index of dir five
.
N.B. Using popd
like so doesn’t change the working dir.
:~/one$ popd +2
~/one ~/one/two/three ~/one/two ~/one/two/three/four
:~/one$
When used without options, popd
removes the topmost (0th) item of the stack and changes the working dir to the next element in the stack.
:~/one$ popd
~/one/two/three ~/one/two ~/one/two/three/four
:~/one/two/three$
Node.. not JS
In addition to its name, every file in a file system has a unique ID, called the inode number. It refers to the physical file, the data stored in a particular location. The command to view the list of files in a dir with their corresponding inode numbers is: ls -li
N.B. The first column in the list contains the inode numbers.
Soft links
We're familiar with "shortcuts" on Windows. If the shortcut to a file or folder is deleted, the file remains unchanged, whereas if the file’s removed, the shortcut exists but it’s inert. On Linux, we have soft links, which are essentially shortcuts.
A soft link (also called symbolic link) contains the path to the original (parent) file; it’s a pointer. It has its own inode number, as it refers to a file by its path.
ln -s <parent> <shortcut>
(cmd to create a soft link)
$ ln -s new.txt newSoft.txt
$ ls -il
...
515 lrwxrwxrwx 1 atanu atanu 7 Jan 10 11:56 newSoft.txt -> new.txt
...
** Soft links to the same file are of the same size.
** A soft link can point to a file that doesn't exist.
Hard links
A hard link is like a new name to an existing file. All the hard links to a file are names of equal importance, as they share the inode number of the original file.
cmd: ln <OG> <new>
N.B. The 2nd column in the stdout of ls -l
shows the number of hard links to a file.
$ ln dummy.txt dummyH.txt
$ ln dummy.txt dummyH2.txt
$ ls -li
...
11011 -rw-r--r-- 3 atanu atanu 142 Jan 12 19:47 dummy.txt
11011 -rw-r--r-- 3 atanu atanu 142 Jan 12 19:47 dummyH.txt
11011 -rw-r--r-- 3 atanu atanu 142 Jan 12 19:47 dummyH2.txt
...
** It’s not possible to create a hard link to a directory.
** If the “original” file is deleted, we can still access its data through the hard links, which is the primary benefit of having the same inode.
That's it for this one.
Thanks for the visit😊
Follow me on X