Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

UNIX PROGRAMMING Problem 8 (18 points): Answer the following questions regarding

ID: 3860914 • Letter: U

Question

UNIX PROGRAMMING

Problem 8 (18 points): Answer the following questions regarding managing files and directories:

a) What does the command rm -r /trashbin/ accomplish?

b) What command flag can you use with cp so that it doesn’t accidentally overwrite a file if it already exists (hint: look up the man page for cp)?

c) What command exists to delete a directory if and only if it is already empty?

d) Which command can be used to rename a file or directory?

e) Give the needed commands to accomplish the following scenario (use only 1 command for each one, and assume that each command affects the commands that follow it):

- Change to the directory /usr/share/

- Create two new directories called “oldcode” and “newcode” inside of this directory

- While still inside of /usr/share, create two new files, one called “main.java” inside of the “newcode” directory and another called “calculate.py” inside of the “oldcode” directory (use relative path/file names instead of absolute path/file names)

- While still inside of /usr/share, rename the “calculate.py” file inside of the “oldcode” directory to “calculate.backup” (use an absolute path/file name instead of a relative path/file name)

- Change to the directory “newcode”

- Set the permissions of the “main.java” file (using symbolic notation) so that the group has read and write permissions, but no execute permission

- Make a copy of the “main.java” file to the “oldcode” directory called “main.save” (use relative path/file names)

- Change to the parent directory /usr/share/ using ‘..’ notation

- Delete all of the files inside of the “oldcode” directory, including the “oldcode” directory itself

- Inside of your current directory, remove the empty directory “tempcode” (assume it exists and contains no files)

Explanation / Answer

Ans (a) "rm" command is used to remove files or directories rm rwmove each specified file, by default it does not remove directories

-r option is used with rm for recursive deletion.It removes directories and their contents recursively.

So here rm -r /trashbin/ remove directory trashbin and its content.

Ans (b) "cp" command does not overwrite the existing file content if "-i " option is used with cp,here i stands for interactive copy.Now it ask before overwriting the content of existing file, if you want to copy then allow it otherwise discard it.

Ans (c) "rmdir" command is used to remove a directory which is empty.

Ans (d) mv command is used to rename a file or directory

FOR EXAMPLE:- to rename a file "file.txt" to "temp.txt" we use

mv file.txt temp.txt

Ans (e)

cd /usr/share/

mkdir oldcode newcode

cat /usr/share/oldcode/main.java

cat /usr/share/newcode/calculate.py

cd newcode

chmod g=rw main.java

cp main.java ../oldcode/main.save

cd ..

rm -r oldcode

rmdir tempcode