Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Train for your career in computer science

anytime, anywhere.

Linux System Basics


02. File operations

email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.

Linux System Basics


02.File operations

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.

Pic. 1. A figurative representation of disk content.

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.

Comparison of the Filesystems


Filesystems, which are used nowadays, are usually very similar. Without deeper under-
standing how they work, they seem to do pretty much the same - store files and folders.
The proof of how much similar they are can be the fact that Windows uses a different fi-
lesystem than Linux and Mac OS X, but the general rule of operation is overall identical.

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.

Pic. 2. Running ls -l in the home folder.

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.

How to read this notation?


d rwx rwx rwx

It can be split it in four groups:


• First group (d): denotes that this entry is a directory. If instead of a d character an - is
present, this entry is a file, not a folder.
• Second group (rwx): denotes the permissions that the file owner (user) has.
• Third group (rwx): denotes the permissions that the group of the file has.
• Fourth group (rwx): denotes the permissions that others have.

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.

The drwxr-xr-x notation can be explained as follows:


• d means that this item is a directory,
• rwx means that the owner can read, modify and open this folder,
• r-x means that the group can read and open this folder,
• the last r-x means that the others can read and open this folder.

The notation of -rw-r-xr-- can be explained as follows:


• - means that this item is a file,
• rw- means that the owner can read and modify the file,
• r-x means that the group can only read and execute the file,
• r-- means that the others can only read this file.

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.

The name of chmod means change mode.

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.

Pic. 3. Revoking the rights to read.

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.

Pic. 4. Trying to list the directory content without the permis-


sion to read.

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.

Pic. 5. Restoring the directory permissions.

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.

Pic. 6. Checking the permission of the projects folder.

As it can be seen, the directory permissions are drwxr-xr-x, so permissions were set
just as they were planned.

What if the owner revoked the rights to write?

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).

The permission can be restored by running chmod u+w .

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.

The name of cp means copy.

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/.

Pic. 8. Copying project1 file into 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.

The cp project1 projects/project2 command not only copied the project1


file into the projects folder, but it also changed its name in this folder into project2. Since
then there are two files - the previously copied project1, and newly copied 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 name of rm means remove.

The rm project1 command will remove the project1 file. It can be confirmed this by
running ls.

Pic. 11. Removing the project1 file.

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).

Pic. 12. Unsuccessful attempt of removing the projects folder.

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.

Pic. 13. Removing the projects folder.

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.

In Linux this feature is provided by the find command.

The basic syntax is very easy.

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.

Pic. 14. A part of the output of find . in a home folder.

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.

Searching by the name


find [folder] -name [name]

By using -name, we can specify the file name that we seek.


For example, one can search for project1 in that way and find will tell its location.

Pic. 15. Searching for „project2” in the home directory..

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?

find [folder] -type [type]

The type can be one of the following:


f - file,
d - directory.

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”.

Pic. 17. Searching for all files beginning with „project.

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.

find [folder] -user [user]

For example, one can display all files of a user from their home directory.

Pic. 18. Searching for all files owned by user.

Searching by size
find [folder] -size [size][unit]

The unit can be one of the following:


c - bytes,
k - kilobytes,
M - megabytes,
G - gigabytes.

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.

Pic. 18. Searching for all files with a size of 0 bytes.

Or to search for files with a size of 4096 bytes (4 kilobytes) - find . -size 4k.

Pic. 19. Searching for all files with a size of 4 kilobytes.

Other Ways of Reading Files


Sometimes while trying to read the whole file, we might encounter a file so big, that
cat becomes inconvenient.

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).

Searching for a phrase


The less command has also a built-in search. Press / and enter the phrase that you’d
like to find. To start the search, press return (enter key).

Pic. 21. Entering the query for search in less.

To navigate through search results, use n and N.

n (lowecase „n”) goes to the next search result,


N (uppercase „n”; Shift + N) goes to the previous search result.

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.

Going to an exact line


Additionally less allows us to quickly navigate through lines in a file. The g and G are
used for that.

g (lowercase „g”) goes to the beginning of the file.


G (uppercase „G”; Shift + G) goes to the end of the file.

When the user reaches the end of the file, an (END) message will be shown.

Pic. 22. The end of the file in less.

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.

Pic. 23. Entering the line number in less.

One can also display the line numbers by entering -N (confirmed by the return key).

Pic. 24. Displaying the line numbers in less.

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.

cp [options] source_file destination_place


Copies file into the destination place. Give the possibility to change the filename during
copying.

Options:
-r Allows to copy a folder.

rm [options] file
Removes a file.

Options:
-r Allows to remove a folder.

find folder [options]


Searches for files and folders in a given directory.

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.

Available commands inside the program:


/phrase - searches for phrase,
n (during search) - goes to the next search result,
N (during search) - goes to the previous search result,
g - goes to the beginning of the file,
G - goes to the end of the file,
ng - goes to the n-th line,
-N - displays/hides line numbers,
q - exits the program.

18 email: contact@cyberskiller.com
cyberskiller.com
Train for your career in computer science
anytime, anywhere.

email: contact@cyberskiller.com 19
cyberskiller.com

You might also like