# Linuxfun exercises **Taken from [this](http://linux-training.be/linuxfun.pdf) pdf** ## 12.9. practice: commands and arguments 1. How many arguments are in this line (not counting the command itself). `touch '/etc/cron/cron.allow' 'file 42.txt' "file 33.txt"` 2. Is `tac` a shell builtin command ? 3. Is there an existing alias for `rm`? 4. Read the man page of `rm`, make sure you understand the `-i` option of rm. Create and remove a file to test the -i option. 5. Execute: `alias rm='rm -i'` . Test your alias with a test file. Does this work as expected? 6. List all current aliases. 7. Create an alias called `city` that echoes your hometown. 7. Use your alias to test that it works. 8. Execute `set -x` to display shell expansion for every command. 9. Test the functionality of `set -x` by executing your city and rm aliases. 10. Execute `set +x` to stop displaying shell expansion. 11. Remove your city alias. 12. What is the location of the `cat` and the `passwd` commands? 13. Explain the difference between the following commands: * `echo` * `/bin/echo ` 14. Explain the difference between the following commands: * `echo Hello` * `echo -n Hello ` 15. Display A B C with two spaces between B and C. 16. (optional) Complete the following command (do not use spaces) to display exactly the following output: ``` 4+4 =8 10+14 =24 ``` 17. Use `echo` to display the following exactly: `??\\` ## 13.9. practice: control operators 0. Each question can be answered by one command line! 1. When you type `passwd`, which file is executed? 2. What kind of file is that? 3. Execute the `pwd` command twice. (remember 0.) 4. Execute `ls` after `cd /etc`, but only if `cd /etc` did not error. 5. Execute `cd /etc` after `cd etc,` but only if `cd etc` fails. 6. Echo `it worked` when `touch test42` works, and echo `it failed` when the touch failed. All on one command line as a normal user (not root). Test this line in your home directory and in `/bin/`. 7. Execute `sleep 6`, what is this command doing ? 8. Execute `sleep 200` in background (do not wait for it to finish). 9. Write a command line that executes `rm file55`. Your command line should print `success` if file55 is removed, and print `failed` if there was a problem. 10. (optional) Use echo to display `"Hello World with strange' characters \ * [ } ~ \ \ ."` (including all quotes) ## 14.13. practice: shell variables 1. Use echo to display `Hello` followed by your `username`. (use a bash variable!) 2. Create a variable `answer` with a value of 42. 3. Copy the value of `$LANG` to `$MyLANG`. 4. List all current shell variables. 5. List all exported shell variables. 6. Do the `env` and `set` commands display your variable? 6. Destroy your answer variable. 7. Create two variables, and export one of them. 8. Display the exported variable in an interactive child shell. 9. Create a variable, give it the value `Dumb`, create another variable with value `do`. Use `echo` and the two variables to `echo Dumbledore`. 10. Find the list of backslash escaped characters in the manual of bash. Add the time to your PS1 prompt. ## 15.3. practice: shell embedding 1. Find the list of shell options in the man page of bash. What is the difference between `set -u` and `set -o nounset`? 2. Activate `nounset` in your shell. Test that it shows an error message when using non-existing variables. 3. Deactivate `nounset`. 4. Execute `cd /var` and `ls` in an embedded shell. The echo command is only needed to show the result of the ls command. Omitting will result in the shell trying to execute the first file as a command. 5. Create the variable `embvar` in an embedded shell and echo it. Does the variable exist in your current shell now ? 6. Explain what `set -x` does. Can this be useful ? 7. (optional) Given the following screenshot, add exactly four characters to that command line so that the total output is `FirstMiddleLast`. `[paul@RHEL4b ~]$ echo First; echo Middle; echo Last` 8. Display a long listing (`ls -l`) of the `passwd` command using the `which` command inside an embedded shell. ## 16.12. practice: shell history 1. Issue the command `echo The answer to the meaning of life, the universe and everything is 42.`. 2. Repeat the previous command using only two characters (there are two solutions!) 3. Display the last 5 commands you typed. 4. Issue the long echo from question 1 again, using the line numbers you received from the command in question 3. 5. How many commands can be kept in memory for your current shell session? 6. Where are these commands stored when exiting the shell? 7. How many commands can be written to the history file when exiting your current shell session? 8. Make sure your current bash shell remembers the next 5000 commands you type. 9. Open more than one console (by press Ctrl-shift-t in gnome-terminal, or by opening an extra putty.exe in MS Windows) with the same user account. When is command history written to the history file ? ## 17.7. practice: shell globbing 1. Create a test directory and enter it. 2. Create the following files : (the last one has 6 characters including a space) * `file1` * `file10` * `file11` * `file2` * `File2` * `File3` * `file33` * `fileAB` * `filea` * `fileA` * `fileAAA` * `file(` * `file 2` 3. List (with ls) all files starting with file 4. List (with ls) all files starting with File 5. List (with ls) all files starting with file and ending in a number. 6. List (with ls) all files starting with file and ending with a letter 7. List (with ls) all files starting with File and having a digit as fifth character. 8. List (with ls) all files starting with File and having a digit as fifth character and nothing else. 9. List (with ls) all files starting with a letter and ending in a number. 10. List (with ls) all files that have exactly five characters. 11. List (with ls) all files that start with f or F and end with 3 or A. 12. List (with ls) all files that start with f have i or R as second character and end in a number. 13. List all files that do not start with the letter F. 14. Copy the value of `$LANG` to `$MyLANG`. 15. Show the influence of `$LANG` in listing A-Z or a-z ranges. 16. You receive information that one of your servers was cracked, the cracker probably replaced the `ls` command. You know that the `echo` command is safe to use. Can echo replace ls ? How can you list the files in the current directory with echo? 17. Is there another command besides `cd` to change directories? ## 18.9. practice: input/output redirection 1. Activate the `noclobber` shell option. 2. Verify that `noclobber` is active by repeating an `ls` on `/etc/` with redirected output to a file. 3. When listing all shell options, which character represents the `noclobber` option ? 4. Deactivate the `noclobber` option. 5. Make sure you have two shells open on the same computer. Create an empty tailing.txt file. Then type `tail -f tailing.txt`. Use the second shell to append a line of text to that file. Verify that the first shell displays this line. 6. Create a file that contains the names of five people. Use `cat` and output redirection to create the file and use a here document to end the input. ## 19.13. practice: filters 1. Put a sorted list of all bash users in `bashusers.txt`. 2. Put a sorted list of all logged on users in `onlineusers.txt`. 3. Make a list of all filenames in `/etc` that contain the string `conf` in their filename. 4. Make a sorted list of all files in `/etc` that contain the case insensitive string `conf` in their filename. 5. Look at the output of `/sbin/ifconfig`. Write a line that displays only ip address and the subnet mask. 6. Write a line that removes all non-letters from a stream. 7. Write a line that receives a text file, and outputs all words on a separate line. 8. Write a spell checker on the command line. (There may be a dictionary in `/usr/share/dict/`.) ## 20.11. practice: basic Unix tools 1. Explain the difference between these two commands. This question is very important. If you don't know the answer, then look back at the shell chapter. * `find /data -name "*.txt"` * `find /data -name *.txt` 2. Explain the difference between these two statements. Will they both work when there are 200 .odf files in /data ? How about when there are 2 million .odf files ? * `find /data -name "*.odf" > data_odf.txt` * `find /data/*.odf > data_odf.txt` 3. Write a find command that finds all files created after January 30th 2010. 4. Write a find command that finds all `*.odf` files created in September 2009. 5. Count the number of `*.conf` files in /etc and all its subdirs. 6. Here are two commands that do the same thing: copy `*.odf` files to /backup/ . What would be a reason to replace the first command with the second ? Again, this is an important question. * `cp -r /data/*.odf /backup/` * `find /data -name "*.odf" -exec cp {} /backup/ \;` 7. Create a file called `loctest.txt`. Can you find this file with `locate`? Why not? How do you make locate find this file ? 8. Use `find` and `-exec` to rename all .htm files to .html. 9. Issue the `date` command. Now display the date in YYYY/MM/DD format. 10. Issue the `cal` command. Display a calendar of 1582 and 1752. Notice anything special ? ## 22.19. practice: vi(m) 1. Start the `vimtutor` and do some or all of the exercises. You might need to run `aptitude install vim` on xubuntu. 2. What 3 key sequence in command mode will duplicate the current line. 3. What 3 key sequence in command mode will switch two lines' place (line five becomes line six and line six becomes line five). 4. What 2 key sequence in command mode will switch a character's place with the next one. 5. vi can understand macro's. A macro can be recorded with q followed by the name of the macro. So qa will record the macro named a. Pressing q again will end the recording. You can recall the macro with @ followed by the name of the macro. Try this example: i 1 'Escape Key' qa yyp 'Ctrl a' q 5@a (Ctrl a will increase the number with one). 6. Copy `/etc/passwd` to your `~/passwd`. Open the last one in vi and press Ctrl v. Use the arrow keys to select a Visual Block, you can copy this with y or delete it with d. Try pasting it. 7. What does dwwP do when you are at the beginning of a word in a sentence ? ## 23.9. practice: introduction to scripting 0. Give each script a different name, keep them for later! 1. Write a script that outputs the name of a city. 2. Make sure the script runs in the bash shell. 3. Make sure the script runs in the Korn shell. 4. Create a script that defines two variables, and outputs their value. 5. The previous script does not influence your current shell (the variables do not exist outside of the script). Now run the script so that it influences your current shell. 6. Is there a shorter way to source the script ? 7. Comment your scripts so that you know what they are doing. ## 24.7. practice: scripting tests and loops 1. Write a script that uses a for loop to count from 3 to 7. 2. Write a script that uses a for loop to count from 1 to 17000. 3. Write a script that uses a while loop to count from 3 to 7. 4. Write a script that uses an until loop to count down from 8 to 4. 5. Write a script that counts the number of files ending in .txt in the current directory. 6. Wrap an if statement around the script so it is also correct when there are zero files ending in .txt. ## 25.7. practice: parameters and options 1. Write a script that receives four parameters, and outputs them in reverse order. 2. Write a script that receives two parameters (two filenames) and outputs whether those files exist. 3. Write a script that asks for a filename. Verify existence of the file, then verify that you own the file, and whether it is writable. If not, then make it writable. 4. Make a configuration file for the previous script. Put a logging switch in the config file, logging means writing detailed output of everything the script does to a log file in `/tmp`. ## 26.6. practice : more scripting 1. Write a script that asks for two numbers, and outputs the sum and product (as shown here). ``` Enter a number: 5 Enter another number: 2 Sum: 5 + 2 = 7 Product: 5 x 2 = 10 ``` 2. Improve the previous script to test that the numbers are between 1 and 100, exit with an error if necessary. 3. Improve the previous script to congratulate the user if the sum equals the product. 4. Write a script with a case insensitive case statement, using the shopt nocasematch option. The nocasematch option is reset to the value it had before the scripts started. 5. If time permits (or if you are waiting for other students to finish this practice), take a look at Linux system scripts in `/etc/init.d` and `/etc/rc.d` and try to understand them. Where does execution of a script start in `/etc/init.d/samba`? There are also some hidden scripts in `~`, we will discuss them later. ## 27.15. practice: introduction to users 1. Run a command that displays only your currently logged on user name. 2. Display a list of all logged on users. 3. Display a list of all logged on users including the command they are running at this very moment. 4. Display your user name and your unique user identification (userid). 5. Use `su` to switch to another user account (unless you are root, you will need the password of the other account). And get back to the previous account. 6. Now use `su -` to switch to another user and notice the difference. Note that `su -` gets you into the home directory of Tania. 7. Try to create a new user account (when using your normal user account). this should fail. (Details on adding user accounts are explained in the next chapter.) 8. Now try the same, but with `sudo` before your command. ## 28.13. practice: user management 1. Create a user account named serena, including a home directory and a description (or comment) that reads Serena Williams. Do all this in one single command. 2. Create a user named venus, including home directory, bash shell, a description that reads Venus Williams all in one single command. 3. Verify that both users have correct entries in `/etc/passwd`, `/etc/shadow` and `/etc/group`. 4. Verify that their home directory was created. 5. Create a user named einstime with `/bin/date` as his default logon shell. 7. What happens when you log on with the einstime user ? Can you think of a useful real world example for changing a user's login shell to an application ? 8. Create a file named `welcome.txt` and make sure every new user will see this file in their home directory. 9. Verify this setup by creating (and deleting) a test user account. 10. Change the default login shell for the serena user to `/bin/bash`. Verify before and after you make this change. ## 29.10. practice: user passwords 1. Set the password for serena to hunter2. 2. Also set a password for venus and then lock the venus user account with `usermod`. Verify the locking in `/etc/shadow` before and after you lock it. 3. Use `passwd -d` to disable the serena password. Verify the serena line in `/etc/shadow` before and after disabling. 4. What is the difference between locking a user account and disabling a user account's password like we just did with `usermod -L` and `passwd -d`? 5. Try changing the password of serena to serena as serena. 6. Make sure serena has to change her password in 10 days. 7. Make sure every new user needs to change their password every 10 days. 8. Take a backup as root of `/etc/shadow`. Use `vi` to copy an encrypted hunter2 hash from venus to serena. Can serena now log on with hunter2 as a password ? 9. Why use `vipw` instead of `vi`? What could be the problem when using `vi` or `vim`? 10. Use `chsh` to list all shells (only works on RHEL/CentOS/Fedora), and compare to `cat /etc/shells`. 11. Which `useradd` option allows you to name a home directory? 12. How can you see whether the password of user serena is locked or unlocked ? Give a solution with `grep` and a solution with `passwd`. ## 30.9. practice: user profiles 1. Make a list of all the profile files on your system. 2. Read the contents of each of these, often they source extra scripts. 3. Put a unique variable, alias and function in each of those files. 4. Try several different ways to obtain a shell (su, su -, ssh, tmux, gnome-terminal, Ctrl- alt-F1, ...) and verify which of your custom variables, aliases and function are present in your environment. 5. Do you also know the order in which they are executed? 6. When an application depends on a setting in `$HOME/.profile`, does it matter whether `$HOME/.bash_profile` exists or not? ## 31.10. practice: groups 1. Create the groups tennis, football and sports. 2. In one command, make venus a member of tennis and sports. 3. Rename the football group to foot. 4. Use `vi` to add serena to the tennis group. 5. Use the `id` command to verify that serena is a member of tennis. 6. Make someone responsible for managing group membership of foot and sports. Test that it works. ## 32.4. practice: standard file permissions 1. As normal user, create a directory `~/permissions`. Create a file owned by yourself in there. 2. Copy a file owned by root from `/etc/` to your permissions dir, who owns this file now ? 3. As root, create a file in the users `~/permissions` directory. 4. As normal user, look at who owns this file created by root. 5. Change the ownership of all files in `~/permissions` to yourself. 6. Make sure you have all rights to these files, and others can only read. 7. With `chmod`, is 770 the same as rwxrwx--- ? 8. With `chmod`, is 664 the same as r-xr-xr-- ? 9. With `chmod`, is 400 the same as r-------- ? 10. With `chmod`, is 734 the same as rwxr-xr-- ? 11. Display the `umask` in octal and in symbolic form. 11. Set the `umask` to 077, but use the symbolic format to set it. Verify that this works. 12. Create a file as root, give only read to others. Can a normal user read this file ? Test writing to this file with vi. 13. Create a file as normal user, give only read to others. Can another normal user read this file ? Test writing to this file with `vi`. 13. Can root read this file ? Can root write to this file with `vi`? 14. Create a directory that belongs to a group, where every member of that group can read and write to files, and create files. Make sure that people can only delete their own files. ## 33.5. practice: sticky, setuid and setgid bits 1. Set up a directory, owned by the group sports. 1. Members of the sports group should be able to create files in this directory. 1. All files created in this directory should be group-owned by the sports group. 1. Users should be able to delete only their own user-owned files. 1. Test that this works! 2. Verify the permissions on `/usr/bin/passwd`. Remove the `setuid`, then try changing your password as a normal user. Reset the permissions back and try again. 3. If time permits (or if you are waiting for other students to finish this practice), read about file attributes in the man page of `chattr` and `lsattr`. Try setting the i attribute on a file and test that it works. ## 35.6. practice: links 1. Create two files named `winter.txt` and `summer.txt`, put some text in them. 2. Create a hard link to `winter.txt` named `hlwinter.txt`. 3. Display the inode numbers of these three files, the hard links should have the same inode. 4. Use the `find` command to list the two hardlinked files 5. Everything about a file is in the inode, except two things : name them! 6. Create a symbolic link to `summer.txt` called `slsummer.txt`. 7. Find all files with inode number 2. What does this information tell you ? 8. Look at the directories `/etc/init.d/` `/etc/rc2.d/` `/etc/rc3.d/` ... do you see the links ? 9. Look in `/lib` with `ls -l`... 10. Use `find` to look in your home directory for regular files that do not(!) have one hard link.