Day 3
1. File and directory permissions:
When ls -l
typed in the console, something like this will show up on your screen: -rw-r--r-- 1 Philipp users 10400 Sep 27 08:52 sales.data
The first character or symbol could be one of the following (Symbol – Type):
-
– Regular file.
d
– Directory.
l
– Symbolic link.
Other characters in the permission string can be (Symbol – Permission):
r
– Read
w
– Write
x
– Execute
Permission is different for files and directories. Read permission for example for a directory, enables you to see files in that directory.
Permission Categories:
Symbol – Category
u
– User
g
– Group
o
– Other
a
– All
Every user belongs to at least one group but can belong to many groups.
groups
– Displays a user’s groups.
id -Gn
– (As previous)
Secret Decoder Ring:
To go back to the example above: -rw-r--r-- 1 Philipp users 10400 Sep 27 08:52 sales.data
-
– Type
rw-
– User
r--
– Group
r--
– Other
If specific permission is not granted, a hyphen -
will take its place.
Changing Permissions (symbolic mode):
Item – Meaning
chmod
– Change mode command.
ugoa
– User category: user, group, other all.
+-=
– Add, subtract, or set permissions.
rwx
– Read, Write, Execute.
So for example: chmod g+w sales.data
This should change:
-rw-r--r-- 1 Philipp users 10400 Sep 27 08:52 sales.data
Into this:
-rw-rw-r-- 1 Philipp users 10400 Sep 27 08:52 sales.data
To add and subtract from multiple groups:
chmod u+rwx,g-x sales.data
Result:
-rwxrw-r-- 1 Philipp users 10400 Sep 27 08:52 sales.data
Change all:
chmod a=r sales.data
Will result in:
-r--r--r-- 1 Philipp users 10400 Sep 27 08:52 sales.data
Octal mode permission change examples:
chmod 700
– “-rwx——”
chmod 755
– “-rwxr-xr-x”
chmod 664
– “-rw-rw-r–”
chmod 660
– “-rw-rw—-”
chmod 644
– “-rw-r–r–”
Change group of file:
chgrp sales sales.data
File Creation Mask:
If no mask was used permissions would be:
777 for directories
666 for files
The umask Command:
umask [-S] [mode]
umask
– Shows the mask used, i.e.: 0022
umask
– Shows the mask used in symbolic, i.e.: u=rwx,g=rx,o=rx
2. View Files and the Nano Editor:
The cat Command:
cat file
– Display the contents of file.
more file
– Browse through a text file.
less file
– More features than more.
head file
– Output the beginning (or top) portion of file (10 lines by default).
tail file
– Output the ending (or bottom) portion of file (10 lines by default).
Navigating a file:
space bar – Advance to the next page.
enter key – Advance one line.
q key – Quit viewing the file.
-n
– Number of lines.
tail -15 file.txt
– Output the last 15 lines at the ending (or bottom) portion of file.
tail -f file
– Display data as it is being written to the file.
To exit the real-time display hit ctrl+c
The Nano Editor:
nano
– Open Nano Editor.
pico
– Some Destributions use pico for the Nano Editor.
nano file.txt
– Open file.txt with the Nano Editor.
Use ctrl+x to exit the file.
Use ctrl+o to save the file.
Use ctrl+g to get help.
The Vi Editor:
vi file.txt
– Open file with Vi Editor.
vim file.txt
– m stands for improved, more features.
view file.txt
Open file with Vi Editor in read only mode.
There are three modes in the Vi Editor, you are always in one of three modes, by default when opening Vi Editor you are in command-mode.
All three modes:
Mode – Key-Binding
Command-Mode – Esc
Insert-Mode – i or a A
Line-Mode – By beginning command with a :.
Command-Mode Key-Bindings:
k – Up one line.
j – Down one line.
h – Left one character.
l – Right one character.
w – Right one word.
b – Left one word.
^ – Go to the beginning of the line.
$ – Go to the end of the line.
x – Delete character.
dw – Delete a word.
dd – Delete a line.
D – Delete from the current position.
r – Replace the current character.
cw – Change the current word.
cc – Change the current line.
c$ – Change the text from the current position.
c – Same as c$.
~ – Reverse the case of a character.
yy – Yank (copy) the current line.
y<position> – Yank the <position>.
p – Paste the most recent deleted or yanked text.
u – Undo.
Ctrl-R – Redo.
/<pattern> – Start a forward search.
?<pattern> – Start a reverse search.
When searching use lower case n to go to the next result, and upper case N to the previous result.
Insert-Mode Key-Bindings:
i – Insert at the cursor position.
I – Insert at the beginning of the line.
a – Append after the cursor position.
A – Append at the end of the line.
Line-Mode Key-Bindings:
:w – Writes (saves) the file.
:w! – Forces the file to be saved.
:q – Quit.
:q! – Quit without saving changes.
:wq! – Write and quit.
😡 – Same as :wq.
:n – Positions the cursor at line n.
:$ – Positions the cursor on the last line.
:set nu – Turn on line numbering.
:set nonu – Turn off line numbering.
:help [subcommand] – Get help.
Repeating Commands:</h4
k5
– Move up a line five times.
80i
– Insert 80 times.
80i_
– Insert 80 “_” characters.
vimtutor
– Tutorial for Vi Editor.
A Vim Cheat Sheet can be found here: https://www.linuxtrainingacademy.com/vim-cheat-sheet/.
The Emacs Editor:
emacs file.txt
– Edit file.txt.
C-<char> – Ctrl while pressing <char>.
M-<char> – “Meta” key (alt key) while pressing <char>. Esc key can also be used as the “Meta” key.
For example:
C-h – Help.
C-x, C-c – Exit.
C-x, C-s – Save the file.
C-h t – Built-in tutorial.
C-h k <key> – Describe key.
Emacs File Navigation:
C-p – Previous line.
C-n – Next line.
C-b – Backward one character.
C-f – Forward one character.
M-f – Forward one word.
M-b – Backward one word.
C-a – Go to the beginning of the line.
C-e – Go to the end of the line.
M-< – Go to the beginning of the file.
M->- Go to the end of the file.
C-d – Delete a character.
M-d – Delete a word.
C-k – Kill (cut).
C-y – Yank (paste).
C-x u – Undo.
C-s – Start a forward search.
C-r – Start a reverse search.
Emacs Repeat Commands:
C-u N <command> – Repeat <command> N times.
For example:
C-u 10 C-d
Find Files and Directories
find [path] [expression]
find options:
-name pattern
– Find files and directories that match pattern.
-iname pattern
– Like -name
, but ignores case.
-ls
– Performs an ls
on each of the found items.
-mtime days
– Find files that are days old.
-size num
– Find files that are of size num.
-newer file
– Finds files that are newer than file.
-exec command {} \;
– Run command against all the files that are found.
find -name *v
– Find anything with a character ‘v’ in it’s name.
find -name s*
– Find anything starting with a character ‘s’.
find -mtime example:
find . -mtime +10 -mtime -13
– Find files that are 10 or more, but smaller or equal to 13 days old…
find -size example:
find . -size +1M
– Find files that are larger than or equal to 1 megabyte. You can use K (kilo), M(mega), G(giga-byte).
locate pattern
– Similar to find
, but faster. Queries an index, results are not in real time. May not be enabled on all systems.
touch
– Create an empty file.
Graphical Editors:
emacs – Emacs has a graphical mode too.
gedit – The default text editor for Gnome.
gvim – The graphical version of vim.
kedit – The default text editor for the KDE.
Microsoft Word repalcement:
AbiWord – Microsoft Word alternative.
LibreOffice – Full office suite.
Kate – Source code editor.