0% found this document useful (0 votes)
3 views

unix_corr

The document provides a comprehensive guide to navigating and manipulating a Unix-like system, covering topics such as file management, I/O redirection, user permissions, and scripting. It includes practical exercises with commands like cd, ls, cp, and chmod, along with questions to reinforce learning. Additionally, it features a section on reading CSV files and calculating customer balances through a bash script.

Uploaded by

Said Bouargalne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

unix_corr

The document provides a comprehensive guide to navigating and manipulating a Unix-like system, covering topics such as file management, I/O redirection, user permissions, and scripting. It includes practical exercises with commands like cd, ls, cp, and chmod, along with questions to reinforce learning. Additionally, it features a section on reading CSV files and calculating customer balances through a bash script.

Uploaded by

Said Bouargalne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Getting to know a Unix-like system

Jorge E. Mendoza and Ronan Bocquillon

1 Wondering around your system


Open a terminal and navigate to the /bin directory using the cd command.
Questions
1. Who is the owner of file bash? Who can override it?
ls -l bash

2. What kind of file is znew?


file znew
3. What does the first line of znew say?
head -n 1 znew

4. What kind of file is sh and what does it mean?


file sh
5. List all the files whose name starts with an m character?
ls m*

Tips: cd, file, head, ls and stat.

2 Manipulating files and directories


Navigate to your Documents directory.
Questions

6. Create a local directory called Unix-TD1 and navigate to it.


mkdir Unix-TD1; cd Unix-TD1
7. Use the nano text editor to create a file called myAnimals.txt in the current directory, then
fill in it with the following list of animals (one animal per line) — fox, bear, eagle, deer, panda,
chicken, dog, elk, crab, ant, cat.
nano myAnimals.txt

8. Make a copy of your file and stock it in a new subdirectory called bacup.
mkdir bacup; cp myAnimals.txt bacup/
9. Now you realize you misspelled the name of the backup directory (you forgot a k), rename it
to backup.
mv bacup backup

Tips: cp, mkdir, mv and nano.

1
3 Redirecting I/O and basic piping
Navigate back from backup to Unix-TD1.
Questions
10. Using command echo (first read the manual page typing man echo) and the appropriate I/O
redirection, add “rat” to the animal list in your myAnimals.txt file.
echo rat >> myAnimals.txt

11. Create a new file called myAnimals2.txt containing the following list of animals (one per line)
— fox, alligator, gorilla, horse, dog, manatee, dove.
nano myAnimals2.txt
12. If one combines myAnimals.txt and myAnimals2.txt files, how many words and how many
lines are there? Use a pipeline of commands cat and wc to answer the question.
cat myAnimals.txt myAnimals2.txt | wc
13. Write a file called myCombinedAnimals.txt, containing the names of all the animals in your
myAnimals.txt and myAnimals2.txt files. The file should be sorted alphabetically and should
not contain repeated values.
cat myAnimals.txt myAnimals2.txt | sort | uniq > myCombinedAnimals.txt

14. Now using a single pipeline, write a new file called MYcOMBINEDaNIMALS.txt where the names
of the animals are written in capital case.
cat myCombinedAnimals.txt | tr a-z A-Z > MYcOMBINEDaNIMALS.txt
15. How many words in your MYcOMBINEDaNIMALS.txt file contain an A character?
grep "A" MYcOMBINEDaNIMALS.txt | wc -l

16. How many words do not contain an A character? Write the list in a file called noA.txt.
grep -v "A" MYcOMBINEDaNIMALS.txt > noA.txt

Tips: cat, echo, grep, sort, tr, uniq and wc.

4 Some new commands and a bit more piping


Open a new terminal.
Questions
17. What do the following statements do?
(a) ls -lt | head -n 20
(displays the 20 newest files in the current directory)
(b) du | sort --numeric-sort --reverse
(displays the list of directories and how much space they consume, sorted from the largest
to the smallest)
(c) rev < f1 > f2
(reverses the order of characters in every line of f1 and writes the output to f2)
(d) find . -type f -print | wc -l
(displays the total number of files in the current directory and all its subdirectories)

2
5 Creating users and changing permissions
Navigate to your Unix-TD1 directory.
Questions
18. Change the permissions of file myAnimals.txt so nobody can write the file.
chmod 555 myAnimals.txt (or even better chmod a-w myAnimals.txt)
19. Execute echo lombriz > myAnimals.txt. What happens?
(permission denied)
20. Create a user called “other” with password “o”.
sudo useradd other; sudo passwd other
21. Transfer the ownership of file myAnimals.txt to user other.
sudo chown other myAnimals.txt
22. Now try to change the permissions of file myAnimals.txt so everybody can read, write and
execute the file. What happens? Why?
chmod 777 myAnimals.txt (it fails because you are not the owner of this file anymore)
23. Now delete user other. What happened to the myAnimals.txt file?
sudo userdel other; stat myAnimals.txt (the file’s owner is the user whose UID is 1001
⇒ this user does not exist anymore ⇒ root should take ownership of this file)

Tips: chmod, chown, passwd, sudo useradd and userdel.

6 My first script
Create a local directory called scripts and navigate to it.
Questions
24. Write and execute a bash script that:
(a) prompts the user for two numbers using the read builtin command,
(b) checks that both of them are positive (going back to the previous step if not),
(c) computes their mean,
(d) outputs the result if it is greater than or equal to 5,
(e) outputs an error message otherwise.

 
# !/ bin / bash
n1 = -1
n2 = -1
until [[ $n1 - ge 0 && $n2 - ge 0 ]]; do
echo -n " number ␣ 1 ␣ = ␣ " && read n1
echo -n " number ␣ 2 ␣ = ␣ " && read n2
done
let mean =( $n1 + $n2 )/2
if [ $mean - ge 5 ]; then
echo " mean ␣ = ␣ $mean "
else
echo " Mean ␣ is ␣ less ␣ than ␣ 5 " >&2
exit 1
fi
 
(correction)

3
7 Reading a well formatted CSV file
Let us consider that you are system administrator in a well-known banking company. The following
(very unsecured) file registers all customer transactions.

username ; date ; transaction ; comment


Lara ;1557 538374;1 000; income
Clad ;1560568039;19; drop
Lara ;1560868025; -120; bow

Questions
25. Create and fill in that file. Call it data.csv.

26. Write a bash script that outputs the current balance of a customer list.
./ balance . sh data . csv Lara Geralt Clad
Lara 880
Geralt 0
Clad 19

 
# !/ bin / bash
function balance ()
{
local IFS = " ; "
local balance =0
while read username date transaction comment
do
if [ " $username " == " $2 " ]
then
let balance = balance + $transaction
fi
done < $1
printf " % -10 s ␣ % d \ n " $2 $balance
}
if [ $ # - eq 0 ] || ! [ -f $1 ]
then
echo " script ␣ filename ␣ [ username ]... " >&2
exit 1
fi
for customer in " $ { @ :2} "
do
balance $1 $customer
done
 
(correction)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy