Unix Shell
Introductionβ
The shell is the interactive command line interface for Unix based operating systems. The user can type commands to accomplish certain tasks.
Printing a messageβ
To display a Hello Earth message in a Unix shell, type:
user@theprogrammingfoundation:~$ echo "Hello Earth"
note
Quotations are not necessary. However, if the message contains special characters, it will show errors. Therefore, using quotations is nice practice.
Clearing the screenβ
To clear the screen, type:
user@theprogrammingfoundation:~$ clear
Listing files and directoriesβ
To list all the directories and files, type:
user@theprogrammingfoundation:~$ ls
note
An alternative to ls is l. This works in many Unix and Unix-like operating systems because l is set as an alias of ls.
Navigating through directoriesβ
To go inside a directory, type:
user@theprogrammingfoundation:~$ cd Pictures
The shell prompt shows a / in the beginning of the current directory:
user@theprogrammingfoundation:~/Pictures$
To list contents of a directory without going inside the directory, type:
user@theprogrammingfoundation:~$ ls Pictures
Creating directories and filesβ
To create a new directory in the Unix file system, type:
user@theprogrammingfoundation:~$ mkdir new_directory
Multiple directories can be created using:
user@theprogrammingfoundation:~$ mkdir new_directory1 new_directory2
note
The shell shows an error if a directory with the same name already exists.
To create a file in an Unix file system, type:
user@theprogrammingfoundation:~$ touch file.txt
Multiple files with different extensions can be created using:
user@theprogrammingfoundation:~$ touch program.c program.h
note
Unix replaces the existing file with the new file, if they have the same name.
Adding and editing contents of a fileβ
Adding text in text file can be done using different programs such as, GNU nano, Vim or Emacs.
To create a file using the text editor Vim and add text, type:
user@theprogrammingfoundation:~$ vim file.txt
Add some texts, press [Esc], then type :wq to save.
note
In Vim w stands for write and q stands for quit.
Reading filesβ
To read a text file, type:
user@theprogrammingfoundation:~$ cat file.txt
Removing files and directoriesβ
To remove a file from the Unix file system, type:
user@theprogrammingfoundation:~$ rm file.txt
Directories can be removed using the flag, -r:
user@theprogrammingfoundation:~$ rm -r new_directory
Aliasβ
To set aliases for convinience using the command alias, type:
user@theprogrammingfoundation:~$ alias python="python2"
The string python becomes the alias for python2
note
This alias example is going to be helpful in the Python section.