Professional Documents
Culture Documents
KaliLinux T2 EN PDF
KaliLinux T2 EN PDF
anytime, anywhere.
email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
Table of Contents
Filesystem4
Comparison of the Filesystems 5
File Permissions 5
Permission Control 6
Copying Files 9
Deleting Files 10
Searching11
Other Ways of Reading Files 14
Command Glossary 17
email: contact@cyberskiller.com 3
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
Filesystem
Operating system is a very sophisticated program, which basic function is to manage
operations that a computer does. Such system contains many modules that are responsi-
ble for controlling different computer parts and other programs. A few examples of this
modules are: a graphical user interface, a text-based user interface, a system kernel and
a filesystem.
We’ve heard about the filesystem for the first time during last class. It’s a kind of a sub-
system that manages writing and reading data on the disk.
The most usual way to visualize data processing is to imagine streams of zeroes and
ones „flying” with a very high speed through the internals of the computer. These data
has to be saved somewhere, so it can be used later - for example it can be saved on a
hard drive.
Is one able to tell anything about files and folders saved on the disk in a binary format?
01010100 01000111 00111001 01111001 01011010 01010111 00110000
01100111 01100001 01011000 01000010 01111010 01100100 01010111
00110000 01100111 01011010 01000111 00111001 01110011 01100010
00110011 01001001 01100111 01100011 00110010 01101100 00110000
01001001 01000111 01000110 01110100 01011010 01011000 01010001
01110011 01001001 01000111 01001110 01110110 01100010 01101110
01001110 01101100 01011001 00110011 01010010 01101100 01100100
01001000 01010110 01111001 01001001 01000111 01000110 01101011
To be honest, the operating system alone cannot read this data. The filesystem is the
translator between binary and „here is the file, and here the folder” language. It’s the
filesystem that knows what those zeroes and ones mean and talks to the operating sys-
tem about it.
4 email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
Many differences are under the hood and refer to how those systems manage different
kinds of memories (HDD or SSD) and how good they are at recovering from failures (eg.
hard drive failures - how easy will it be to recover a file).
Filesystems in Linux and Mac OS X operating systems have an extra feature, which is
not present in a Windows filesystem - an advanced file permissions system.
File Permissions
The ls -l showed much more information than was needed. Let’s take an another look
at the output of this command.
The most incomprehensible part was the first column - eg. drwxr-xr-x. It was actually
a shortened notation of the permissions that this file or folder has.
email: contact@cyberskiller.com 5
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
Users in the Linux and Mac OS X systems belong to the groups. Usually those gro-
ups does not have any impact on an average user, but most often are used in terms
of special system users, who are given permissions all at once. Users can also not
belong to any group, then their group is denoted by their username.
Each rwx notation denotes information of each of the following three permissions:
• r (read) denotes the right to open the file,
• w (write) denotes the right to modify the file,
• x (execute) denotes the right to execute the file (run as a program).
The Linux system has an additional security that prevents unauthorized program
execution - it’s the x permission. If a system administrator revokes the x permission
for a user home folder, they cannot run any program of theirs (eg. a program down-
loaded from the internet). Those settings cannot be omitted in any way.
It is worth mentioning that in case of folders, the x option means the permission to
open it, but in case of files it means the permission to execute.
Permission Control
The file and folders permissions can be changes using the chmod command.
To grant or revoke file permissions, one needs to choose who they modify permissions
for (u, g, o or a), whether they want to grant or revoke them (+, -) and the permissions
itself (r, w or x).
6 email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
Option Explaination
u File owner, user.
g Group.
o Others.
a All, everyone.
Examples:
• chmod u+r plik.txt - for the „file.txt” file, for the file owner (user; u) grant (+) a
right to read (r),
• chmod g-w plik.txt - for the „file.txt” file, for the group (g) revoke (-) a right to
modify (w).
One could try to change the permissions of the projects folder that was created during
last subject. Then, one could check how this folder would operate after revoking the per-
missions to read from everyone.
The directory listing shows that chmod a-r projects revoked the rights to read
from everyone - the owner, the group and the other users (d-wx--x--x).
This folder can be entered and attempt to list its contents can be made.
email: contact@cyberskiller.com 7
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
Despite making it into the folder, the user does not have the rights to read it, so they
cannot see its content. If they would like to see its content, they would have to grant the
permission to read to themselves.
The chmod a+r . command will grant the read permission to everyone for the current
folder.
In this case a shortened „.” notation was used, which means the current directory.
After running this command ls does not return any error. One could check, what permis-
sions this folder has now.
As it can be seen, the directory permissions are drwxr-xr-x, so permissions were set
just as they were planned.
Pic. 7. Revoking the right to write the projects folder from everyone.
Not having the rights to write, one cannot create a new file called „something” (or any
other).
8 email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
Copying Files
Apart from permission management commands, Linux allows to do more „ordinary” file
and folder operations.
One of them is copying files and folders. To create a sample file for copying in a home
folder, one could use the touch project1 command.
Then, one could copy this project into the projects folder. A cp command will be used for
that.
As a first argument one ought to specify the file that needs to be copied, and as a second
argument - the destination, where they would like to copy this file to. For example if one
would like to copy a project1 file into the projects folder, they should use cp project1
projects/.
If the projects folder were not restored before, the command will possibly result in
an error: cp: cannot create regular file ‚projects/project1’:
Permission denied. To restore the permissions, use chmod u+w projects.
During copying, a new name for the file can be specified. To do this, apart from the desti-
nation folder, we need to specify this new name.
One can copy project1 file once more, but this time entering the destination name of
project2 as target.
Pic. 10. Copying the project1 into projects under the name of project2.
email: contact@cyberskiller.com 9
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
What if the user would like to copy a whole folder? For example, one could create a new
folder my_works, where the projects folder will be copied into.
Pic. 9. A failed attempt of copying projects folder into a new folder my_works.
The cp projects my_works command fails. It’s caused by the fact that by default,
the cp command ommits folders - so that a user cannot copy too much by accident. To
copy the whole folder, use the -r (recursive) option.
Pic. 10. A successful attempt of copying the projects folder into my_works.
Deleting Files
The project1 file can become useless after copying to a new destination. To delete files,
use the rm command.
The rm project1 command will remove the project1 file. It can be confirmed this by
running ls.
10 email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
Since the projects folder had been copied into my_works, the original folder can be safely
deleted (as it isn’t needed anymore).
Similar to how cp works, rm will not delete folders by default. The -r (recursive) option
is needed. Only rm -r projects allows to remove the projects directory.
Searching
While storing a lot of documents, photos or other files on a computer, it can be hard to
navigate through them. Then usually a search comes very useful, which can find what we
want.
find [folder]
By providing only the folder name, find will search for every single file that exists in this
folder. It’s not always what the user wants, and sometimes it might be less readable than
manually traversing folders looking for a specific file.
email: contact@cyberskiller.com 11
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
But find gives the user a lot of options to choose from, which make this command more
useful.
While searching by name, one can also use wildcards. Those include * and ?.
* - this symbol means that in its place can be any sequence of characters,
? - this symbol means that in its place can only be a single undefined character.
If one would like to search for any files and folders beginning with „project”, they would
have to run:
find . -name "project*"
Pic. 16. Searching for all files and folders beginning with „project”.
Searching by type
In the previous example the results included files and folders matching the pattern.
What if one would like to get files only?
12 email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
The search parameters can be combined. Let’s try to search for files beginning with „pro-
ject”.
As it can be seen in the picture 17, find . -name "project*" -type f will return
only two results - the project1 i project2 files.
Searching by owner
A search by the file owner can also be performed.
For example, one can display all files of a user from their home directory.
Searching by size
find [folder] -size [size][unit]
To search for all files with a size of 0 bytes, run find . -size 0c.
email: contact@cyberskiller.com 13
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
Or to search for files with a size of 4096 bytes (4 kilobytes) - find . -size 4k.
A less command comes very handy in those cases, which gives a lot more features
helpful for reading files.
Let’s try to display a .bash_history file that contains the whole history of entered com-
mands - to do this, run less .bash_history in the home folder.
Pic. 20. Displaying the command history in less (on the left) and cat (on the right).
14 email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
What is more interesting, we would not see a prompt after running this command unlike
with cat, and the beginning of the file will be displayed.
The less command is a command that does not quit immediately after running. It’s in
fact a program which allows us to browse file content.
One can scroll through the file by using the arrows (what was impossible in cat).
If one hits the end of the file, a Pattern not found (press ENTER) message can
be shown. You can try to go back to the previous search results (N) or press return (enter
key) to finish searching.
When the user reaches the end of the file, an (END) message will be shown.
One can also use those commands to go to an exact line. Just enter the wanted line
number and press „g”.
email: contact@cyberskiller.com 15
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
For example, to go to the 10th line, enter 10g. After entering this command, less will
take you to the entered line, and the command will dissappear.
One can also display the line numbers by entering -N (confirmed by the return key).
Exiting less
To exit less and return to the Linux shell, press q.
16 email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
Command Glossary
chmod [options] permissions file
Changes the permissions of a file or folder.
Permissions:
Written in a notation of xyz, where:
• x denotes: who the permissions are changes for (u - the file owner, g - the group,
o - the others, a - everyone),
• y denotes: what is being done (+ - grant, - - revoke),
• z denotes: what permissions (r - read, w - write, x - execute)
Options:
-R Allows to change the permissions of all files in a folder.
Options:
-r Allows to copy a folder.
rm [options] file
Removes a file.
Options:
-r Allows to remove a folder.
Options:
-name name Searches by name. Allows for wildcards such as „?” or „*”.
-type type Searches by type. f - file and d - directory.
-size nunit Searches by unit, where n is the amount and unit might be
one of: c - bytes, k - kilobytes, M - megabytes, G - gigabytes.
-user user Searches by the file owner.
email: contact@cyberskiller.com 17
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
less file
Allows one to read a file.
18 email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.
email: contact@cyberskiller.com 19
cyberskiller.com