0% found this document useful (0 votes)
14 views

Linux Notes

Notes on Basic Linux operation
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Linux Notes

Notes on Basic Linux operation
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

02/05/2013 Lesson

GNOME - is the name of the desktop environment and the graphical user interface that runs on
top of a Linux computer operating system. It is composed entirely of free and open source
software and is mainly developed by redHat employees. GNOME controls the look of the GUI
that interacts with the operating system.

Linux is almost similar to the windows environment. It is helpful that you have used Windows
OS before.

Linux desktop environment - Controls the look and feel of Redhat Linux
Window Manager inside the desktop environment

< The X Window System >

Microsoft Windows is based on a graphical user interface (GUI for short) where you can control
the apps by pointing and clicking. But Linux, just like Unix or MS-DOS, is completely text based.
This means that everything in Linux can be done without any GUI, and it's a plus when using
Linux, for example, as a server because the computer's resources aren't wasted in running a
GUI. However, most of us normal home users want a pretty GUI where we can use graphical
apps and point and click to our heart's content. So how do we get to the GUI?
Because Linux is text based, you run the GUI on top of it. In Unix the GUI is called X Window
System or X for short. The term X Windows is also widely used, but it's technically incorrect. I
personally use the incorrect term anyway, because it's short and handy, but keep in mind some
Linux users avoid using the term X Windows.
The X Window System makes it possible to run graphical apps on Linux. X is responsible for the
hardware related settings: it controls, for example, the mouse, keyboard, and the monitor
settings like refresh rate and resolution. The graphical apps themselves don't need to care for
the hardware they're running on. The apps just talk to X and tell it what they want to display. X
listens to the apps and converts the apps' display commands into something that the graphics
hardware can display. So, X makes it possible for the graphical apps to display their interface on
the screen, but X doesn't control the windows where the apps are displayed.
The Linux version of X used to be XFree86, but these days, most newer distros use X.org. X.org
is a fork of XFree86 that was created because of some licensing issues. So, if you want a GUI
in Linux, you must run X.org on top of it. Most Linux users, including me, mean XFree86 or
X.org when they say X Windows or X Window System, or just X.

< Window managers >

Because X provides the place to put the windows on but doesn't control them, you need
additional software that takes care of handling the windows. The piece of software dealing with
the windows is the window manager. The window manager is just an X program itself, and like
the other graphical apps, it also needs the X Windows in order to work. It's just a special piece
of X software because all it does is take care of the windows.
The window manager controls the way your desktop works: how the windows look and act. The
window manager decides what kind of decorations to put around the windows. It's the window
manager's job to provide ways of controlling the windows, like moving, hiding, resizing,
iconifying, or closing them. The window manager decides what window at the moment accepts
input from you and what window is on the top. The window manager also controls the ways you
do these tasks: what mouse buttons you click or what keys you press in order to accomplish
these window management tasks.
The window manager may also provide additional things. Different window managers have
different features, but most window managers today provide a menu or menus for launching
apps. Many window managers provide virtual desktops - multiple screens you can switch
between pretty much the same way as you switch between windows, but instead of switching
between apps only, you switch between whole desktops. Some window managers may also
provide graphical configuration programs in order to make configuring them easy.
Since there are dozens of different window managers out there, you can change your desktop's
look and feel completely by changing the window manager. Of course MS Windows lets you
have different desktop themes, but in Linux, you can change everything. The window managers
may focus on different things: one is very configurable, one provides lots of keyboard shortcuts
for many different tasks, one is very minimalist and provides only the essential features for
handling the windows, one is graphically pleasing with stunning window decorations and menus,
one is fast and slick, one imitates the look and feel of Windows, and so on...

< Desktop environments >

The window manager provides everything you need for controlling the windows on your
desktop, and for many users, this is just enough. However, you may want some additional
features and may want the window manager to take care of the whole desktop, but providing
these additional features isn't a window manager's problem. This is where you need a desktop
manager, or a desktop environment, whatever you wish to call it.
Like the name suggests, a desktop manager takes care of your whole desktop by providing and
controlling additional helpful features that don't directly deal with handling the windows. For
example, a desktop environment may provide you with a taskbar or many taskbars, additional
menus, icons on the desktop, screen savers, and many little utility programs like a graphical file
manager, search tool, text editor, and so on.
The two big players in the desktop manager field are KDE (K Desktop Environment) and
GNOME (GNU Network Object Model Environment). There are a lot of differences between
them, but they have one thing in common: you must use a window manager in addition to a
desktop environment. A desktop environment takes care of the whole desktop, but it's still the
window manager's job to control the windows. One of the biggest differences between KDE and
GNOME is the way they play with window managers. KDE has its own window manager, so if
you use KDE, it'll be really painful to change the window manager you use with it. However,
GNOME doesn't come with its own window manager, so you can freely choose what window
manager to use with it.

The Command Line:


Advantages of using the CLI or the command line interface:

it is the most powerful way to communicate with the operating system.


everything you learn on the command line will transfer to every Linux distribution out there.

structure of the command prompt:

example: [mariano.santos@den1-1-2g ~]$ ~ The command prompt contains three fields, the
first is mariano.santos, it is the name of the account I used to log in to, next is the machine I
logged into (den1-1-2g) and last is the current working directory which in this example is the
home directory for the account I logged into or the Tilde.

Dot files are configuration files. ~ example .bash_profile and .bashrc

MORE command:

The MORE command allows you to look at a text file page per page.

TAIL command:

The tail command allows you to look at the last part of the text file.

LESS command:

The Less command is a lot like MORE but gives you more flexibility.

*It is a good habit to always log into the system as a regular user compared to the root user to
avoid unnecessary alterations to system files since the root user has special permissions to
read, write and execute compared to a regular user. Only log into the system as the root when
you have to access files or directories that require special permissions.

Terminal Window -

to log in as a different user in the terminal window, you have to use the SU command followed
by a dash then the username you will use to log in ~ example: su - mariano.santos

SU - The su utility requests appropriate user credentials via PAM and switches to that user ID
(the default user is the superuser). A shell is then executed. This means switch user. by typing
just SU with nothing follows, it will automatically log you in as the root user.
Opening the terminal window - right click and select open terminal

The terminal window is the most basic way of communicating with the linux environment and
unlike the GUI, it is more universal and will work in every Linux OS out there. It uses the
command line interface to interact.

COMMANDS

PWD - print working directory - show the path of the current active directory
LS - List all the files and directories in the current active directory. If you add a filename then it
will display the filename there or if it is a directory it will print the contents of that directory.
CD - change directory
~ .. - move up one level
~ . - stay in the current level
CD ~ will navigate to the users home directory

Command completion - while typing the name of a file or directory, you can hit tab and the
computer will auto populate the rest only if the directory or the filename is available in the
current active directory that you are working on.

CP - copy file -
format: cp filename1 filename2 ~ this will copy 'filename1' and make an exact copy assigning
the filename 'filename2' to the new file.
If you will copy a file to another path like for example, format: cp /user/documents/marlofile
/tmp/. this will copy the file to the specified directory using the same filename "marlofile".

Command History - hit up or down to browse through the previous commands that you have
already executed in the CLI and you can edit once it comes up in the CLI

CLEAR - type this to clear the texts that are already in the terminal. you can also type in CTRL +
L as a key board shorcut

RM - remove file ~format: rm filename ~ removes/delete the filename from the disk

MV - move file/rename file ~ format: mv filename filename1 - This command will rename the file
'filename' to 'filename1'.

MKDIR - make directory - this will create a directory ~ format: mkdir directoryname - this will
create a directory in the active directory. You cannot rm a directory, you have to use the rmdir
and it will also not allow you to remove a directory that isn't empty.

TOUCH

The touch utility sets the modification and access times of files. If
any file does not exist, it is created with default permissions.

By default, touch changes both modification and access times. The -a and
-m flags may be used to select the access time or the modification time
individually. Selecting both is equivalent to the default. By default,
the timestamps are set to the current time. The -t flag explicitly spec-
ifies a different time, and the -r flag specifies to set the times those
of the specified file. The -A flag adjusts the values by a specified
amount.

Text editors readily available in Linux


- EMACS and VI

EMACS -
Exit EMACS - Ctrl-x then Ctrl-c
Save EMACS file - Ctrl-x then Ctrl-s
Start Typing - Ctrl-l
Emacs Tutorial - Ctrl-h then t

Tilde in the CLI represents the root

02/06/2013 Lesson

Learning MAN and INFO pages

MAN - stands for manual ~ format: man <command> ~ example: man ls <<manual on the list
command
the manual will also list down the description and the options available for that command.
Searching in MAN is similar to VI and the way you navigate through a man page is the same as
the LESS command where "u" is scroll up and "spacebar" scrolls down.

INFO - information message ~ format: info <command> ~ example: info ls


there are subsections in the INFO pages characterized by asterisks or a star
search in the INFO page by hitting ctrl-s and continue hitting ctrl-s to go through the rest of the
instances of the word. Press 'q' to quit the INFO page

The difference from MAN and INFO is that info is designed for more non technical users or
beginners using Linux while the MAN pages are for more advanced/experienced users.

APROPOS
apropos searches a set of database files containing short descriptions of system commands for
keywords and displays the result on the standard output.

SUDO
sudo allows a permitted user to execute a command as the superuser or another user, as
specified in the sudoers file. The real and effective uid and gid are set to match those of the
target user as specified in the passwd file and the group vector is initialized based on the group
file (unless the -P option was specified). If the invoking user is root or if the target user is the
same as the invoking user, no password is required. Otherwise, sudo requires that users
authenticate themselves with a password by default (NOTE: in the default configuration this is
the user's password, not the root password). Once a user has been authenticated, a time stamp
is updated and the user may then use sudo without a password for a short period of time (5
minutes unless overridden in sudoers).

.bash_history contains all the commands you have typed. It is located in the user directory
(example: /Users/mariano.santos)

DIRECTORY STRUCTURE

The BIN directory

- Holds the most commonly executed commands in the system

the SBIN directory


- Holds the commands used by root users, super users, system administrators. The "S" is for
system

the OPT directory


- This is where you will store optional software.

the USR directory


- the user configuration files.

Alias

The 'SOURCE' command runs or executes the configuration file. Example of configuration files
are .bash_profile and .bashrc. Use this when you made changes to the configuration file so that
it would be executed.

ABSOLUTE directory names are directory names that start with a slash "/" like /home or /etc.
these directories are found in the root directory

RELATIVE directory are the directories in the current users directory or the subdirectory of the
directories in the ABSOLUTE directory.

02/07/2013
Linux Installation

Questions to ask before installing a Linux OS

What's the purpose?


Personal Workstation
Server

Linux vs. Unix (Solaris/IRIX)

Linux Distributions:

Red Hat - most popular Linux OS version


Debian - Most stable version of Linux, The Universal Operating Sysytem
SuSe - Popular in Europe
Slackware - Purest Linux OS no GUI

Linus Torvalds developed Linux in August 1991.


You can go to linux.org to get the rest of the Linux distributions' links
you can go to linuxiso.org to get the iso image of the installation disk
linuxsoftware.org is a site showing the various available software for linux installations

Software Licenses:

Open Source Software - Free software movement - GPL: General Public License ~ LGPL:
Library General Public License
the free software movement lets you use, study, modify, and redistribute the software that is
under GPL and in turn, you have to share back to the community what you have created or
developed. More information about this can be read in http://www.debian.org/intro/free

Commercial Software:

Closed Source: The source code cannot be seen


Distribution Limitation
Paid software

02/08/2013

Workstation is similar to the term desktop computer

Installation of Linux

Installation methods: Text, GUI and scripted installation


Instructions:

Put in bootable media

Power on computer

If its a network install then point the installation program to the site or if you are in an ethernet
environment, point to the network path of the computer you want to use as a resource. The
easiest method is to use the text or GUI based installation similar to the current Windows OS.

Linux rescue mode is the same as the repair installation in Windows.

Text and GUI is almost similar. they both have a wizard to assist you with the installation, the
only difference is the interface how you will communicate with the installation program. text ~
keyboard == GUI ~ mouse pointer and keyboard.

Grand unified boot loader - GRUB is the default boot loader for Linux

Firewall - setup various levels of network security in your computer.

AUTHENTICATION CONFIGURATION

NIS - Name Information Server

LDAP - Lightweight Directory Access Protocol - Directory information for all the users

Kerberos - Password will travel in the network via an encrypted message

Boot Disks can load your computer if your boot loader gets corrupted or some important file for
booting is gone.

Installing Linux is similar to installing Windows, there are basically instructions to guide you in
the process.

02/11/2013 INSTALLING AND UPDATING SOFTWARE IN LINUX

In Linux distributions, a “package” refers to a compressed file archive containing all of the files
that come with a particular application. The files are usually stored in the package according to
their relative installation paths on your system. Most packages also contain installation
instructions for the OS, as well as a list of any other packages that are dependencies
(prerequisites required for installation.
Common types of Linux packages include .deb, .rpm, and .tgz. Since Linux packages do not
usually contain the dependencies necessary to install them, many Linux distributions use
package managers that automatically read dependencies files and download the packages
needed before proceeding with the installation. Some examples of package managers are APT,
YUM, and Pacman.

Installing Software (A.K.A Packages)

The package will come in file formats like tar, rpm or deb.

TAR - Tape Archive - it is a compressed file

As much as possible, use the package manager that is specific for your operating system.
Although, RPM(red hat package manager) or deb (debian package manager), it is still
recommended that you use the package specific for your system. The installation might get an
error when it points to a library that is specific to the other and vice versa or the naming
convention might be different.

RedHat Package Management:

Naming convention for RPM:

<packagename>-a.b.c-x.arch.rpm

the arch in the naming convention is usually the type or processor or will be labeled for "src" or
source code"

to run the rpm command - format: rpm <operations> <options> <packages>

Common Operations:

-i ~ install
-U ~ update
-q ~ query
-e ~ uninstall

Common Options

--force ~ force the installation

--nodeps ~ no dependency
example:

rpm -ivh <packagename>.rpm ~ the "vh" operation shows you the logs on how far the
installation is progressing by showing progressive hashes.

Debian Package Management:

used by Debian OS and all the Linux OS that was branched out of this operating system

Commands:

dpkg and apt-get

DPKG OPERATIONS:

-i ~ install
-P ~ Purge
-p ~ print info about installed pkg
-I ~ info about uninstalled pkg

APT-GET OPERATIONS - Installs or updates softwares from remote locations (source lists)

update - Obtains updates from remote location

install installs from a remote location

format: apt-get update <packagename>


format: apt-get install <packagename>

apt-get will look at all the resources from a remote location, apt-get will install it for you and you
don't have to worry about the dependency information.

TAR FILES:

Works on every Linux distribution. Slackware only supports this package manager.

Command:
Format: tar xvf <packagename>.tar <package-directory>

package directory is where you want to install the package.

Custom main Menu items:


Menu Editor

Choose from the Gnome icon then select the menu editor and then select the path where you
want to see the "shortcut" of the program you want to run. Fill out just the name and the
command to run the command.

Failed dependencies:

You have to locate the library that is required by the package manager to successfully install the
software. You have to locate the package that provides the missing library your package is
dependent on. You can locate the package by searching it in Google and downloading it to your
disk and running the newly downloaded file.

127.0.0.1

.conf - configuration file

02/12/2013

User Administration in Linux - Multi User Operating System

- Add, Remove, Modify (Change Password)


~ User must the root
~ useradd, userdel, usermod (in /usr/sbin)
~ default config files in /etc/skel

- Groups
- Permissions (modes)

Users in Linux

- User = Account
- Account attributes
- superuser

/etc/shadow ~ this will contain the user password but it is encrypted.

USERMOD ~ you can check the man page for operations and options
format: usermod -c "Lisa Simpson" lisa

File Permissions

Separate Permissions for User, Group, Other


when you do a long listing (ls -l or ll) it shows the modes of the files in the current directory
for example:

drwxr-xr-x 3 mariano.santos platops 4096 Feb 12 08:24 .


drwxr-xr-x 76 root root 4096 Dec 14 17:29 ..
-rw------- 1 mariano.santos platops 40886 Feb 11 11:02 .bash_history
-rw-r--r-- 1 mariano.santos platops 33 Sep 14 2010 .bash_logout
-rw-r--r-- 1 mariano.santos platops 176 Feb 12 08:15 .bash_profile
-rw-r--r-- 1 mariano.santos platops 124 Sep 14 2010 .bashrc
drwx------ 2 mariano.santos platops 4096 Sep 14 2010 .ssh
-rw------- 1 mariano.santos platops 3638 Feb 12 08:24 .viminfo

-rwxr-xr--

Only the root and the Owner can change modes

the command for change mode is CHMOD

CHMOD has 2 formats


1. Symbolic
format example: chmod g+w <filename> ~ in this example, change mode will add write function
to the group for the specified file "g" is for group and "w" is for write ("+" is to add and "-" is to
take away)

2.Binary

format: chmod 754 <filename> ~ this is determined by the mode you see in the beginning of the
list when you do a long listing
for example: drwx------ ~ This is a mode. Another example: -rwxr-xr-- ~ This is also a mode. The
very first place is the identifier of the mode if it is a directory or a file, if it is a directory the letter
"d" is listed in the beginning. The mode is formatted by groups of threes after the identifier which
will be for the user, group and other. The group of threes will be identified by binary digits and
has an attribute of 421. example is 101 = 5 because in the first place it has a set value of "4"
and the third place has a set value of "1" which makes it 5. For 111 it is equals to 7 because the
first place values is 4, second is 2 and last is 1 so 4+2+1 = 7. so the 754 in the example would
give the user full permission(read, write and execute), the group would have read read and
execute and other would have read only attribute. This will be listed like this: -rwxr-xr--

Directory Permissions (Modes)

Change Owner command ~ chown bob.<groupname> <directoryname>

newgrp <groupname> command will change the group you are currently on to the specified
group after the command.
gpasswd command is used to administer the /etc/group file (and /etc/gshadow file if compiled
with SHADOWGRP defined). Every group can have administrators, members and a password.
System administrator can use -A option to define group administrator(s) and -M option to define
members and has all rights of group administrators and members.

02/19/2013

More on the shell environment

ENV command - Displays the information of the environments available to the user. A useful
line in the ENV output is the PATH where it shows the available executable commands at the
users disposal. Displays Environment Variables

EXPORT command - This is used to change environment variable.

example: export <environment variable> ~ export PATH=$PATH:/usr/sbin:/sbin

*The dollar sign followed by the environment variable "PATH" tells the computer to copy
everything in the current PATH variable and add the paths after the colon.

redirection operator - these are commands to redirect the current command to another
command. An example of which is the GREATER THAN sign ">" after a command ~ ex: env >
envoutput ~ This command will redirect the output of the "ENV" command to a text file named
"envoutput".

PIPE operator

This connects the output of the first command to the input of the second command ~ example:
<command> | <command> ~ env | more ~ What this does is the output of the ENV command
will be displayed as viewing it using the MORE command without saving a text output.

GREP (get regular expression)

example: grep 5 etc/passwd

another powerful search using grep is using square brackets when searching digits. example ~
grep 5[0-9][0-9] passwd ~ What this does is it will run grep using the expression 5[0-9][0-9]
which will search for a three digit number starting with a five and will have 2 digits following that
will range from 0-9 in the filename passwd. The dash operator identifies a range in the square
brackets.

You can also add a colon to the expression to restrict it to just look for just the 3 digits specified
in the expression. example ~ grep :5[0-9][0-9]: passwd.

If something is inside the square brackets, it looks for the digits inside the brackets. For example
"grep [56] passwd" is not looking for the number 56, it is looking for a digit with a 5 or a 6.

WILDCARDS = "*"

02/22/2013

FIND command

format: find <starting point> <search criteria> 'search string' ~ example: find . -name junk ~ what
this does is to find the name junk starting in the current directory symbolized by the dot "." and
all the subdirectories from the current directory

03/19/2013

Continuing the tutorial on FIND

EXEC command
-exec

example: find . -name '*.h' -exec diff -u '{}' /tmp/master ';'

The diff command just find the difference between the files the semi colon is the operator to
terminate the command string.

whereis command

You can use “type” or “whereis” command to find out which command shell executes and to
print binary (command) file location for specified command.

Binary Files:

A binary file is a "non-human" readable file, i.e., code to be interpreted by the OS or any app,
not to be read. Just think as the opposite for a text file. You can use the "file" command on any
file to get its type.

SORT command

will sort a specified list in alphabetical order by default.


command example ~ format: sort <filename> ~ what this does is sort the texts by lines in the
filename specified.
NL command

Prints a text file displaying the line number of each line on the left side example format: nl
<filename>

TAC command

Prints a textile in reverse order. example format: TAC <filename>

CUT command

- used to limit the output of a file so you will only view specific data. This command is always
paired with an option which is the -f option. The -f option is the "field" or in common terms, the
column in a text file. example format: cut -f1,3 Network ~ what this does is print only the 1st and
3rd column in the file "Network".

PASTE command

This command joins two text files together.

DELIMITER

In computer programming, a delimiter is a character that identifies the beginning or the end of a
character string (a contiguous sequence of characters). The delimiting character is not part of
the character string. In command syntax, a space or a backslash () or a forward slash (/) is often
a delimiter, depending on the rules of the command language. The program interpreting the
character string knows what the delimiters are.

delimiter option usage: format: ~ paste -d"

FMT command

will arrange a text file to limit the spaces in between the words to single spaces.

03/20/2013

Linux Processes

INIT is the very first process to run in a linux system.

Program is a collection of instructions. A process is a running version of that program.

PS command - lists the processes in the OS


PSTREE command - Is a visual representation of the PS

"&" or the ampersand sign is useful for running a process in the background. For example if you
type "cat <filename>.txt" it will print the contents of the text file in the terminal window until and
will stream past until the end of the file. If you do not want to do other commands while that is
happening, you can type the ampersand "&" at the end of the command like so : "cat
<filename>.txt &" and it will immediately display the command prompt after hitting enter and run
the command in the background.

BG command - will bring a current process to the background

FG command - Will bring a current process to the background

The process ID or the PID as displayed in the PS command is used to identify a process. It is
essential to know this if you want to terminate a process with the "KILL command".

NOHUP command - will run a process and will not tie it up to a terminal

NICE command - will change the priority of a process.


-20 is the highest priority
+19 is the lowest priority

sample format: nice -18 ./<program name> & ~ what this does is it sets the priority number of
the program "test" to +18 which is quite low in priority. The number beside nice is just the option

RENICE command - Change the Priority of a Already Running Process ~ quite similar to the
NICE command also in format

JOBS command - lists the jobs running in the background, if nothing is running in the
background, then it will not display anything.

TOP command - is a dynamic screen that tells you the information in the system like the
number of running processes, the free disk space, the PID's etc. The TOP command also eat
up CPU time and take away resources from other tasks. This is a very useful command to see
what is eating up CPU time. the more CPU time a process eats, then it is more likely the one
causing the hang up in your CPU.

LILO - is a type of bootloader, this loads the Linux Operating system

05/19/2015

BASH scripting
What is a shell? - A shell is a program that is used to start other programs.

There are a variety of UNIX shells to choose from:


Bourne shell - sh
Korn shell - ksh
C shell - csh
Bourne-again shell - bash

comments - side notes in a shell script, user notes

variable - hold data and names

special variables - arguments or parameters in a script ex. $1, $2, $0, $#

if statements - allow us to make decisions and test the certain conditions in your script to make
the program more dynamic

while loop - like if statements but will continuously loop until the conditions are true in the if
statement

SDL - shell definition line - the first line in a shell script to show what shell the script will be
running

*you can use emacs or vi to edit your shell script.

Double and single quotes behave differently

ex. testing="moby dick";

*everything between the double quotes is assigned to the variable.


*Single quotes are commonly used to allow a special character to be a part of a string, even if
you put $<variable>, it will not derive the value of the variable assigned to it, instead it will print
$<variable>
*Another way to assign a special character to a variable is by using the backslash (\)
*Do not confuse the backtick (`) with a single quote, a back tick is needed when you are
inputting a command in a shell script

Operators for numerical comparisons:


-eq (equal)
-ne (not equal)
-gt (greater than)
-ge (greater than or equal
-lt (less than)
-le (less than or equal)
Operators for file comparisons

-d (is the file a directory)


-f (is the file a regular file)
-r (is read permission set)
-w (is write permission set)
-x (is execute permission set)
-s (is there a non-empty file)
-a (is same as the english language "and"

*if you are assigning a file or a directory to a variable in a script, you do not have to put quotes.
ex. dir1=/home/marlo; file1=/home/marlo/scripts/shellscript.sh;

More on shell scripting:


*If you want the output of a command to display on the screen when it is successful then you
have to assign the output to a variable to retrieve when you echo it. ex.:

#!/bin/bash

read -ep "Please enter the date in YYYY-MM-DD Format : " date1
cd ~/ach/inbound/archive
if output="$(stat -c "%y|%n" * | grep "^$date1")"; then
echo "$output"
else
echo "No ACH file on the given date"
fi
exit $?

In the example above, if you want the input of a prompt to be assigned a variable for later use,
use the "read" command with option "p" (p means prompt). additionally, if the input is a file in a
certain directory, you can add option "e" so that you can use the autocomplete (tab key) of linux
when typing something.

To change the output text color, you have to add the command $(tput setaf <number>) where
the number corresponds to the color. The chart below shows the color and its numerical
equivalent:

0 – Black
1 – Red
2 – Green
3 – Yellow
4 – Blue
5 – Magenta
6 – Cyan
7 – White

Similarly, you can use $(tput setab <number> to set a background text color. The same color
chart applies to this. Lastly for the tput, you have to set the

05/22/2015

SED command tutorial

SED - stands for stream editor

SED allows you to manipulate text files by using the command

sed substitute format: 's/…./…./' <file> :: the s in the beginning of the forward slashes mean
substitute and will always appear in the beginning of this command.

option -i with the sed makes the necessary modifications in the file. note that there is no undo
on the file so it is best to just redirect it to a new file. ex.
sed 's/t/T/g' (filename) > (newfilename)
In the given example, the command changes all the lower case t with the upper case T and
creates a new file with the substitutions in place and saves it to a new file. If you just want to do
the modifications to the text file without creating a new file you use option -i with the sed
command like this: sed -i 's/t/T/g' (filename). the g in the end of the command options make the
substitution for all non-overlapping matches of the regular expression, not just the first one,
without the "g" in the end, it will just do the substitutions in every first regular expression match
of each new line.

you could also replace words instead of just letters. for example: sed s/this/that/g (filename),
what this does is replace the word "this" to "that in a given file.

you could also replace a single letter to a string with the sed command.

if you just want the substitute the first match in each line you use the caret (^) symbol as in the
example: sed 's/^th/ht/g' (filename), what this command does is it replaces all the first "th"
matches in each line with "ht".

so if you have a text file with these words:

this is great!
that is fine
through thick and thin
the command will substitute the "th" with ht and will have an output similar to this:

htis is great!
htat is fine
htrough thick and thin

with the caret, even if you have the option g, it will just replace the first regular expression match
in each line.

As with the example above, if you want to change something at the end of a line, you use the
the $ symbol. use it in this format: sed 's/y$/oy/g' (filename). What this does is it changes all the
letter y's located at the end of each line with "oy"

with this sed command sed ‘s/[0-9]/*/g’ (filename), the command changes the values in a file
from 0-9 to an asterisk. If your text file would have an output like this:

54 teams
4 members
logout

It would be modified to this:

** teams
* members
logout

You could also add the range of alphabets in this example: sed ‘s/[a-z]/*/g’ <filename> , what
this does is it changes all the lower case letters in a text file to and asterisk. Same goes if you
want to just change all the uppercase letters, you just replace a with A and z with Z. The range
follows the standard ASCII character chart where it begins with the uppercase letters (for the
alphabet) to the lower case letters. the square brackets represent a single character with a
specified range, this does not just work for sed, as this can also apply for other UNIX
commands.

Lets look at other examples: sed ‘s/[0-z]/*/g’ <filename> , what this does is change all the letters
and numbers in a text file, again following the order of how the ASCII character chart is
organized.

sed 's/[a-ch-o]/*/g' <filename> specifies a range which skips other letters, in the example given,
the letters a to c and h to o will be replaced with the asterisk symbol in a file with text.

sed ‘s/[a-c][m-o]/*/g’ <filename> , This example is different from the one given above as this
specifies a range for two character string. This command will replace all instances in a text file
where the first character is from a-c and the second is from m-o.
Using Ampersand to match strings SED

sed ‘s/[0-9]//g’ (filename) What this command does is to remove all instances of numbers in a
file.

sed ‘s/[0-9]/(&)/g’ (filename) What this command does is to put a parenthesis around all
numbers that are between 0-9. The Ampersand symbol is a representation of the range we
specified. It does not replace any character, instead it modifies or adds text or symbols in the
specified range. But if you have more than a one digit number, it will append each character like
this: (2)(6). If you want to tell your command to include 2 or more digit numbers, you should
modify your command to look like this sed ‘s/[0-9][0-9]*/g’ (filename) : This modification now
reads "zero or more of the preceding character"; that is, the pattern /[0-9][0-9]*/ will match ANY
number with one or more digits, not just one- and two-digit numbers.

sed ‘s/[0-9][0-9]*/&&/g’ (filename) This command will take numbers which are 1 to 2 digits long
and copy them right beside each other, so if you have the number 3, it will modify it to 33 and if
you have the number 39, it will modify it to 3939. The ampersand will take whatever matched
string we have and put it into whatever we are replacing it with.

*The forward slashes in the sed command are called delimiters, you can basically any symbol
for your delimiter. Like for example, you could type the command in this fashion: sed ‘s:a:b:g’
(filename), in this example, instead of using the forward slash, I used the colon as my delimiter.
As long as you use all three symbols as a delimiter in a command, it will accept it. it is good
practice that if the character you will replace is similar to your delimiter, then use a symbol that
you are sure that you are not going to use. Usually this is beneficial if you are changing the path
in a file. For example: if you are changing the directory path from usr/bin/create to
tmp/boing/boing_boing, you could write this as sed ‘s:usr/bin/create:tmp/boing/boing_boing:g’
(filename) with this format, you do not need to place a backslash before the forward slash like
so sed ‘s/\/usr\/bin\/create/\/tmp\/boing\/boing_boing/g’ (filename) It is also good practice that if
you will use an alternative format, like the previous example where the colon was used in place
of the forward slash, in a script. You could put a comment when you use it: example:
echo “men are from mars and women are from venus”
sed ‘s:usr/bin/create:tmp/boing/boing_boing:g’ (filename)
#used colon instead of a forward slash as a delimiter to avoid confusion in modifying the
directory path.

sed ‘s/[^0-9]/*/g (filename), this command tells bash to find all characters that are NOT in
between 0-9, which basically means all other non-numerical characters in a file. When you run
this file using this file,

101 Dalmatians
202 Foreigners
3 Firemen
it will display an output like this:

101 **********
202 **********
3 *******

The caret can be described as the english word NOT.

You can also cram multiple commands in a single line command by placing a delimiter in
between and starting the s/…./…./ again like in this example: sed
‘s/gundam/zaku/g:s/bawoo/crimson/g’ (filename) . This command will replace the word
“gundam” with “zaku” and the word “bawoo” with “crimson. Again, you can use this format
multiple times in one line, you just have to make sure that you put a delimiter in between
substitution commands.
sed ‘s/\w*.//’ (filename) this command removes the first word in each succeeding line + the
space before the second word. the “\w*” is the variable that tell the command to “match any
alphanumeric or underscore character that appears 0 or more times followed by any other
character”.
if you write the same command without the period, it will just remove the first word and will leave
the space that existed before the second word. If you want to replace the last word in a line, you
can type the command like this: sed ‘s/\w*$//’ (filename) , this command will remove the last
word in each line. Like the previous sed command, this uses the $ to specify to read from the
last word in each line.

Creating Linux Menus

;; or double semicolon is the option separator in for a case statement. This bit of information is
important to note when creating interactive menus in a bash script.

like in the if statements (where you end your argument with a fi, you end your case statements
in esac, its case spelled backwards in case you are wondering.

the -e option in echo is used to “enable interpretation of backslash escapes” in the cases like

You might also like