bash - console


Bash - Console

Behind the graphical interfaces is the console. The console is the most basic way of communicating with the operating system. (The console in the past was a keyboard and a teletype machine. At that time, operators would type a command and the result would be printed in front of them.)

These days, with the development of computer monitors, we no longer need a typewriter to communicate with our computer. For this we can use the built-in console terminal application called bash.


CONSOLE: A physical terminal display with a keyboard. These used to be empty computers that connected to a large mainframe computer.
VIRTUAL CONSOLE: These are virtual versions of a physical console. In Linux, you can have multiple virtual consoles.
TTY: Teletypewriter. In Linux, tty is used to display which virtual console you are using: tty1, tty2, and so on.
COMMAND LINE: This is the text-based environment in general, which starts with a dollar sign ($), known as the ‘prompt’.
TERMINAL: This is the program you use to access the command line from the PIXEL desktop (its full name is LXTerminal).
SHELL: This is a command-line interpreter. It surrounds the computer’s kernel (hence the name). To get to the kernel, you go through the shell. The shell interprets your text commands and turns them into code the kernel understands.
BASH: This stands for ‘Bourne Again Shell’ and is the type of shell used in Raspbian.


======================

Bash

The Bourne again shell (bash) is a console shell developed by the GNU project that was released in 1989. The bash shell was written as open source and it supports many features, including the autocompletion of commands and filename wildcards.

The bash command interpreter is made available through the LXTerminal application. You can launch LXTerminal by double-clicking on its icon on the desktop. (Or you can ssh to the Raspberry Pi, and you will receive the same shell.) After starting it you will have Command Prompt. By default, this Command Prompt will show you the current directory that you are working in, your Raspberry Pi's hostname (by default raspberrypi), and your username (by default this is pi). As you move around the filesystem, you will see the name of the directory that you are in.

pi@raspberrypi:/etc/security $ pwd                   <--pwd Prints the current (Working) Directory
/etc/security

To run a command  type the command and press the Enter. The command will run and the results will be displayed. While a command is running, you won't be able to run other commands until it is finished. If you wish to let a command run in the background, add an & operator to the end of the command. This will let the command run in the background. To see which processing commands are running in the background run: ps –a.

One of the most useful features of bash is its ability to autocomplete the name of the command that you are typing. This is as easy as pressing the Tab key.

======================

Security  - sudo

Linux is a secure operating system. Every file is owned by a particular user, and that user is able to allocate permissions to that file in order to restrict other users and groups from accessing the file.

By default, all commands that are run in bash are run as the current user. The current user is normally the Pi user. This user is a standard user, and this means that you will not be able to run any commands that can affect other users, such as installing software or changing the network configuration. To achieve these task root (also knonw ase administrator or superuser) priviliges are needed. The root user has unlimited access to every part of the operating system and must be used with caution. To run a command as the superuser, you can use use the sudo utility. The sudo utility lets you run a command as a different user from what you are logged in as.

In order to use the sudo utility to run a command as root, all you need to write sudo in front of the command like: sudo apt-get moo

======================

Moving around the filesystem using bash

The most basic part of using a command interpreter, such as bash, is to be able to move around the Linux filesystem. Directories in the filesystem are represented in two different ways: absolute paths and relative paths.

An absolute path is a path that describes the location of a file or folder starting from / (root). It will always start with /. An example is /home/pi/games
A relative path is a path to the file or folder based on where you are in the filesystem. An example is ../games. (this means that a folder called games is one level above where you are currently)

Some special paths:
/                      This is the root of the Linux filesystem and the highest path that you can have.
./                     This represents the directory that you are currently in.
../                    This is the directory one level below the one you are in.
~                      This represents your home directory. (for the user pi, it is /home/pi)


Some  important files:
/etc/environment       sytem wide environmental variable
/etc/bash.bash.rc      initialization script for "interactive shell" ???
/etc/profile           initialization script for login shell ???
~/.bashrc              the user`s own intialization script
~/.bash_profile        the user`s own initialization profile (can create its own variables valid for the session he is logged in)

TAB                    auto completion (it will complete the command if possiblle)

======================

Bash commands

man ls             <--shows the documentation (manual) for the command ls

ls                 <--lists the content of a directory (-a: ; -l will )
  -a               <--shows hidden files, which are starting with "."
  -l               <--shows additional info: file size, permissions and the owner of the file

cd <directory>     <--change directory (you can change to the given directory like "cd /home/pi")
pwd                <--show which directory we are currently in (print working directory)

cat <file>         <--shows content of a file ("cat /dev/video0 > video" record video coming in from camera)
  -n               <--when displaying it shows line numbers
  -E               <--when displaying it adds $ at the end of each line

less <file>        <--shows content of a file by pages (with cursor keys possible to move inside the document)

head -10 <file>    <--shows first 10 lines in a file
tail -10 <file>    <--shows last 10 lines of a file

cp <file1> <file2> <--creates a copy of file1 as file2
  -r               <--recursively copies a file or directory
  -p               <--preserves the owner and timestamp of the files that you are copying
  -v               <--indicates verbose mode, it shows the names of the files that are being copied

mv <file1> <file2> <--moves file1 to a new name as file2 (after move file1 name will not be available anymore)

rm <file>          <--removes (deletes) a file (a dangerous command can delete everything: rm –rf /)
  -f               <--it will not ask for confirmation when deleting any files
  -i               <--it will ask before deleting each and every file
  -r               <--recursively deletes the files and folders

mkdir <directory>  <--creates a directory
  -p               <--creates any missing directories in the path
  -v               <--displays a message for every directory that is created

touch <file>       <--it will create a new empty file with given name

adduser            <--creates a new user
addgroup           <--creates a group
passwd             <--change password for a user

chmod              <--changes file/directory permissions

chown              <--change ownership of a file or directory
  -R               <--changes the owner of all the files and folders in the folder recursively
  -v               <--displays a message for every file that is processed

printenv           <--lists environment variables

>    <--redirects output of a command to a file  (cat file1 > file2, content of file1 will be written to file2)
>>   <-- appends output of a command to file (cat file1 >> file2, file1 will be appended at the end of file2)
<    <--reads a file and passes it to the command
|    <--this is a general-purpose command that chains similar to >


======================

Creating alias

1. Edit the file ~/.bashrc

alias ll='ls -l'     <--creates an alias "ll", and when that is entered it will be interpreted as commmand "ls -l"

2. source .bashrc    <--it will refresh current session with new settings (or if you logout, at next login alias will be available)

======================

No comments:

Post a Comment