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

Solved Questions of Unix Ch2 and Ch3

The document provides examples of Linux commands for creating directory structures, manipulating files and directories, setting permissions, and viewing file contents. Some key examples include: 1. Commands to create the directory structure shown in Figure 1 under the root directory. 2. Commands using ls, cat, rm, cp, and other commands to list, view, delete, copy files matching certain patterns based on filename length, contents, location. 3. Chmod commands using octal and symbolic modes to set permissions for users, groups, and others on files and directories. 4. Head and tail commands to view specific lines from the beginning or end of files. 5. Wc commands to count lines,

Uploaded by

Jawad Aadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Solved Questions of Unix Ch2 and Ch3

The document provides examples of Linux commands for creating directory structures, manipulating files and directories, setting permissions, and viewing file contents. Some key examples include: 1. Commands to create the directory structure shown in Figure 1 under the root directory. 2. Commands using ls, cat, rm, cp, and other commands to list, view, delete, copy files matching certain patterns based on filename length, contents, location. 3. Chmod commands using octal and symbolic modes to set permissions for users, groups, and others on files and directories. 4. Head and tail commands to view specific lines from the beginning or end of files. 5. Wc commands to count lines,

Uploaded by

Jawad Aadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Q1. Write the commands for creating the directory structure of (Figure 1) inside root directory.

Figure 1

$cd /

$mkdir College $cd College $mkdir Students Resources $touch Resources/Labs.txt


$mkdir Resources/Classroorms

$cd Students $mkdir Engineering IT Business $touch ./Business/E-Commerce.doc

Specifying multiple filenames

a. Write a command to display all files under DATA directory, whose name contains three
character long.
$ls DATA/???
b. Write a command to display the data from all the files under directory Test, whose
name is five character long with ali in middle.
$cat Test/?ali?
c. Write a command to remove all files in current directory, whose name is starting with
de.
$rm de*
d. Write a command to copy all files, directory, subdirectories from /Test into /Data,
whose name is ending with deo.
$cp -R /Test/*deo /Data
e. Write a command to display all the files of /Demo directory, whose name is ending
with a character in between a to m.
$ls /Demo/*[a-m]
f. Write a command to display directory DATA contents recursively, whose name is
beginning with a letter from 'm' to 'k' and ends in a letter from 'c' to 'l'.
$ls DATA/[m-k]*[c-l]

g. Write a command to delete all the files in the parent of parent directory, whose name is 6
character long, first character in between h to m and ending with a number 6 or 8.
$rm ../../[h-m]????[6,8]
h. Write a command to display the data from all the files, whose name is beginning in between a
to c and ending with a number 6 or 9.
$cat [a-c]*[6,9]

i. Write a command to remove all non-empty directories in interactive mode, whose


name is four character’s long with 'el' in the middle.
$rm -ri ?el?
j. Write a command to display your current location in the file system.
$pwd
k. Write a command to display all the files of /Demo directory, whose name is ending with a
character in between a to m.
$ls /Demo/*[a-m]
l. Write a command to delete all the files in the parent directory, whose name contain 5
characters long, first character in between a to d and ending with 4 to 8.
$rm ../[a-d]???[4-8]
m. Write a command to copy all the files interactively from Exam directory to TEST
directory, whose name is ending in .doc.
$cp -i Exam/*.doc TEST
n. Write a command to remove all files under /demo directory, whose name contains 1
character long but ending in .ppt.
$rm /demo/?.ppt
o. Write a command to list all the files, whose name is beginning with a or h.
$ls [a,h]*
p. Write a command list all the files. Whose name is beginning with a to z in any case
(upper case/lowercase).
$ls [a-zA-Z]*
q. Write a command to list all the files ending in .c in any directories beginning with dir.
$ls dir*/*.c
r. Write a command to list all the files ending in .c in any subdirectory.
$ls */*.c

More examples of Chapter 2


Q 1. Write commands of following directory structure (Figure1) under root directory.

a. Write a command to copy recursively all files, directory, subdirectories under theory
directory with new name "newtheory" to unix.
$cp -R /Student/theory /Student/theory/unix/newtheory
b. Write a command to list all files, including hidden files under unix directory.

$ls -a /Student/theory/unix

c. Write a command to create a directory linux under theory directory.


$mkdir /Student/theory/linux
d. Write a command to create a file lab2.doc under Student directory and write the
data “Hello how are you”.
$cat > /Student/lab2.doc
Hello how are you
Ctrl+d

e. Write a command to display the data from dsp.doc page by page with scroll the data
by line by line.
$less /Student/theory/dsp.doc
Q2. a. Write a command to create a symbolic link EEE of the directory ENG.

$ln -s ENG EEE

b. Write a command to delete all the files in the parent directory, whose name contain 5
characters long, first character in between a to d and ending with 4 to 8.

$rm ../[a-d]???[4-8]

c. Write a command to display the data from all the files, whose name is beginning with “a” or
“c”.

$cat [a,c]*

d. Write a command to remove all empty directories, whose name is ending with exam.

$rmdir *exam

e. Write a command to create a directory test inside /home directory.

$mkdir /home/test
Q 3. Write commands of following directory structure.

a. Write a command to copy all files, directory, subdirectories including D11


directory interactively to D13. (use relative path only)
$cp -R ../D11 ../D13
b. Write a command to change your current directory to D14. (use relative path
only)
$cd ../D14
c. Write a command to move a file F2 interactively to F7. (use only relative path)
$mv -i ../D11/F2 ../D14/F7

CHAPTER – 3 Examples
Chmod Command Examples
1. Write a command to change the permission of a file named is
private.txt.
- User/owner can read and write the file only. (using octal mode)
$chmod 600 private.txt

2. Write a command to set the permission of file.txt to “read and write


by everyone.”(using octal mode)
$chmod 666 file.txt

3. Write a command to set the permission for all the files that is ending
in .txt
• user can read and write, group can execute only, other can
read. (Using octal mode)
$chmod 614 *.txt
4. Write a command to modify the permission for all the files, directory,
subdirectories under demo directory. (Using octal mode)
- User can read and execute it.
- Group and others can read and write it.
$chmod -R 566 demo
5. Write a command to add read and write permission for all the users
for file, directory, and subdirectory inside directory civil. (symbolic
mode)
$chmod -R a+rw civil
6. Assume you have a file named hello, do the following based on
commands as shown below.
• Add the read and write permission for the others.
• Remove executes permission from member of your group.
• Set(assign) all three permissions for the owner.
$chmod u=rwx, g-x, o+rw hello
7. Write a command to set(assign) the permission to a file named is
ali.txt.(symbolic mode)
- User can read and write the file
- Group can execute the file.
- Others can read and execute the file
$chmod u=rw,g=x,o=rx ali.txt $chmod 615 ali.txt
8. Let’s say you are the owner of a file named myfile.
a. For user can add read, write permission,
b. members of your group assign the read and execute
permission,
c. for others remove the execute permission. (symbolic mode)
$chmod u+rw,g=rx,o-x myfile

Head and Tail Command Examples

Q1. Write a command to display the first 3 line of a file named is ali.

$head -3 ali

Q2. Write a command to display the last 4 line of a file named is ali.

$tail -4 ali

Q3. Write a command to display the 3rd and 4th line from the beginning of file ali.

$head -4 ali | tail -2

Q4. Write a command to display the 5th and 6th line from the end of file ali.

$tail -6 ali | head -2

Examples of wc command
Q10. Write a command print the number of lines in a file named is demo.
$wc -l demo
Q11. Write a command print the number of words in a file named is demo.
$wc -w demo
Q12. Write a command print the number of bytes in a file named is demo.
$wc -c demo
Q13. Write a command print the number of characters in a file named is
demo.
$wc -m demo
Q14. Write a command print the number of characters & lines in a file
named is demo.
$wc -ml demo

You might also like