Lab1 and 2 diff question
Lab1 and 2 diff question
Answer:
$cd home
$sudo mkdir projects
$cd projects
$for i in {1..9}; do
>sudo touch house$i
>done
$ls house[1-9]
Question 5: Recursively list the contents of the $HOME/projects/ directory. Pipe the output to the less
command so you can page through the output.
Answer:
Explain:
(-R : Recursive
“|” (Pipe): Passes the output of the ls -R $HOME/projects command as input to another command.
less: a command-line pager utility that allows you to view output one screen at a time.
q: quit less )
Question 6: Remove the files house6, house7, and house8 without being prompted.
Answer:
$sudo rm -f house[6-8]
Explain:
rm: remove
-f: stands for "force". This option forces the deletion without prompting for confirmation, even if the files
are write-protected.
Question 9: Change the permissions on the $HOME/projects/house2 file so it can be read and written by
the user who owns the file, only read by the group, and have no permission for others.
Answer:
Explain:
640: write = 2, read = 4, execute = 1, so for user its 6 (4+2= 6) for read and write, 4 for group to read and
0 for others)
Question 10: Recursively change permissions of the $HOME/projects/ directory so nobody has write
permission to any files or directory beneath that point in the file system.
Answer:
Explain:
(chmod: command to change file or directory permissions.
-R: This option applies changes recursively to all files and subdirectories.
a-w: a: applies to all (user, group, others), -w: removes write permission.)
Chapter 5:
Question 1: Copy the /etc/services file to the /tmp directory. Open the /tmp/services file in vim, and
search for the term WorldWideWeb. Change that to read World Wide Web.
Answer:
$vim /tmp/services
/WorldWideWeb
:wq
Question 2: Find the following paragraph in your /tmp/services file (if it is not there, choose a different
paragraph) , and move it to the end of that file.
# port number for both TCP and UDP; hence, most entries here have two entries
# Updated from RFC 1700, "Assigned Numbers" (October 1994). Not all ports
Answer:
$vim /tmp/services
press Shift+v to select the paragraph => press d to cut the paragraph
press G to move to the end of the file => press p to paste the paragraph
Question 3: Using ex mode, search for every occurrence of the term tcp (case-sensitive) in
Answer:
$ vim /tmp/services
:wq
Question 4: As a regular user, search the /etc directory for every file named passwd. Redirect
Answer:
find: Đây là lệnh dùng để tìm kiếm file hoặc thư mục trong hệ thống.
/etc: Đây là đường dẫn thư mục mà bạn muốn tìm kiếm. Ở đây, bạn tìm trong thư mục /etc.
-name passwd: Tùy chọn -name dùng để chỉ định tên file mà bạn muốn tìm. Ở đây, lệnh sẽ tìm các file có
tên là passwd.
2>/dev/null:
2>: Phần này chuyển hướng (redirect) các thông báo lỗi (error messages). Trong shell, số 2 đại diện
cho stderr (luồng lỗi chuẩn).
/dev/null: Đây là một thiết bị đặc biệt trong Linux, được gọi là "bit bucket". Bất cứ thứ gì được
chuyển hướng tới /dev/null sẽ bị "xóa" hoặc bỏ qua.
Question 5: Create a directory in your home directory called TEST. Create files in that directory named one,
two, and three that have full read/write/execute permissions on for everyone (user, group, and other).
Construct a find command to find those files and any other files that have write permission open to
“others” from your home directory and below.
Answer:
$mkdir TEST
Question 6: Find files under the /usr/share/doc directory that have not been modified in more than 300
days.
Answer:
Question 7: Create a /tmp/FILES directory. Find all files under the /usr/share directory that are more than
5MB and less than 10MB and copy them to the /tmp/FILES directory.
Answer:
$mkdir -p /tmp/FILES
Question 8: Find every file in the /tmp/FILES directory, and make a backup copy of each file in the same
directory. Use each file's existing name, and just append .mybackup to create each backup file.
Since there are no files with sizes between 5MB and 10MB, the /tmp/FILES directory does not contain
any files copied from /usr/share. Therefore, in this question, I will create 2 dummy files (file1.txt and
file2.txt) to replace them and continue with the task.
Answer:
Answer:
Question 10: Search for the e1000 term again in the same location, but this time list every line that
contains the term and highlight the term in color.
Answer:
Chapter 7:
Question 1: Create a script in your $HOME/bin directory called myownscript. When the script runs, it
should output information that looks as follows:
Of course, you need to read in your current date/time, current working directory, and hostname. Also,
include comments about what the script does and indicate that the script should run with the /bin/bash
shell.
Answer:
$mkdir -p $HOME/bin
$cd $HOME/bin
$nano myownscript
#!/bin/bash
current_date=$(date)
current_directory=$(pwd)
hostname=$(hostname)
$chmod +x myownscript
$myownscript
Question 2: Create a script that reads in three positional parameters from the command line, assigns those
parameters to variables named ONE, TWO, and THREE, respectively, and outputs that information in the
following format: There are X parameters that include Y. The first is A, the second is B, the third is C.
Replace X with the number of parameters and Y with all parameters entered. Then replace A with the
contents of variable ONE, B with variable TWO, and C with variable THREE.
Answer:
$cd ~/bin
$nano tuanduyscript
#!/bin/bash
if [ "$#" -ne 3 ];
then
exit 1
fi
ONE="$1"
TWO="$2"
THREE="$3"
X=$#
Y="$*"
echo "The first is $ONE, the second is $TWO, the third is $THREE."
Press Ctrl+O to save script with name: “tuanduyscript” and press Ctrl + X to exit.
$chmod +x tuanduyscript
Question 3: Create a script that prompts users for the name of the street and town where they grew up.
Assign town and street to variables called mytown and mystreet, and output them with a sentence that
reads as shown in the following code (of course, $mystreet and $mytown will appear with the actual town
and street the user enters):
The street I grew up on was $mystreet and the town was $mytown.
Answer:
$cd ~/bin
$ nano townscript
#!/bin/bash
echo "The street I grew up on was $mystreet and the town was $mytown."
Press Ctrl+O to save script with name: “townscript” and press Ctrl + X to exit.
$ chmod +x townscript
$ ~/bin/townscript
Question 4: Create a script called myos that asks the user, “What is your favorite operating system?”
Output an insulting sentence if the user types Windows or Mac. Respond “Great choice!” if the user types
Linux. For anything else, say “Is <what is typed in> an operating system?”
Answer:
$cd ~/bin
$nano myos
#!/bin/bash
then
echo "d2g$#$%^$^%#^"
then
fi
fi
Press Ctrl+O to save script with name: “myos” and press Ctrl + X to exit.
$chmod +x myos
$~/bin/myos
Question 5: Create a script that runs the words moose, cow, goose, and sow through a for loop.
Have each of those words appended to the end of the line “I have a....”
Answer:
$cd ~/bin
$nano animalscript
#!/bin/bash
done
Press Ctrl+O to save script with name: “animalscript” and press Ctrl + X to exit.
$chmod +x animalscript
$~/bin/animalscript