Linux Command Line: Essential Operations and Scripting
1. Relationships:
- 1: fg% 3-D – Mounted disk
- 2: B – gzip-compressed
- 3: unmount – Nothing
- 4: mke3fs -C – Format
- 5: tar – Nothing
- 6: dd if-E – To back up
- 7: bg% 3 -F – Passing the task
- 8: tar cvf-A – To package
2. Commands:
- A: 2 + 2 =
expr 2 + 2
- B: Change user password:
passwd ana
- C: Shut down computer:
shutdown -h now
- D: Change ownership of ejer60 to user antonio:
chown antonio ejer60
- E: Delete all files:
rm *
- F: Change the file name:
mv script52 ejer52
- G: Copy all files starting with “exercise” to the “Exercise” directory:
cp exercise* Exercise/
- H: Display full path:
pwd
- I: Search for lines containing “/bin/bash” in all files in the current directory:
grep "/bin/bash" *
- J: Look in /etc for files starting with “pro”:
find /etc/ -name pro*
3. Permissions:
After executing ls -l ejer52
, to give read, write, and execute permissions only to the user on this file, use the following command:
chmod 700 ejer52
5. Definitions:
- A: PS1-5 – Defines the prompt
- B: HOME-4 – User’s home directory
- C: Background process-1 – Does not receive any signal from the terminal
- D: MANPATH-3 – Specifies the manual page search path
- E: Foreground process-2 – Receives input from the terminal
6. Terminating a Process:
To terminate a process that is not responding, you can use the following steps:
- Open the console as root or the user who owns the process.
- Run
ps -aux
to list all processes and find the process ID (PID). - Terminate the process using
kill -9 PID
, replacing PID with the actual process ID.
8. Running a Script:
To run a script located in the current directory, you can include the current directory (.
) in the PATH
environment variable.
9. Packaging and Compressing Files:
To package the contents of /home/angeles/exercises
and compress it into /home/angeles/copies/copia.tar.gz
, use the following command:
tar cvf - /home/angeles/exercises | gzip -9c > /home/angeles/copies/copia.tar.gz
10. Command Output:
- A: After executing
ls /win | sort > result
, the file “result” will contain the sorted contents of the /win directory. - B: If the /win directory is empty or does not exist, the command would show nothing, and the “result” file would be empty.
11. chmod Examples:
- A:
chmod a+r
is equivalent tochmod 444
(read permission for everyone) - B:
chmod 666
is equivalent tochmod uog+w
(read and write permission for everyone)
12. Command Classification:
cp
– Yesyast
– Nohalt
– Yescat
– Nouseradd
– Yes
13. Directory Functions:
- A:
/bin
– Stores essential system executable files - B:
/sbin
– Stores executable files used by the administrator - C:
/dev
– Stores device drivers - D:
/etc
– Contains system configuration files
Practical Exercises:
1. Shell Script 1:
#!/bin/bash
option=4
while [ $option -ne 0 ]
do
echo "MENU"
echo "1) Display the contents of a file"
echo "2) View files in a directory"
echo "3) Show Time and Date"
echo "4) Exit"
read option
case $option in
1)
clear
echo "Please fill in the name of the file"
read file
if [ -e "$file" ]; then
if [ ! -f "$file" ]; then
echo "The parameter is not a file"
else
cat "$file"
fi
else
echo "Error: File does not exist"
fi
;;
2)
clear
echo "Enter the directory you want to see"
read directory
if [ -d "$directory" ]; then
if [ ! -d "$directory" ]; then
echo "The parameter is not a directory"
else
ls -l "$directory"
fi
else
echo "Error: Directory does not exist"
fi
;;
3)
clear
date
;;
4)
exit
;;
esac
done
2. Shell Script 2:
#!/bin/bash
echo "Enter directory to check:"
read directory
if [ -d "$directory" ]; then
cont=0
for dir in $(ls "$directory"); do
if [ -d "$directory/$dir" ]; then
cont=$(expr $cont + 1)
fi
done
echo "$directory contains $cont subdirectories"
else
echo "$directory is not a directory"
fi
3. Shell Script 3:
#!/bin/bash
if [ $# -eq 1 ]; then
cd "$1"
for i in *; do
if [ -d "$i" ]; then
echo "$i is a directory"
else
cat *.* >> /home/directory/file.txt
fi
done
else
echo "The program requires that you pass a directory as a parameter"
fi