Lab 1 Matl and Exr
Lab 1 Matl and Exr
/
(root)
Data can be stored in a file. Each file has a filename. A label referring to
a particular file
Creating Files: There are many ways of creating a file. One of the
simplest is using the cat command:
$ cat > shopping_list
cucumber
bread
yoghurts
fish fingers
Note the greater-than sign (>) — this is necessary to create the file. The
text typed after the file name is written to a file with the specified name
Press Ctrl+D after a line-break to denote the end of the file and the shell
prompt gets displayed denoting the end of successful file creation
process
$ ls –R directoryname
Using wild characters ? and *
? stands for a single character and * for any number of characters.
To list all the filenames and directory names beginning with a
$ ls –l a*
To list all the filenames and directory names beginning with ‘a’ and
containing three or more characters.
$ ls –l a??
To list all the filenames and directory names with second letter ‘a’ and
containing any number of characters.
$ ls –l ?a*
There are many ways of viewing the contents of a file One of the simplest is
with the cat command:
$ cat shopping_list
cucumber
bread
yoghurts
fish fingers
To delete a file, use the rm (‘remove’) command. Simply pass the name of
the file to be deleted as an argument:
$ rm shopping_list
The file and its contents are removed. There is no recycle bin. There is no
‘unrm’ command. The ls command can be used to confirm the deletion
$ mv Coimbatore Mumbai
Similar to using cp then rm For both commands, the existing name is
specified as the first argument and the new name as the second. If a file with
the new name already exists, it is overwritten.
To remove a directory first and foremost the directory should be empty, and
we should be in the previous directory.
$ rmdir AMRITA
Command History
Often it is desired to repeat a previously-executed command. The shell keeps
a command history for this purpose. Use the Up and Down cursor keys to
scroll through the list of previous commands. Press Enter to execute the
displayed command. Commands can also be edited before being run. This is
particularly useful for fixing a typographic error in the previous command.
The Left and Right cursor keys navigate across a command. Extra characters
can be typed at any point. Backspace deletes characters to the left of the
cursor Del and Ctrl+D delete characters to the right Take care not to log out
by holding down Ctrl+D too long
Vi Quick Reference
Entering vi
% vi name edit name at top
% vi +n name edit name at line n
% vi + name edit file at the end
% vi -r list saved files
% vi -r name recover file name
% vi name1 name2 edit name1 first, rest via :n
% vi +/pat name search file for pat
File Manipulation
:w write back changes
:wq write and quit
:q quit
:q! quit and discard changes
:e name edit the file name
:e! reedit, discard changes
:e +n edit starting at line n
:w name write file name
:w! name overwrite file name
:n edit next file in arglist
Cursor Motion
back forward
letter ^H, h <space>
word end E, e
word beginning B, b W, w
sentence ( )
paragraph { }
section [[ ]]
line ends 0 $
1st char - +
same column ^P ^N, <linefeed>
specified line <line#>G <line#>G
1/2 screenful jump ^U ^D
screenful jump ^B ^F
Undo
u undo last change
U restore current line
"#p retrieve #th last delete
<esc> abandon incomplete command
:q! (drastic) quit without save
Insert
i before cursor
I before first nonblank
a after cursor
A after line
o open line below
O open line above
p paste last cut
<esc> terminates insert
Change
cw
<newword> change word
C substitute line
s<char> substitute char
S substitute lines
r<char> replace char
R replace chars (overstrike)
xp transpose chars (cut paste)
<esc> terminates change
Delete
x char under cursor
X char before cursor
dw word
de delete word but leave punctuation
dd line
#dd # lines
D rest of line
Miscellaneous Operations
J join lines
:sh run shell, then return
:!cmd execute a command, then return
:f show current file and line
^g same as :f
Numbers etc.
. (period) repeat last action
3. repeat it 3 times
3dd delete 3 lines
d3w or 3dw delete 3 words
3rx replace next 3 chars with x
The vi editor is a shell editor for Unix systems. It makes use of a
regular keyboard with an Escape key. On a DEC station the Escape key
is the F11 key. It works on all Unix computers. Use a Telnet or SSH
client and login to your web server. Enter your username and
password, then type
su -
at the command line. Enter your root password to get access to root.
Complete documentation is available by typing: man vi
START SESSION
vi filename (filename is the file to be edited)
UNDO
u - undo the last command
SCREEN COMMANDS (CTRL-X means pressing the CTRL key and the
X key
simultaneously)
CTRL-l - reprint current screen
CTRL-L - expose one more line at the top of the screen
CTRL-E - expose one more line at the bottom of the screen
CTRL-F - go forward one screen
CTRL-B - go back one screen
CTRL-D - page down half a screen
CTRL-U - page up half a screen
EXIT COMMANDS
ZZ - exit vi and save changes
:wq - write changes to current file and quit editing session
:q! - quit editing session (no changes are made)
Additional Commands:
--help option
With almost every command, ‘--help’ option shows usage summary for that command.
$ date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
whatis command
This command gives one line description about the command. It can be used for quick
reference for a command.
$ whatis date
date (1) - print or set the system date and time
$ whatis whatis
whatis (1) - display manual page descriptions
Manual Pages
‘--help’ option and ‘whatis’ command do not provide thorough information about the
command. For detailed information, Linux provides man pages and info pages. To see a
command's manual page, man command is used.
$ man date
The man pages are properly documented pages. They have following sections:
REPORTING BUGS: Link of website or mail-id where you can report any bug.
SEE ALSO: Any commands related to the command, for further reference.
With -k option, a search through man pages can be performed. This searches for a pattern in
the name and short description of a man page.
$ man -k gzip
gzip (1) - compress or expand files
lz (1) - gunzips and shows a listing of a gzip'd tar'd archive
tgz (1) - makes a gzip'd tar archive
uz (1) - gunzips and extracts a gzip'd tar'd archive
zforce (1) - force a '.gz' extension on all gzip files
touch command
For creating an empty file, use touch command.
If a file already exists, touch will update its time stamp.There are a lot of other methods to
create a new file, e.g. using a text editor like vi or gedit, or using redirection. Here is an
example of creating a file using redirection:
wc command
Word count
This command counts lines, words and letters of the input given to it.
$ wc /etc/passwd
35 57 1698 /etc/passwd
The /etc/passwd file has 35 lines, 57 words, and 1698 letters present in it.
grep command
Print the matched line, along with the 3 lines after it.
$ grep -r "ramesh" *
1. A library needs to organize its collection of electronic books on the hard drive. The
books are to be categorized as shown below:
2. Enter the following commands at the prompt and interpret the output:
a. echo hello world
b. passwd
c. hostname
d. last
e. man man
f. Execute the following commands in order and interpret the output:
i. ls
ii. ls > myoutput
iii. ls
iv. cat myoutput
g. man head
h. man tail
i. Execute the following commands in order and interpret the output:
i. ls
ii. ls | head -3
iii. ls | head -3 | tail -1 >newoutput
iv. Display the contents of newoutput