Wednesday, April 8, 2015

Day 5: The Linux Command Line Ch1-3 Notes

Uff, that was quite a reading I must say. Now, here's the stuff to remember after first reading of chapters 1-3.

Chapter 1 - Learning The Shell

A bunch of useful commands (on the terminal type:whatis followed by the command below; for instance $ whatis date):

  • date - print or set the system date and time
  • cal - displays a calendar and the date of Easter
  • df - report file system disk space usage 
  • free - Display amount of free and used memory in the system 

Displaying current date and time:

pi@raspberrypi ~ $ date
Wed Apr  8 18:07:46 IST 2015

Displaying Calendar:

The option -3 displays current month, month before and next month.

Report File System Disk Usage (-h more human readable instead of byte count):

Display Free And Used Memory:

Chapter 2 - Navigation

  • pwd - print name of current/working directory
  • cd - changes directory 
  • ls - list directory contents
Question 2-1
How does Linux differ from Microsoft Windows regarding file system organization?

Windows has a separate files system tree for each storage device.
Linux (Unix-like system) uses single file system tree regardless of a storage device. They all are mounted (attached) at various points of the same file system tree. 

Question 2-2
From home directory you have entered: cd playground (playground is a directory).
  • Which symbol refers to the current directory?
    The current directory (ls -a) is marked with a period  . 
  • Which symbol refers to working's directory parent directory?
    The parent directory (ls -a) is marked with double period ..
  • Current directory is: /home/pi/playground. How to change current directory using relative path?
    $ cd .. (enter)
  • Current directory is ~/ (user pi's home directory). How to change working directory to 'playground' that has been added to user pi's home directory using absolute path?
    $ cd /home/pi/playground
  • What is the meaning of command: cd without any parameter?
    It changes directory to user's home directory
  • What is the meaning of command: cd -
    It changes directory to the previously entered directory
  • What is the meaning of command: cd ~
    It changes directory to user's home directory (just like cd alone)
  • What is the meaning of command: cd ~pi
    It changes directory to user pi's directory
Related to Bash navigation I accidentally bumped into this shortcut cheat sheet:

Ctrl + A
Go to the beginning of the line you are currently typing on

Ctrl + E
Go to the end of the line you are currently typing on

Ctrl + L
Clears the Screen, similar to the clear command

Auto-complete files and folder names

Ctrl + U
Clears the line before the cursor position.

If you are at the end of the line, clears the entire line.

Ctrl + K
Clear the line after the cursor

Ctrl + R
Let’s you search through previously used commands (repeat if the first match is not the command you want)

Ctrl + C
Kill whatever process your terminal is currently running

Ctrl + D
Exit the current shell

Ctrl + Z
Puts whatever you are running into a suspended background process. fg restores it.

Ctrl + W
Delete the word before the cursor

Ctrl + T
Swap the last two characters before the cursor

Esc + T
Swap the last two words before the cursor

Alt + F
Move cursor forward one word on the current line

Alt + B
Move cursor backward one word on the current line

Chapter 3 - Exploring The System

  • ls – List directory contents
  • file – Determine file type
  • less – View file contents
Less Cheat Sheet:

Page Up or b
Scroll back one page

Page Down or space
Scroll forward one page

Up Arrow
Scroll up one line

Down Arrow
Scroll down one line

Move to the end of the text file

1G or g
Move to the beginning of the text file

Search forward to the next occurrence of characters

Search for the next occurrence of the previous search

Display help screen

Quit less

Question 3-1
How would you display the content of user's home directory and 'python_games' directory assuming it is located in the user's home directory?
$ ls ~ python_game

Question 3-2
What is the difference in terms of the output between: ls -lt and ls -ltr?

$ ls -lt
Displays the content of current directory in a long format. Files and directories are listed from the most recent modification time the last modified files and directories.

$ ls -ltr
Displays the content of current directory in a long format. Files and directories are listed from the last modified files and directories to the most recent modified ones (bottom of the list).

Question 3-3
How would you display the content of /usr/bin directory sorted by the size in such a way that it displays file size in KB, MB, GB rather than in bytes?

$ ls -lh /usr

Question 3-4
Given the output below, describe every meaning of every field (fields are separated by white characters 'spaces'):
drwxr-xr-x  7 pi pi 4096 Apr  7 18:47 Documents

file type (d = directory, - means regular file) followed by user's, groups and other's permissions.

the number of symbolic link

pi pi
first user then group owner of a file or directory

size in bytes

Apr  7 18:47
Last modified

Name of the file/directory

Question 3-5
Why should we learn how to examine text files in Linux?

System setting files, scripts, code are all kept in text files. In order to understand those, it is necessary to learn how to explore text files.

Question 3-6
How can you copy a filename in terminal and how can you paste it using mouse?

Double-click the filename to copy it, middle click to paste it (press scroll wheel in case there is no third button)

Question 3-7
What is a sybolic link (symlink) and what is a benefit of using it?

Symbolic link allows to create a different filename for another file. While creating different version of programs we can link them to the same name. For instance myprog-2.1 version can be referenced by other program using symbolic link called myprog. While changing versions from myprog-2.2 all I have to do is to remove symlink myprog pointing to myprog-2.1 and create a new one myprog pointing to myprog-2.2. All other programs will still refer to myprog which now points to newer version. Also, we can keep previous version if we wanted to downgrade myprog version from 2.2 to version 2.1.

Tonight, read chapter 4 of "The Linux Command Line".