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

Lab1 and 2 diff question

The document provides a series of command-line tasks and scripts related to file management, permission settings, and scripting in a Linux environment. It includes creating directories and files, copying files while preserving permissions, modifying file contents, and writing scripts that interact with user input. Each task is accompanied by explanations of the commands used and their functionalities.

Uploaded by

tranquoctuanduy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab1 and 2 diff question

The document provides a series of command-line tasks and scripts related to file management, permission settings, and scripting in a Linux environment. It includes creating directories and files, copying files while preserving permissions, modifying file contents, and writing scripts that interact with user input. Each task is accompanied by explanations of the commands used and their functionalities.

Uploaded by

tranquoctuanduy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Question 1: Create a directory in your home directory called projects.

In the projects directory, create


nine empty files that are named house1, house2, house3, and so on to house9. Assuming there are lots
of other files in that directory, come up with a single argument to ls that would list just those nine files.

Answer:
$cd home
$sudo mkdir projects
$cd projects
$for i in {1..9}; do
>sudo touch house$i
>done
$ls house[1-9]

Question 4: Recursively copy the /usr/share/doc/initscripts* directory to the $HOME/projects/


directory. Maintain the current date/time stamps and permissions.
Answer:

$sudo cp -ra /usr/share/doc/init* $HOME/projects/

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:

$ls -R $HOME/projects | less

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.

I can scroll through the output using the following keys:

Space: move to the next page.

Enter: scroll one line 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:

$sudo chmod 640 $HOME/projects/house2

Explain:

(chmod: command to change file or directory permissions.

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:

$sudo chmod -R a-w $HOME/projects/

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:

$sudo apt install vim

$cp /etc/services /tmp

$vim /tmp/services

/WorldWideWeb

:%s/WorldWideWeb/World Wide Web/g

: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.

# Note that it is presently the policy of IANA to assign a single well-known

# port number for both TCP and UDP; hence, most entries here have two entries

# even if the protocol doesn't support UDP operations.

# Updated from RFC 1700, "Assigned Numbers" (October 1994). Not all ports

# are included, only the more common ones.

Answer:

$vim /tmp/services

/# Note that it is presently the policy of IANA to assign a single well-known

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

:wq (save all change)

Question 3: Using ex mode, search for every occurrence of the term tcp (case-sensitive) in

your /tmp/services file and change it to WHATEVER.

Answer:

$ vim /tmp/services

:%s /tcp /WHATEVER /g

:wq

Question 4: As a regular user, search the /etc directory for every file named passwd. Redirect

error messages from your search to /dev/null.

Answer:

$find /etc -name passwd 2>/dev/null

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

$touch TEST/one TEST/two TEST/three

$chmod 777 TEST/one TEST/two TEST/three

$find -type f -perm -002

Question 6: Find files under the /usr/share/doc directory that have not been modified in more than 300
days.

Answer:

$find /usr/share/doc -type f -mtime +300

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

$find /usr/share -type f -size +5M -size -10M -exec cp {} /tmp/FILES/ \;

$ls -lh /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:

$find /tmp/FILES -type f -exec cp {} {}.mybackup \;


Question 9: Install the kernel-doc package in Fedora or Red Hat Enterprise Linux. Using grep, search inside
the files contained in the /usr/share/doc/kernel-doc* directory for the term e1000 (case-insensitive) and
list the names of the files that contain that term.

Answer:

$sudo apt update

$sudo apt install linux-doc

$grep -ril “e1000” /usr/share/doc/linux-doc*

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:

$grep -- color=always -rin “e1000” /usr/share/doc/linux-doc*

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:

Today is Sat Dec 10 15:45:04 EST 2016.

You are in /home/joe and your host is abc.example.com.

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

in this step, I write a script content:

#!/bin/bash

current_date=$(date)

current_directory=$(pwd)

hostname=$(hostname)

echo “Today is $current_date.”

echo “You are in $current_directory and your host is $hostname.”


Press Ctrl+O to save script with name: “myownscript” and press Ctrl + X to exit.

$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

in this step, I write a script content:

#!/bin/bash

if [ "$#" -ne 3 ];

then

echo "Hay nhap 3 tham so tu ban phim."

exit 1

fi

ONE="$1"

TWO="$2"

THREE="$3"

X=$#

Y="$*"

echo "There are $X parameters that include $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

$ ~/bin/tuanduyscript trai_tao trai_bo trai_xoai

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

in this step, I write a script content:

#!/bin/bash

read -p "Nhap vao town: " mytown

read -p "Nhap vao street: " mystreet

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

in this step, I write a script content:

#!/bin/bash

read -p "What is your favorite operating system?" OS

if [ "$OS" == "Windows" -o "$OS" == "Mac" ];

then

echo "d2g$#$%^$^%#^"

else if [ "$OS" == "Linux" ];

then

echo "Great choice!"


else echo "Is $OS an operating system?"

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

in this step, I write a script content:

#!/bin/bash

animals=("moose" "cow" "goose" "sow")

for animal in "${animals[@]}"; do

echo "I have a $animal."

done

Press Ctrl+O to save script with name: “animalscript” and press Ctrl + X to exit.

$chmod +x animalscript

$~/bin/animalscript

You might also like