Appendix to TD1

Brief UNIX tutorial

1 — Using system manual pager : man

Question: Try the "man ls" command and check the different options available. What are the most useful options?

2 — User identifiers

Unix-like operating systems identify a user by a value called UID (User identifier), along with the group identifier named GID.

The password file maps textual user names to UIDs.

You can see your UID and GID by analyzing output of "getent passwd" command.

Use the "passwd" command to change your password.

To get your UID, try the "whoami" command.

3 — Environment variables

The environment variables are dynamic variables used by different processes (running programs) of an operating system for communicating.

Question: Try the following commands;

echo $USER: The name of the user,

echo $SHELL: The shell in use,

echo $HOME: The working directory,

echo $PATH: The list of paths to some executables.

Question : Try: env | more

This is used for listing the environment variables page by page.

4 — Owner and group permissions

The "ls" command list the contents of directories and files, as well as additional information about them. With no options, ls list the files contained in the current directory, sorting them alphabetically. With the "–l" option ("ls -l"), ls displays various information related to file permissions.

Permissions are defined by a list of 10 characters:

Example : drwxr-xr-x

Where the first symbol can be:

Permissions are grouped in three groups of three characters each, and each position in the group denotes a specific permission, in this order:

read (r), write (w), execute (x).

The first three characters (2-4) represent the permissions for the file’s owner.

For example, -rwxr-xr-- represents that the owner has read (r), write (w) and execute (x) permission.

The second group of the three characters (5-7) consists of the permissions for the group to which the file belongs.

For example:

-rwxr-xr-- represents that the group has read (r) and execute (x) permission, but no write (w) permission.

The last group of three characters (8-10) represents the permissions of everyone else.

For example, -rwxr-xr-- represents that there is only the read (r) permission.

ls -l

drwxr-xr-x   6 Sonia Sonia     4096 2019-01-29 23:09 Bureau
drwxr-x---   2 Sonia Sonia     4096 2019-01-22 22:46 Documents
lrwxrwxrwx   1 Sonia Sonia       26 2019-01-22 22:30 Examples -> /usr/share/example-content
-rw-r--r--   1 Sonia Sonia  1544881 2019-01-18 15:37 forum.xcf
drwxr-xr-x   7 Sonia Sonia     4096 2019-01-23 18:16 Images
drwxr-xr-x   2 Sonia Sonia     4096 2019-01-22 22:45 Modèles
drwxr-xr-x 267 Sonia Sonia    20480 2019-01-27 22:17 Musique
drwxr-xr-x   2 Sonia Sonia    4096 2019-01-22 22:45 Public
drwxr-xr-x   2 Sonia Sonia     4096 2019-01-26 13:14 Vidéos

Use the "touch" command to create two empty files file1 and file2 in the TD1 directory: "touch file1 file2"

Then Call "ls -l"

Question: What are the permissions for TD1, file1 and file2?

5 — How to modify the permissions

The command "chmod" (change mode) is used for modifying the permission of a file with the r, w and x symbols

For example:

chmod o-w file_3 removes the write permission for all users outside of the group owning the file.

chmod a+x adds the exec permission to everyone.

Copy file1 and file2 to the /tmp directory using " cp TD1/file* /tmp " command.

Modify the permission of file1 and file2 in /tmp so that : The other users can read and modify the file but not you. Is it possible?

Question: What is the command line allowing to modify the permissions on /tmp so that the users of the same group can execute the cd, mkdir and rm commands?

6 — File search

The "find " command is used for searching files and directories using some criteria and can also execute some scripts on the files or directories found.

find $HOME -name "INF442*" -size +1M -print > file3

All the files in the home directory with a name containing INF442 and with a size greater than 1M are returned and stored in file3.

In the following example:

find . \( -name core -o -name '*.o' \) -size +2 -exec rm {} \;

From the current directory, all the files named core or object files with the *.o extension are searched and then removed (the rm command is called on the files found).

Question: What is the following command doing? Use « man find » for documentation:

find $HOME -user $USER -size +2M -name 'INF442*' -exec ls -l {} \; | mail $USER@polytechnique.edu

7 — Data disk usage: "du " command

The following command: "du –k ~ | sort –nr | more"

dump out the disk usage of your directories, sort them with respect to their size in reverse order.

8 — Processes

When you execute a program on your Unix system, the system creates a special environment for that program.

A process, in simple terms, is an instance of a running program.

The operating system tracks processes through a five-digit ID number known as the pid or process identifier.

Question: Try the following command for getting the current process and check its pid: ps -ef .

9 — Unix signals

Signals provide a limited form of inter-process communication (IPC).

A signal is an asynchronous notification sent to a process, to report an event that occurred.

For example, by running "kill -SIGINT 501 " , the SIGINT signal is sent to the identified process with the pid 501.

SIGINT is also sent to the running process (And thus, terminates this process) when the user presses Ctrl+C.

10 — The grep command

The " grep " command is useful for finding words in files.

For example: grep "Cluster" INF442/*.cpp | wc -l

returns the number of lines containing the word "Cluster" in all the files with the extension .cpp in the INF442 directory.

ps -ef | grep $USER returns the list of processes that belong to you.

Question: What do the following lines do?

11 — Sequential and parallel execution

In Unix, commands can be executed sequentially or in parallel.

Commands separated by ";" will be executed sequentially.

The commands separated by "|" will be executed in parallel.

The pipe "|" is a communication tool between two commands.

The first command sends its result to the next command through the pipe.

For example: grep "Cluster" INF442/*.cpp | wc -l

returns the number of lines containing the word "Cluster" in all the files with the extension .cpp in the INF442 directory.

More precisely the command grep send the list of lines containing the word "Cluster" in all the files with the extension .cpp in the INF442 directory to the pipe and command wc count the number of lines sended by grep . Try "man grep" .