User Tools

Site Tools


Sidebar

os_cp:exercises-shell

In the exercises, one may also provide names of intractive programs (e.g., htop for process management, but if one chooses so, then one has to specify what needs to be done after starting the program in order to solve the exercise.
For instance, if the exercise states Provide a command, that will let an already running process called expensiveComputations use only the zeroth CPU core, then the correct asnwers are, among others:
taskset -c 0 -p $(pgrep expensiveComputations),
or start htop, look up expensiveComputations with the shortcut /, press 'a' to change affinity,
or even concisely htop, /expensiveComputations, a.

To do some of the exercises, one has to create files/directories mentioned in the exercise.


1. Introduction to Unix-like operating systems. Getting help. Browsing filesystem.

1.1. Using the system manual for bash, provide which switch to the ulimit bash built-in command controls the maximum number of open files.

1.2. Provide what is the page titled random in fourth section of the system manual about.

1.3. One of the manual pages titled signal explains what is the SIGILL signal. Find this fragment and provide the explanation (comment).

1.4. Look up in the system manual for the yt-dlp program an option that extracts only audio from the given link, and provide a command that will download only audio from https://www.youtube.com/watch?v=de2Hsvxaf8M.

1.5. Provide a command, that will log you SSH sever available at the alt.org address as the user nethack.

1.6. Provide a command, that logs you via SSH on the computer named slurm as your current user, without providing the username within the command1).

1.7. Provide a command, that outputs the current working directory.

1.8. Provide a command, that enters the directory db located in the directory var that is directly in the root directory.

1.9. Provide a command, that changes the directory to the Documents directory that is one directory up from your working directory.

1.10. Provide a single command, that displays the contents of .config and .local directories that both are in yout home directory.

1.11. Provide a command, that displays all files (including the hidden files) in the /tmp directory.

1.12. Provide a command, that lists files in the /usr/lib64/python/ directory so that the size of files are shown.

1.13. Tell what is the size in MB of the file /usr/bin/gdb and provide the command you used to tell that.

1.14. Provide a command, that displays the contents of the elementary school directory which is a subdirectory of the old photos directory (both directories have a space in the name).

1.15. Provide a name of the most recently modified file in the /var/log directory and provide the command you used to learn that.


2. Editing line in console. Console text editors.

2.1. Copy the file /home/jkonczak/so/old_mcdonald_mistyped.txt elsewhere, and run the /home/jkonczak/so/old_mcdonald_check.sh your_new_filename. Correct the spelling mistakes in the file, and provide the output of md5sum < your_new_filename command.


3. File operations.

3.1. Provide a single command that creates a directory 2024 in a directory data, creating thereby the data directory if it's missing.

3.2. Provide a single command that creates directories moje obrazy and moja muzyka.

3.3. Provide a command, that remove an empty directory Downloads.

3.4. Provide a command, that creates a (hard) link named commandHistory to the file .bash_history.

3.5. Provide a single command that creates a (hard) link to files ~/.config/mimeapps.list and .config/htop/htoprc in your home directory.

3.6. Provide a command, that creates a symbolic link to the file /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq named cpu0_frequency in the current directory.

3.7. Provide a command, that creates a symbolic link named up that points to the parent directory of the current directory.

3.8. Provide a command, that copies all files with the .jpg extension form the directory pictures to the directory /tmp/a.

3.9. Provide a command, that copies the directory .local from your home directory alongside with its contents to the desktop2) with the name the local files.

3.10. Podaj komendę (wykonywaną z katalogu domowego), którą stworzysz kopię pliku .bashrc pod nazwą .bashrc.bak tak, by kopia miała tę samą datę modyfikacji co oryginał.

3.11. Provide a command, that moves all files with the *.txt extension from your home directory to a notes directory.

3.12. Provide a command, that amends the name of the file haunted hose.png to haunted house.png.

3.13. Provide a command, that amends the name of the soembdoy directory that is located in the /tmp directory to somebody.

3.14. Provide a command, that removes the temporary file file from the /tmp directory.

3.15. Provide a command, that removes all contents from the .cache directory located in your home directory (without removing the directory itself).

3.16. Provide a command, that removes write-protected file readOnly without asking for confirmation.

3.17. Provide a command, that removes all files with the extensions .toc, .nav and .aux in the current directory.

3.18. Provide a command, that removes all files which name starts with try and ends with .c from the directory programming.


4. Processes.

4.1. Provide a command, that runs the lsof program and measures its run time.

4.2. Provide the process identifier of your current shell and provide the command you used to learn that.

4.3. Provide the name of the parent process of the process X (X is a program that draws 'windows' in X Window System). If you solve the exercises on polluks, look up parent of the pickup process.

4.4. Provide the list of arguments that were passed to the ntpd process running in the system.

4.5. Provide the name of the user that started the rpcbind program.

4.6. Provide the name of the program that uses most space in the main memory with its non-shared files (that is, consider resident set size only). Provide the command you used to learn that.

4.7. Run in another terminal the command sleep 1h, and in yet another sleep 2h. Provide a command, that will tell the process identifier of the process sleeping for an hour.

4.8. Run in another terminal the command sleep 1h. Provide a command, that will terminate the process from another terminal.

4.9. Run in another terminal the command mkpasswd. Provide a command, that will send a FPE signal to the process.

4.10. Run in another terminal the command mkpasswd. Provide commands, that will send a HUP signal to the shell in which the mkpasswd process is running.


5. Users. File Permissions.

5.1. Provide a command, that displays your username.

5.2. Provide a command, that displays to which groups the current user belongs.

5.3. Provide a command, that displays to which groups the root user belongs.

5.4. Provide to which user and which group the file /dev/console belongs and provide the command you used to learn that.

5.5. Provide to which user and which group the /var/spool/clientmqueue directory belongs3) and provide the command you used to learn that.

5.6. Tell and justify whether the user aiXYZ belonging to the group students can display the contents of the file /home/jkonczak/so/obscure/mooo on polluks.

5.7. Tell and justify whether the user aiXYZ belonging to the group students can display the contents of the file /home/jkonczak/so/obscure/mooo on polluks4).

5.8. Tell and justify whether the user foo belonging to the group staff can display the contents of the directory /home/jkonczak/so/dir on polluks.

5.9. Tell which users can see the contents of the file .bash_history in your home directory.

5.10. Provide a command, that revokes the users that do not belong to your group permission to read the file our secret data.txt.

5.11. Provide a command, that forbids access (read, write and execute) to your file ~/public_html/index.php (belonging to the group students) to other students, but does not forbid access to the http server (that does not belong to the grup students)5).

5.12. Provide a single command that sets the permissions to the file access_log so that the group can only write to the file, you can only read it, and the remaining users have no access to the file.

5.13. Provide a command, that changes the permissions to the /tmp/myDir directory so that the users apart from you can display the file /tmp/myDir/pub.txt, but cannot list the files in the /tmp/myDir directory.

5.14. Provide a command, that lets the user jkonczak belonging to the groups staff and lindev read and execute the exercise.sh file in your home directory on polluks, but forbids the user ai11111 belonging to the group students reading the file.

5.15. Tell when the contents of the file /etc/sudoers has been modified most recently.

5.16. Tell when the metadata of the file /etc/sudoers have been modified most recently.

5.17. Tell when the file /var/log/wtmp has been created6).

5.18. Provide a command, that changes the date od th last modification of the file treaty to 1992-2-7 11:55


6. Searching for files.

6.1. Provide a command, that searches for all files that contain the word plasma in your home directory.

6.2. Provide a command, that searches for all directories which contain their name the string qt, ignoring the letter case.

6.3. Provide a command, that searches for all ordinary files that end with .jpg or .jpeg in the directories Downloads and Dekstop in your home directory.

6.4. Provide a command, that searches for all files with size greater than 1MB that were modified within last week.


7. Redirections.

7.1. Provide a command, that executes and measures the run time of lsof command and puts the result of lsof in a file lsof.log

7.2. Provide a command, that appends the current date to the file lsof.log.

7.3. Provide a command, that displays the contents of the file /etc/protocols prepended with line numbers.

7.4. Provide commands that, by means of redirections, create a file named 1 with numbers from 1 to 5 (each in separate line), and file 5 with numbers starting from 5, incremented by 5, up to 25 (each in separate line).

7.5. Provide a command, that displays the contents of the files created in the previous exercise side by side.

7.6. Provide a single command, that displays the contents of the files created in the previous exercise one after another.

7.7. Provide a command, that created a file message containing the text Premission denined using redirections.

7.8. Provide a command, that executes the command find /var and discards the error messages.

7.9. Provide a command that will execute the command strace -e 'openat' ls ~ so that the result of the ls command ends up in the ls.out file, and the (output to standard error) messages of the strace program end up in the ls.strace file.

7.10. Provide a command, that runs in the background the program glxgears and discards all its output.

7.11. Provide a command, that runs wc replacing its stadard output with the file /etc/resolv.conf.

7.12. Provide a command, that tuns bc and passes to the standard input of that program the tow following lines: (4^4)^4 and 4^(4^4).


8. Pipes. Filters.

8.1. The file /proc/cpuinfo contains information about CPUs. Provide a pipeline that displays the clock speed of each CPU (all values ​​in MHz).
Example command output:

800.000
3345.481
…

8.2. Provide a pipeline that tells which combinations of access permission appear in the /etc/ directory and how many times each of these combinations is present there.
Example command output:

       2 rw-r-----
     102 rw-r--r--
…

8.3. Provide a pipeline that looks up in the English dictionary (/usr/share/myspell/en_GB.dic) eight-character words ending with less.
Example command output:

careless
Erchless
…

8.4. Provide a pipeline that looks up in the English dictionary (/usr/share/myspell/en_GB.dic) words ending with more and starting with a lowercase letter.
Example command output:

claymore
evermore
…

8.5. The /dev/urandom file is a random data generator. Provide a pipeline that, by filtering the file, displays a random 32-digit hexadecimal number.
Example command output:

05f5a888fe12ae981539adf3de9f3762

8.6. Provide a pipeline that, by using /dev/urandom, generates a list of 10 passwords, each 8 characters long, composed of lowercase letters and an underscore.
Example command output:

qtoemmiy
rjcpu_cl
…

8.7. Provide a pipeline that, counts how many bash processes are currently running (for all users).
Example command output:

8

8.8. Provide a pipeline that outputs a list the 5 most frequently issued commands with arguments from the shell history.
Example command output:

       5 make
       5 rm -rf *
       9 cd ..
      14 ./9
      48 ls

8.9. Provide a pipeline that outputs a list of the 5 most common file extensions in the /var/log/ directory. Skip files without extension. Consider all following the first dot as an extension.
Example command output:

      15 log
       5 gz
       2 log-20200927
       2 log-20200501.gz
       2 log-20200401.gz

8.10. Provide a pipeline that outputs a list of the 5 most common file extensions in the /var/log/ directory. Skip files without extension. Consider all following the last dot as an extension.
Example command output:

      18 log
      16 gz
       3 old
       2 log-20200927
       1 wtmp

8.11. Provide a pipeline that outputs a list of the three users with the most processes running. Skip the root user.
Example command output:

     163 user
       7 distcc
       2 sddm

8.12. Provide a command that finds out which header file in the directory /usr/include/ contains the declaration of the function SHA512_Init declaration .

8.13. Provide a pipeline that counts how many # characters are in the file found in the previous exercise.
Example command output:

36

8.14. Provide a command that displays the file from the previous exercise, skipping lines starting with '#'.

8.15. The /etc/services file contains known port names for the transport layer protocols. Provide a command that outputs the names of the protocols that have any named ports (the second column).
Example command output:

udp
tcp
…

8.16. Provide a pipeline that displays the number of hidden files and directories in your home directory.
Example command output:

172

8.17. The 'ip --statistics link show br0' command displays the number of bytes and packets sent and received by the network card. Provide a pipeline that will display only the number of bytes sent and received (in separate lines).
Example command output:

1345718968
686011084

8.18. Provide the five process IDs in the system that have the largest number child processes. Provide a pipeline that you used to learn this.
Example command output:

2
1
4120
3912
4108

8.19. Provide a pipeline that tells how many users present in the /etc/passwd file use the bash shell.
Example command output:

13

8.20. Provide a pipeline that outputs the names of files ending with .h that are included from any of the files in the /usr/include/infiniband/ directory.
Example command output:

arpa/inet.h
byteswap.h
emmintrin.h
…

8.21. Provide a pipeline that filters the output of 'ping -i 0.5 -c 10 put.poznan.pl', to display only the rtt times themselves. answers.
Sample command output:

1.20
0.654
…
0.776
0.760


9. Scripting - basics.

9.1. Provide the first line of an ordinary shell script that tells the operating system to interpret the file with /bin/sh program.

9.2. The file setup contains commands that initialize certain shell variables. Provide a command that executes the contents of this file in current shell so that the variables become available.

9.3. Provide a command that calculates md5sum of the combined output of the command lsusb and the output of the command date +%D.

9.4. Provide a command that redirects the combined output of the commands date and lsof to a file lsof.latest.log.

9.5. Provide a fragment of the script that creates a directory results and exits id creating the directory failed. You must not use if statement or any loop.


10. Job control.

10.1. Provide a keyboard shortcut that will normally interrupt and end the foreground process.

10.2. Provide a keyboard shortcut that will normally stop (i.e. put to sleep) the foreground process.

10.3. Run the xterm program from the terminal. Stop the program with the appropriate shortcut. Provide a command that will resume the xterm process in background (i.e. so that you can continue to use both the current terminal and the xterm program).

10.4. Provide a command, that runs the command perl -e "system'fortune'while sleep 5" in background.

10.5. Provide a command, that moves the process running in background to foreground.

10.6. Start mkpasswd in background first, and then start sleep 50 in background. Provide a command that resumes the mkpasswd as a foreground process.

10.7. Provide a command, that lists the processes running in background in the current shell.


11. Environment variables.

11.1. Provide a command, that sets the variable TEXT to the value That's "the" value (with all quotes intact).

11.2. Provide a command, that sets the variable FOO to the value baz so that programs run in this shell see the value of FOO. (You can verify this by running the perl -e 'print "Correct!\n" if $ENV{"FOO"} == "baz"' command).

11.3. Provide a command that lists all currently set variables.

11.4. Provide a fragment of shell script that checks if there are four arguments, and if not, then prints an error message and quits the script.

11.5. Provide a fragment of shell script that checks if the first argument names an existing file and the second argument names an existing directory, and if not, then prints an error message and quits the script.

11.6. Provide a fragment of shell script that checks if the second argument is greater than the second argument, and if not, then prints an error message and quits the script.

11.7. Provide a fragment of shell script that assigns to variable DEST the value of the second arguments if at least two arguments were provided, and ./destination/ otherwise.

11.8. Provide a fragment of shell script that calls a program cat with argument -nv and full list of arguments of this script. (That is, if the script was run as ./script foo "baz bar", then the fragment shall call cat -nv foo "baz bar".)


12. Command substitution and arithmetic expansion.

12.1. Provide a command that outputs There are N files in your home directory with N being replaced with the actual number.

12.2. Provide a script that creates if necessary and opens in the default editor the file notes_DATE.txt, where DATE is replaced by the current date (e.g., as output by date +%m_%d).

12.3. The truncate --size N f command resizes (creating if necessary) to N byte the file f. Provide a command that resizes the file 3m2k1b to a size of 3MB+2kB+1B (that is, 3*1024*1024+2*1024+1).

12.4. Provide a single command that outputs a random number greater or equal 100 and lesser than 1000. Use the RANDOM environment variable.


13. Reading input from user.

13.1. Write a shell script that reads two numbers from standard input and outputs their sum and product.

13.2. Write a fragment of a shell script that reads first name and last name from standard input until both are provided, outputting diagnostic messages if the user did not provide one of the names.


14. Control flow statements.

14.1. Provide commands that write for every file with the <tt>.rc</tt> extension in the <tt>/etc</tt> directory its name or path followed by its first and last line.

14.2. Provide a shell script that requires two arguments, and draws a rectangle of given width and height.
Example output of ./script.sh 15 4:

##############
#            #
#            #
##############

14.3. Provide a shell script that assumes that each its argument is a file name. For each file, the script shall count the number of lines beginning with # in each file. If there are no such lines, the script shall output No comments in filename, and shall output There are N comments in filename.

14.4. Provide a shell script that accepts a single argument being a directory name. The script goes through each file in the directory, and checks its mime type (with the command file --mime-type --brief filename). If the mime type starts with image, the scripts displays the file name and its size.

14.5. Provide a shell script that repeatedly checks if the file log exists, and as soon as the file appears, calls tail -f log.

14.6. Provide a shell script that acts as a timer. The script shall accept one argument, the number of seconds. The script shall output M minutes and N seconds remaining every second, and shall call xmessage expired when the time runs out.

14.7. Provide shell commands that output all powers of two that are smaller than the value of the process identifier of the current shell.
Example output:

0 1
1 2
2 4
3 8
4 16
...

14.8. Provide shell commands (that include a loop) that check if the files log.0, log.1, log.2, ... exist. Upon the first filename log.i that refers to a non-existent file, the loop shall finish and that name shall be assigned to a variable LOGFILE.

14.9. Provide a shell function named maketar that, when run as maketar foo, will execute tar cf foo.tar foo.

14.10. Provide a shell function named systemStatus that displays current date, runs uptime and free -m, and outputs two empty lines.

14.11. Provide a command that will cause the shell to generate a random fortune whenever the user presses Ctrl+c

14.12. Provide a fragment of a shell script that will remove file tmpfile whenever the script terminates.

1) You can verify if your command works issuing it from the polluks server
2) which is usually the directory Desktop in your home directory
3) such directory exists on polluks
4) depending on your approach, this may be tricky
5) Files that are put in the public_html subdirectory of the home directory of the user username are accessible at the URL https://sirius.cs.put.poznan.pl/~username/.
6) the exercise is doable if the filesystem records file birth dates; it is the case on polluks
os_cp/exercises-shell.txt · Last modified: 2024/04/04 00:49 by jkonczak