(for Real Time Systems subject in Automatic and Robotics on Poznan University of Technology)
Course repo:
https://github.com/PUT-JUG/proj-scr
Expand!
-
Change your own password. Then go back to the default password.
Answer
passwd
-
Check your own ID and the groups you belong to.
Answer
id
-
Check who is currently logged into the system.
Answer
whoami
-
See the description of the directory structure.
Answer
man 7 hier
-
View the contents of your home directory.
Answer
ls ~
-
List the contents of the primary directories on your system (e.g. /dev, /etc, /home, /usr).
Answer
ls /etc /dev /etc /home /usr
-
Create directory cat1 in your home directory.
Answer
mkdir ~/cat1
-
In directory cat1, create a directory structure with one command: cat2/cat3/cat4.
Answer
mkdir -p ~/cat1/cat2/cat3/cat4
-
Delete the entire directory structure of cat3/cat4 in one command.
Answer
rm -r ~/cat1/cat2/cat3
-
Create files with any names with extensions .txt and .c in your home directory (2-3 files with each extension)
Answer
touch ~/xD.txt ~/xDD.txt ~/xDDD.txt ~/y.c ~/y.cc
-
Copy all the files from the home directory with the extension .txt to the directory cat1 with one command.
Answer
cp ./*.txt ~/cat1
-
Copy all files from the home directory with the extension .c to the directory kat2 with one command.
Answer
cp ./*.c ~/cat1/cat2
-
Copy the entire directory structure of kat1 creating an analogous structure called cat1b.
Answer
cp -r ~/cat1 ~/cat1b
-
Delete all files in cat1/cat2.
Answer
rm ~/kat1/kat2/*
-
Delete the entire directory structure of cat1b with one command.
Answer
rm -r ~/cat1b
-
Rename any file in directory cat1.
Answer
mv ~/cat1/x.txt ~/cat1/xx.txt
-
Move the directory cat1/cat2 to your home directory, thus creating the directory cat2b.
Answer
mv ~/cat1 ~/cat2b
-
Using the find program, find all files that have the word mozilla in the name and are located in subdirectories of the /usr directory.
Answer
find /usr -name mozilla
-
Using the find program, find all the bin directories that are in the /usr directory.
Answer
find /usr -type d -name bin
-
Copy all regular files between 10 and 100 bytes from /usr/bin to cat1/cat2 (use the find command with the -exec parameter).
Answer
find /usr/bin -size +10 -size -100 -exec cp {} ~/cat1/cat2 \;
-
In your home directory, create a file named file.txt - check the access rights to it.
Answer
touch ~/file.txt | ls -l ~
-
For file.txt, add write access for other users.
Answer
chmod a+w ~/file.txt
-
For file.txt, subtract the owner's write permission.
Answer
chmod u-w ~/file.txt
-
For file.txt, add execute permission for all users.
Answer
chmod +x ~/file.txt
-
For file file.txt and all users, allow only file read.
Answer
chmod 444 ~/file.txt
-
For file file.txt, restore the original rights using numerical notation.
Answer
chmod 664 ~/file.txt
-
Create a link to file.txt named file2.txt in your home directory.
Answer
ln ~/file.txt ~/file2.txt
-
Create a symbolic link to directory file1/file2 named abc in your home directory.
Answer
ln -s ~/file1/file2 ~/abc
-
View the system help for all the commands presented in the class.
Answer
your_command --help
Expand!
-
List your own processes with the ps command. Compare the results with the results of the ps x and ps ax commands.
Answer
a - This option prints the running processes from all users.
x - This option prints the processes those have not been executed from the terminal. -
Log in to the system several times through virtual consoles or by opening a new window in the graphical environment. Always check the name of the terminal you are working on with the tty command.
Answer
tty /dev/pts/{console_number}
-
Display the process hierarchy with the pstree command.
Answer
pstree
-
View the list of processes with the top command, sorting it by CPU usage and memory usage (check the -o switch)
Answer
top top -o %MEM
-
Follow these steps in order:
-
Change the priority of the bash shell you are currently in to 10.
Answer
sudo chvt 10
or
CTRL
+
ALT
+
F10
-
Run the sleep command for 30 seconds. Pause them immediately with Ctrl-Z.
Answer
sleep 30s
-
Run another sleep command in the background, this time for 3600 seconds.
Answer
sleep 3600s &
-
List the active jobs in the current session with the jobs command.
Answer
jobs
-
Check the priority and status (running/paused) of programs running in the current session with the appropriate ps command.
Answer
ps a
-
Restore suspended sleep in the background.
Answer
kill -CONT {id_procesu}
-
Check for active jobs with the jobs command until sleep 30 ends.
Answer
jobs
-
End sleep 3600 by bringing it back to the foreground and closing it with Ctrl-C.
Answer
fg {id_from_jobs}
-
-
Run the sleep 1000 sequence in the background; touch sleep_finished. Check if sleep_finished file exists. End the sleep process with the TERM signal. Check again for existence of sleep_finished.
Answer
sleep 1000 & touch ~/sleep_finished ls ~/sleep_finished
CTRL
+
C
ls ~/sleep_finished
it exist
-
Launch an application with a GUI, such as the Mousepad text editor. Check its PID. Send a STOP signal to its process, check if the application responds. Send a CONT signal.
Answer
kill -STOP {process_PID} kill -CONT {process_PID}
-
Create a folder in your home directory called readonly. Remove write access to it. Then execute the command that will try to create the file in it, and in case of failure it will display ERROR message (command echo ERROR).
Answer
mkdir ~/readonly | chmod 444 ~/readonly touch ~/readonly/xd.txt || echo ERROR
-
Go to the /proc directory and read its contents with ls -l /proc.
Answer
cd /proc ls -l /proc
-
Compare the PIDs of the processes pointed to when ps is invoked with the names of the directories in the /proc folder. Then try to go to the directory named corresponding to the PID of the ps process - does the given directory still exist?
Answer
ps
no
-
Go to the subdirectory in /proc named after the PID of the bash process (you can get it by typing ps). Browse its contents and view the contents of the status. Pay attention to the information stored (e.g. Name, State,PID).
Answer
ps cat status
-
Check the information on which processor you are currently working on. To do this, read the content of the cpuinfo file with the cat /proc/cpuinfo command
Answer
cat /proc/cpuinfo
-
Check the RAM usage information. To do this, read the contents of the meminfo file with the command cat /proc/meminfo.
Answer
cat /proc/meminfo
-
Using Nano, increase the size of the bash history stored (HISTSIZE value in the .bashrc file in your home directory)
Answer
nano .bashrc
CTRL
+
W
➤
HISTSIZE
CTRL
+
X
➤
Y
-
Using Vim, edit any text file.
Answer
vim xD.txt
write something
SHIFT
+
;
+
x
+
ENTER
-
Run in a single console, three nano editors in the background, for three different files. Check the background processes in the current terminal with the jobs command. Learn to bring the selected process back to the foreground.
Answer
sudo chmod 777 /etc/nanorc nano /etc/nanorc
unocomment allow nano to suspend
nano x.txt
CTRL
+
Z
nano xD.txt
CTRL
+
Z
nano xDD.txt
CTRL
+
Z
jobs bg 1
Expand!
-
Paginate the /etc/passwd file, assuming the page has 5 lines of text. Hint: check out more
Answer
cat /etc/passwd | more -n 5
-
Create text1 and text2 files, fill them with a few lines of text. Using the cat command, create a text3 file, which will consist of the contents of the text1 and text2 files.
Answer
xD > ~/xD.txt | D > ~/xDD.txt | cat ~/xD.txt ~/xDD.txt > xDDD.txt
-
Display the first 5 lines of all files in your home directory so that their names are not displayed. Hint: remember that you can use patterns with programs that take filenames as arguments.
Answer
cat * */* 2>/dev/null | head -n 5
-
List lines 3, 4, and 5 of the /etc/passwd file
Answer
cat /etc/passwd | sed -n '3,5p'
-
Display lines 7, 6 and 5 counting from the end of the /etc/passwd file (i.e. 7th, 6th, and 5th, respectively)
Answer
cat /etc/passwd | tail -n 7 | head -n 3
-
Display the contents of /etc/passwd on one line
Answer
cat /etc/passwd | tr '\n' ' '
-
Use the tr filter to modify the file by placing each word (separated by a space) on a separate line. Hint: to pass a space character as an argument, you must put it in quotes
Answer
cat < ~/xD.txt | tr ' ' '\n'
-
Count all the files in the /etc directory and its subdirectories
Answer
find /etc -not -type d 2>/dev/null | wc -l
-
Write a command that counts the sum of the first three lines of the /etc/passwd file
Answer
cat /etc/passwd | head -n 3 | wc* -c
-
List the files in the current directory, converting all lowercase letters to uppercase.
Answer
ls | tr a-z A-Z
-
List the access rights of files in the current directory, their size and name
Answer
ls -l | awk '{print $1 " " $5 " " $9}'
-
Display a list of files in the current directory, sorted by file size
Answer
ls -1sp | grep -v / | sort -n -r | grep -oE '[^ ]+$'
-
Display the contents of the /etc/passwd file sorted by UID numbers in order from largest to smallest
Answer
cat /etc/passwd | sort -t ':' -n -k 3 -r
-
Display the contents of the /etc/passwd file sorted first by GID numbers in order from largest to smallest, then UID
Answer
cat /etc/passwd | sort -t ':' -n -k 4 -r -k 3
-
Enter the names of the three smallest files in the directory sorted by name
Answer
ls -1sp | grep -v / | sort -n -r | grep -oE '[^ ]+$' | sort | head -3
-
The /etc/services file stores a list of popular network services, along with port numbers and protocol. List (only) the names of services that use UDP.
Answer
cat /etc/services | grep 'udp' | awk '{print $1}'
-
View how many virtual terminals (dev/tty) numbered 50-69 are in the system.
Answer
ls /dev/tty* -1 | grep '[5-6][0-9]' | ls /dev/tty* -1 | grep '[5-6][0-9]' | wc -l
-
Build a pipeline that displays the cupsd process's PID in the terminal.
Answer
ps -ax | grep -e 'pts' | grep -e 'cupsd' | awk '{print $1}'
Expand!
-
Define the NAME variable and assign your name to it. Display the contents of this variable. Export this variable and check if it is available in the new (child) interpreter.
Answer
NAME=John echo $NAME export $NAME
It is not available in any other interpreter
-
Display the list of exported variables.
Answer
env
-
Change your own prompt by modifying the PS1 variable.
Answer
PS1="xd: "
-
Write a script that for each element (file, folder) in the current directory will display its name along with information whether it is a file or a directory.
Answer
#!/bin/bash for FILE in ~/* do name=$(basename $FILE) if [ -f $FILE ] then echo "$(basename $FILE) -> file" else echo "$(basename $FILE) -> folder" fi done
-
Write a script that for each of the files given as arguments to the call will display the name of the file, and then its contents sorted alphabetically.
Answer
#!/bin/bash for FILE in $* do echo $( cat $FILE | sort ) done
-
Write a script that will copy the file given as the first argument to all directories given as the subsequent arguments of the call.
Answer
#!/bin/bash for ctl in ${@:2} do cp $1 $ctl done
-
Write a script that will back up the files given as arguments to the backup directory and append the current date to their names:
Answer
#!/bin/bash if [ ! -d ~/backup ] then mkdir ~/backup fi for file in $* do cp $file ~/backup/$(basename $file)_$(date '+%Y-%m-%d') done
-
Write a script that will wait for the appearance of the file with the name indicated in the argument. The script should periodically (every 5 seconds) check the existence of the file. If the file exists, the script should display its contents and exit. Run the script and create a monitored file from the second terminal.
Answer
#!/bin/bash while ! [ -f $1 ] do sleep 5 done cat $1
-
Create a script and place in it a function that implements the sum of two arguments (numbers) given to the script.
Answer
#!/bin/bash function sum { echo $(($1+$2)) } sum $1 $2
Expand!
-
Write a script that will find in the directory given as an argument to the script all files with the sh extension, modified not more than 7 days ago and will grant them the right to execute.
Answer
#!/bin/bash find $* -type f -mtime -7 -name "*.sh" -print0 | xargs -0 chmod +x
-
Napisz skrypt który policzy liczbę linii zawierających słowo “color” w pliku ~/.bashrc
Answer
#!/bin/bash cat ~/.bashrc | grep color | wc -l
-
The linux system logs messages in the text file /var/log/kern.log. List the last 3 USB device events from this file.
Answer
#!/bin/bash cat /var/log/kern.log | grep usb | tail -n 3
-
Write a script that will print the number of bytes downloaded by the network interface that has downloaded the most data
Answer
#!/bin/bash ifconfig | grep 'RX packets' | awk '{printf $5 "\n"}' | sort -nr | head -n 1
-
Write a script that lists the MAC addresses of all network interfaces
Answer
#!/bin/bash ifconfig | grep -o ..:..:..:..:..:..
-
Write a script that will create a report.txt file containing the name of the file in each line and its checksum calculated with the md5 algorithm for each *.txt file in the current directory. filename and checksum should be separated by a space. Use md5sum.
Answer
#!/bin/bash find -maxdepth 1 -type f -exec md5sum {} \; | awk '{$2 = substr($2, 3); printf $1 " " $2 "\n"}' > raport.txt
-
Write a program to verify the integrity of files in a directory. For each *.txt file in the current directory, compare its checksum calculated with program/*u md5 with the checksum written in the report.txt file from the previous task, and display a warning message in case of discrepancies.
Answer
#!/bin/bash var=$(find -maxdepth 1 -mindepth 1 -type f -exec md5sum {} \; | awk '{$2 = substr($2, 3); printf $1 " " $2 "\n"}' | grep -v -e raport.txt -e $0) var2=$(cat raport.txt | grep -v -e raport.txt -e $0) if [ "$var" != "$var2" ] then echo "Error" fi
-
Write a script that, based on the input file indicated by the first argument, will display the names of the three planets with the most moons, in alphabetical order.
Answer
#!/bin/bash cat $1 | sort -n -r -k 4 | head -3 | sort
-
The trees.txt file contains information about several trees growing in the garden in the csv format (along with the header in the first line, informing about the content of the file's columns). Write a script that will write to the output.txt file 3 the heights of the two tallest birch trees with the “protected” status. (NOTE error in trees.txt file)
Answer
#!/bin/bash cat trees.txt | tail -n +2 | grep chronione | grep brzoza | awk -F',' '{printf $3 "\n"}' | sort -n -r | head -2 > output.txt
-
Write a script that will concatenate the contents of all files passed as arguments and output to the console
Answer
#!/bin/bash for file in $* do cat $file| awk -v vname=$(basename $file) '{ printf vname ": " $1 "\n" }' done
-
Write a script that will count and print the sum of characters in all files given as call arguments.
Answer
#!/bin/bash cat $* | tr -d '\n' | wc -c
-
Write a script that will create a pictures_backup folder in your home directory and copy all .jpg files in the current directory to it, and then make the new files read-only.
Answer
#!/bin/bash mkdir ~/pictures_backup ls -a | grep "\.jpg"$ | xargs -I{} cp -u {} ~/pictures_backup chmod 444 ~/pictures_backup/*
-
Write a script that assigns the appropriate access rights to files based on the file - access rights argument pairs in numeric notation.
Answer
#!/bin/bash while [ $1 ] do chmod $2 $1 shift shift done
-
Write a script that appends the text specified as the first argument to the end of all files with the extension defined as the second argument and located in the current directory.
Answer
#!/bin/bash for line in *.$2 do echo $1 >> $line0 done
-
Write a script that sums up the size of files in the current directory for each extension given as an argument.
Answer
#!/bin/bash for var in $* do ls -l | grep "."$var | awk '{printf $5 "\n"}' | awk -v text=$var '{s+=$1} END {print text ": " s}' done
-
Write a script that will display the first line from the end of the file given as the first argument, the second line from the end of the file given as the second argument, etc. if the file is too short, display an appropriate message.
Answer
#!/bin/bash cat $1 | tail -1 var=$(wc -l $2 | awk '{printf $1}') if [ $var -gt 1 ] then cat $2 | tail -2 | head -1 else echo "The second file has few lines" fi
-
Write a script that takes two arguments - two filenames, which will compare the contents of these two text files. If the content of both files is the same, the script should print the message: files are the same If the files are different, the script should print which of them has more lines, e.g.: file1.txt has more lines than file2.txt. You can use the diff file1.txt file2.txt command to compare the contents of the files
Answer
#!/bin/bash t1=$(cat $1) t2=$(cat $2) if [ "$t1" == "$t2" ] then echo "identical files" else l1=$(cat $1 | wc -l) l2=$(cat $2 | wc -l) if [ "$l1" -gt "$l2" ] then echo "file $1 has more lines than $2" elif [ "$l1" -lt "$l2" ] then echo "file $2 has more lines than $1" else echo "the files have the same lines but they are different" fi fi
-
Create 3 files in the empty directory: file1.txt, file2.txt and file3.txt. Put a few words in each. Write a script that converts the names of all files in this directory to the number of characters in the file.
Answer
#!/bin/bash for file in $* do count=$(cat $file | wc -w) path=$(dirname $file)"/"$count mv $file $path done
-
Write a script that reads the process number (PID) and signal number from the keyboard in a loop and then sends the indicated signal to a specific process. Entering the word EXIT ends the script.
Answer
#!/bin/bash while true do read -p "Enter PID: " pid if [ "$pid" == "EXIT" ] then break elif [ "$pid" ] then kill -STOP $pid fi done
-
Write a script that recursively renames each directory (except files!) to uppercase.
Answer
#!/bin/bash var1=$(find $1 -type d | sort -n -r) for dir in $var1 do var2=$(basename $dir | tr a-z A-Z) var3=$(dirname $dir) var5=$(echo $var3"/"$var2) mv $dir $var5 done
-
UUID (universally unique identifier) or GUID is a globally unique identifier - the identifier of objects, among others, in Windows or wherever a unique identifier is needed
Answer
#!/bin/bash for i in {1..10} do uuidgen done | sort > id.txt
-
Write a script that asks the user for the number (index) of a word from the Fibonacci sequence and saves this value to a variable. In the script, add a function that uses recursion to calculate the given string term. Display the calculated value in the terminal.
Answer
#!/bin/bash function fib(){ if (( $1 <= 0 )) then echo 1 else echo $(( $(fib $(($1-1)) ) + $(fib $(($1-2)) ) )) fi } read -p "Enter number: " val2 val=$(fib $val2) echo $val
Expand!
-
Write a script that will create a photos folder in the current directory, and then move all files with .jpg and .png extensions in the current directory to it. Set the permissions of the transferred files to read-only. (for your own tests, create a few jpg and png files yourself)
Answer
#!/bin/bash var="photos" mkdir $var ls -a | grep -e "\.jpg"$ -e "\.png"$| xargs -I{} mv -u {} $var chmod 444 $var/*
-
The cars.txt file contains car data in the format year;model;speed. Display car names in one line sorted by speed, separated by commas. Pass the cars.txt file as an argument to the script.
Answer
#!/bin/bash cat $1 | sort -k 3 -r | awk -F ';' 'a++{printf ","}{printf $2} END {print ""}'
-
Write a script that will count files and directories that are in the given directory (script argument), without counting in subdirectories. If the argument is not given, the count is to refer to the current directory.
Answer
#!/bin/bash find $1 -maxdepth 1 -mindepth 1 -type d | wc -l | awk -v text=$1 '{printf "Number of directories in directory " text " = " $1 "\n"}' find $1 -maxdepth 1 -mindepth 1 -not -type d | wc -l | awk -v text=$1 '{printf "Number of files in directory " text " = " $1 "\n"}' find $1 -maxdepth 1 -mindepth 1 | wc -l | awk '{printf "Sum = " $1 "\n"}
Expand!
-
Write a script that will return the number of occurrences of a given word in a given file. Both the word and the file location are passed as arguments to the script. A word can follow or precede any other sequence of characters and can occur more than once on a single line, e.g.
Answer
#!/bin/bash cat $2 | grep -o $1 | wc -w