Você está na página 1de 7

------------------------------------------------------------------

THE LINUX BASH

INTRODUCTION:
-It is the standard command line linux console terminal.
------------------------------------------------------------------

-The /etc/passwd file contains the list of all the user accounts, along with some
basic configuration about each user.
Example: rich:x:501:501:Rich Blum:/home/rich:/bin/bash.
-Each column is selerated by a ":".
-In above example the first column represets the username.
-the second column represents the placeholder for the password.
-Next is the the system id number and the following the subsequent id number.
-The next represents the systems full name.
-Following is the home directory and finally
-The last column represents the shell that is being used.

The bash Command Line Parameters


Parameter Description
-c string Read commands from
string and process them.
-r start a
restricted shell, limiting the user to the default directory.
-i Start an
interactive shell, allowing input from the user.
-s Read
commands from the standard input.

-The bash program uses these basic command line parameters to modify the type of
shell you want to start.
-When the bash shell starts, it executes the commands in the .bashrc file in the
users home directory.
-Most linux distributions store the commands in a particular file that is located
in /etc/bashrc.
-This file also sets various environment variables.
------------------------------------------------------------------
SHELL PROMPT

-The linux command line has a shell prompt that is the gateway to the shell. It's a
place where we enter the shell commands.
-"$" is the default symbol, however the prompt can be modified.
-There are two formal parameters that control the format of the command line
prompt.
-PS1: Controls the format of the default command line prompt.
-PS2: Controls the format of the second tier command line prompt.
-Shell uses the default PS1 prompt for initial data entry to the shell. To enter a
command that requires additional data information, the shell dispplays a second-
tier prompt specified by the PS2 environment variable.
-To display the current settings for your prompts, use the echo command:
rich@1[~]$ echo $PS1
\u@\l[\W]\$
rich@1[~]$ echo $PS2
>
rich@1[~]$

-The shell uses special characters to specify elements within the command
line. Also, it can be seen that special shell characters start with a "\". A
combination of prompt characters can be created inside the prompt.
-To create a new prompt, just assign a new string to PS1 variable:
-[rich@testbox ~]$ PS1="[\t][\u]\$"
[14:40:32][rich]$
NOTE: once a new prompt is created, it could be used within that session, else when
the system is booted, the default prompt is loaded.
------------------------------------------------------------------
FILE SYSTEM MANAGEMENT

-Windows has different drives and a folder in a particular drive is well separated
from other drives, But linux has a different file structure.
-The file structure of linux can be thought of has a "Tree" datastructure as it has
a parent root directory and various sub directories.
-The root or the parent directory is often termed as virtual directory and the
subsequent directories, that is directories at immediate lower levels are called as
mount points.
-Windows uses "\" while a "/" is used in linux.
-Mount points, are the additional directories that a linux system creates in
virtual directory to assign additional storage devices.
-Linux file system structure has evolved from the Unix file system structure.
------------------------------------------------------------------
TRAVERSING DIRECTORIES

CD
-Format: cd destination(if no destination provided, it takes us back to the home
directory).
-There are 2 basic ways in which we can traverse
-First is providing the ABSOLUTE PATH(specifies the complete path)
-Relative path, if traversing in a directory within a directory, this
technique comes handy as we don't have to secify the entire path to reach a file.
In this case there are 2 basic operators we can use to traverse between the
files efficiently:
-The '.'(dot) operator, represents the current directory.
-The '..'(double dot) operator, takes us one directory bacck/upwards in
the file system hierarchy.

LS
-Format: ls -[option]
When a normal listing is done, that is using ls, It has been found that ls does the
listing in alphabetical order coulumn wise and not row wise.
-Example:
-$ ls
4rich Desktop Download Music Pictures store store.zip test
backup Documents Drivers myprog Public store.sql Templates Videos
$
-The ls command also shows different colors for different type of entities. The
LS_COLORS environment variable controls this feature.
-The command: ls -F also distinguishes between files and directories with '/'
associated with directories and '*' with executable files.
Example:
$ ls -F
4rich/ Documents/ Music/ Public/ store.zip Videos/
backup.zip Download/ myprog* store/ Templates/
Desktop/ Drivers/ Pictures/ store.sql test
$
-The command: ls -a is used show hidden files where linux stores it's system
configuration information.
-The parameter -R can be used to list files that are present in directories within
current directory.
Example:
$ ls -F -R
.: file1 test1/ test2/
./test1:
myprog1* myprog2*
./test2:
$
-The parameter -l is called long listing parameter and is used with ls to list more
information about files in a directory.
Format: ls -l
Example:
$ ls -l
total 2064
drwxrwxr-x 2 rich rich 4096 2007-08-24 22:04 4rich
-rw-r--r-- 1 rich rich 1766205 2007-08-24 15:34 backup.zip
drwxr-xr-x 3 rich rich 4096 2007-08-31 22:24 Desktop
drwxr-xr-x 2 rich rich 4096 2001-11-01 04:06 Documents
drwxr-xr-x 2 rich rich 4096 2001-11-01 04:06 Download
drwxrwxr-x 2 rich rich 4096 2007-07-26 18:25 Drivers
drwxr-xr-x 2 rich rich 4096 2001-11-01 04:06 Music
-rwxr--r-- 1 rich rich 30 2007-08-23 21:42 myprog
drwxr-xr-x 2 rich rich 4096 2001-11-01 04:06 Pictures
drwxr-xr-x 2 rich rich 4096 2001-11-01 04:06 Public
drwxrwxr-x 5 rich rich 4096 2007-08-24 22:04 store
-rw-rw-r-- 1 rich rich 98772 2007-08-24 15:30 store.sql
-rw-r--r-- 1 rich rich 107507 2007-08-13 15:45 store.zip
drwxr-xr-x 2 rich rich 4096 2001-11-01 04:06 Templates
drwxr-xr-x 2 rich rich 4096 2001-11-01 04:06 Videos
[rich@testbox ~]$
-Parameter with ls command can be categorised into two categories.
1: Single letter(represented with a single '-' and can be combined in form of
a string)
Example:
ls -sail
: -s is for providing the block size of the file
: -i is used for providing the inode value of wach file
: -a to represent hidden each file
: -l to provide long listing.
2: A complete string(Represented with '--' and cannot be conjoined)
Example:
: ls --size

-Sometimes listing can yield a lot of files and accessing the useful content out of
it can be a challenge. Hence, for that we pass a filter element.
-Format: ls -l myprog
The above command does a string by string matching and displays the
result for that particular file.
If the string doesn't match exactly, wildcard characters are added to
represent the OFF character.
Example: If the file name is myproblem and we enter the command: ls -l
mypro
The question mark can be used to replace exactly one character
anywhere in the filter string.
For example:$ ls -l mypro?
-rw-rw-r-- 1 rich rich 0 2007-09-03 16:38 myprob
-rwxr-r-- 1 rich rich 30 2007-08-23 21:42 myprog
$
The filter mypro? matched two files in the directory. Similarly,
the asterisk can be used to match zero or more characters:
$ ls -l myprob*
-rw-rw-r-- 1 rich rich 0 2007-09-03 16:38
myprob
-rw-rw-r-- 1 rich rich 0 2007-09-03 16:40
myproblem
$
------------------------------------------------------------------
FILE HANDLING

-Creating files:
:touch command - Creates and empty file, hence size of the file is zero. Also
each file,created or already present has a unique inode number associated
with it.
- Syntax :: $ touch filename
- Also, we cand change the access time by using -a
parameter or modify time using -m and setting a particular time using -t and an
appropriate timestamp.
Example:
$ touch -t 200812251200 test1
$ ls -l test1
-rw-r--r-- 1 rich rich 0 Dec 25 2008 test1
$

-Copying files:
:cp command - Copies an existing file to a new file.
- Syntax :: $cp source destination
- The source and destination can be both relative or
absolute paths.
- If the destination file already exists, the system
asks for an overwrite confirmation.
- If we list down the two files(the base and the copied
file) we can see, both have different inode values.
- A file can also be copied in a directory.
- -p parameter can be used to preserve the access or
modification time of the original file for the copied file.
Example:
$ cp -p test1 test3
$ ls -il
total 4
1954886 drwxr-xr-x 2 rich rich 4096 Sep 1 09:42 dir1/
1954793 -rw-r--r-- 1 rich rich 0 Dec 25 2008 test1
1954794 -rw-r--r-- 1 rich rich 0 Sep 1 09:39 test2
1954888 -rw-r--r-- 1 rich rich 0 Dec 25 2008 test3
$
Now, even though the test3 file is a completely new
file, it has the same timestamps as the original test1 file.
- -R is very powerful in case of copying, it allows to
recursively copy an entire directory to another directory with just
one command.
Example:
$ cp -R dir1 dir2
$ ls -l
total 8
drwxr-xr-x 2 rich rich 4096 Sep 6 09:42 dir1/
drwxr-xr-x 2 rich rich 4096 Sep 6 09:45 dir2/
-rw-r--r-- 1 rich rich 0 Dec 25 2008 test1
-rw-r--r-- 1 rich rich 0 Sep 6 09:39 test2
-rw-r--r-- 1 rich rich 0 Dec 25 2008 test3
$
- Wildcard characters can also be used with cp command.
-Some parameters:

The cp Command
Parameters
Parameter
Description
-a
Archive files by preserving their attributes.
-b
Create a backup of each existing destination file instead of
overwriting it.
-d
Preserve.
-f
Force the overwriting of existing destination files without
prompting.
-i
Prompt before overwriting destination files.
-l
Create a file link instead of copying the files.
-p
Preserve file attributes if possible.
-r
Copy files recursively.
-R
Copy directories recursively.
-s
Create a symbolic link instead of copying the file.
-S
Override the backup feature.
-u
Copy the source file only if it has a newer date and time than the
destination
(update).
-v
Verbose mode, explaining what’s happening.
-x
Restrict the copy to the current filestytem.

-Linking files: Various types of linkages can be involved in files.


1: Soft link -it acts as a virtual link to the parent file and contains
the address of the parent file.
-both the files have different inode values.
-The -s parameter creates a soft link between two
files
-Example:

$ cp -s test1 test5
$ ls -il test*
total 16
1954793 -rw-r--r-- 2 rich rich 6 Sep 1 09:51 test1
1954794 -rw-r--r-- 1 rich rich 0 Sep 1 09:39 test2
1954888 -rw-r--r-- 1 rich rich 0 Dec 25 2008 test3
1954793 -rw-r--r-- 2 rich rich 6 Sep 1 09:51 test4
1954891 lrwxrwxrwx 1 rich rich 5 Sep 1 09:56 test5
-> test1
$
2: Hard link -It is the actual file and acts as a reference file.
-Both the parent file and the hard link have the same
inode value which essentially means that it's the same file.
-Example:

$ cp -l test1 test4
$ ls -il
total 16
1954886 drwxr-xr-x 2 rich rich 4096 Sep 1 09:42 dir1/

1954889 drwxr-xr-x 2 rich rich 4096 Sep 1 09:45 dir2/

1954793 -rw-r--r-- 2 rich rich 0 Sep 1 09:51 test1


1954794 -rw-r--r-- 1 rich rich 0 Sep 1 09:39 test2
1954888 -rw-r--r-- 1 rich rich 0 Dec 25 2008 test3
1954793 -rw-r--r-- 2 rich rich 0 Sep 1 09:51 test4
$
Also int the 3rd column we can see that both the
files test1 and test4 on which hard link has been made have the link count of 2.
-Hard link can be created on the same physical
medium. For files under different mount points, a soft link has to be made.

-Renaming files:
-Moving in linux is called as renaming. Moving one file and specifying
another name for files with hard links is possible. The inode value and the
timestamp gets copied but for the case of soft link, this can lead to false vallues
as in this case the files can have a random inode values.
-The syntax is: mv file1 file2 for files and mv dir1 dir2 for directories.

-Deleting files:
-The process of deletion is called removing.
-Command to remove files in a bash shell is rm.
-Syntax : rm filename.
-File once removed is removed forever.
------------------------------------------------------------------
-DIRECTORY HANDLING:
creating: mkdir name
deleting: rmdir name (if name is empty) else
rm -r name (still verifies files inside the directory i.e.
prompts the user whether to delete files)
rm -rf name (force remove recursively with no prompt)
------------------------------------------------------------------
VIEWING CONTENT
1: stat command-
syntax: $stat filename
This gives a complete rundown of a program on the filesystem.
$ stat test10
File: "test10" Size: 6 Blocks: 8 Regular File
Device: 306h/774d Inode: 1954891 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 501/ rich) Gid: ( 501/ rich)
Access: Sat Sep 1 12:10:25 2007
Modify: Sat Sep 1 12:11:17 2007
Change: Sat Sep 1 12:16:42 2007
$

2: file command-
syntax: $file filename
This gives the type of the file
types of files: text, executable, data(non printable binary characters that
can't be run on the system).

3: cat command-
syntax: $cat filename
This return the entire file data and displays it on the screen.
-n parameter numbers the lines in the file in ascending order.
-b numbers only those that have text in them.
-T removes the tab spaces between the text if present.

4: more command-
The major drawback of cat command was that if the data set was huge, entire data
set can't be processed at once.
This issue was solved by the more command that loads only the data that can be
accomodated on a screen.
This also displays how far you have come reading the data set.

5: Less command-
The less command is more advanced than the more or cat commands because it is
able to load the data required for the window size without loading the entire text.
Less command functions like more command but is also able to display the number
of lines that have been loaded.
Less command has a lot of functionality and also supports the
up/down/pageup/pagedown keys to enhance the viewing of file.

6: Tail command-
To peek at the last set of lines of a very large file have more than 1000 lines.
Default number of lines is 10.
$tail -f filename is a command that helps to keep the bottom of the file live
i.e. if the process is in running stat, the bottom can be shifted, this helps a lot
in live systems and keeping track of log files.

7: Head command-
Displays first 10 commands
-f parameter is not supported as head can't change.
Used to give a peek of the file and not loading the entire file.
------------------------------------------------------------------
------------------------------------------------------------------

Você também pode gostar