Linux Lab
Problem Sheet #1 Linux basics Commands-part A
Objective: To learn basic commands of Linux- file / directory creation, concept of
paths, move, copy, delete and access rights of files/directories.
Refer manual (man command )for more details about the commands covered in this
sheet.
(Be careful – Linux COMMANDS ARE case SENSITIVE)
HINT: Name of the command is written in "( )". Explanation is enclosed in "[ ]".
In some questions Hints are given with the parameters along with commands.
[If more than one file or folder of same name then use the file/folder which is
farthest from current directory ]
[ Create files and directories as per given names and at specified location]
[Do not change present working directory unless it is mentioned in the question]
***** Text File Creation *****
1. Recording the Linux commands and their output in a file (script -a xxxxscript)
[The file name is "xxxxscript" where xxxx is last four digit of your class ID]
2. Create following text file- file1 (cat > file1)
this a text file created using cat.
Display its content using cat
(press Ctrl+D) [This will close the cat utility and returns back to shell]
3. Display the contents of the file1 (cat file1)
4. Create following text file- file2 (cat > file2)
I will learn basic commands of linux
now I shall work in linux
(press Ctrl+D)
5. Display the contents of the file2 (cat file2)
6. Create a single line text file- file3 (echo “First line” > file3)
7. Append a line in the existing file file3 (echo “Second line” >> file3)
8. Display contents of the file file3 (cat file3)
[Link] in a line without newline in exiting text file file3 (echo -n “Third line”
>> file3)[effect of -n: - suppressing the CR-LF when writing current line]
10. Append a line [ last line] in existing file file3 (echo “ in the same line”
>> file3)
11. Display contents of the file file3 (cat file3) [verify the effect of -n of
Q10]
***** Listing Contents of a folder*****
12. Display [list] contents of the current directory (ls)
13. Display all contents of the current directory (ls -a)
14. Display all contents of the current directory in long format (ls -al)[shows
total memory blocks for shown entries and link counts]
15. Display the directory name[not contents] of /home directortory in long format
(ls -dl /home/os23/*)
16. Read the manual of command ls (man ls) [ explore following switches:
-l,-a,-d,-p,-s etc]
17. List all the files having the f as first character in their name [use of
wildcard * for one or more chars ]
18. List all the files having same first four characters in their name but fifth
character is different in these filenames [use of any single wildcard ?. You may
use multiple ? for that many characters, example- f?l??]
19. List all the files having same first(and last) few characters in their name but
one character[only certain char] is different in these filenames [use of character
set wildcard [.…]. You may [a-z][0-9] or [a-d] as per the case ]
20. List all the files having same first(and last) few characters in their name but
NOT having one/more characters[only certain char] in these filenames [use of
character set wildcard [^.…]. You may [^a-z][^0-9] or [^a-d] as per the case ]
21. use echo commands to list file/folder names (echo) with wildcard *
***** Absolute and Relative Path *****
22. Print the path of present[current] working directory (pwd)
23. Move two level up[parent of parent of current] from the current directory,
using relative path (cd)
24. Return back to user's home directory [UHD] using ~ [tilde](cd ~) or space (cd
)
25. Move two level up[parent of parent of current] from the current directory,
using absolute path (cd)
26. Return back to UHD using relative path
27. Display all contents of the root file system in long format [without
changing UHD] using absolute path (ls /)
[Link] all contents of the current directory and the root file system in long
format [without changing UHD] using relative path (ls . ../../.. )
29. Display the directory structure of the parent of the UHD (tree ..)[not
showing hidden files]
***** Subdirectory Creation *****
30. Create four level nested directory structure draa/drab/drac/drad in your UHD.
[Hint-first create(mkdir) draa , move(cd) to draa. verify the location(pwd). Now
create next lower level subdirectory drab, move to drab. so on ... ] [use of
mkdir , cd ,pwd and cd commands. Run these commands-set four times]
31. Return back to UHD (cd)
[After creating all four nested levels as given in Q30, you are in drad folder]
Follow the given instructions(Q32-Q33) WITHOUT CHANGING current working directory [
presently you are in the UHD ]
32. Create four level nested directory structure drba/drbb/drbc/drbd in your UHD
using relative path of each nested depth [run commands four times, WITHOUT CHANGING
UHD. repeating mkdir with different parameters ]
[Use (mkdir drba ) etc]
33. Create four level nested directory structure drca/drcb/drcc/drcd in your
UHD but using single command (mkdir -p ) [write proper parameters with the command]
34. Show the directory structure of your UHD (tree)
35. Show the directory structure of root (tree / ) [use Ctrl+C to stop
scrolling screen]
36. Show the directory structure of drbb using absolute path (tree ) [ give proper
parameter for drbb in tree command]
37. Show the directory structure of drcc using relative path (tree ) [ give
proper parameter for drcc in tree command]
[Link] the contents of the draa using relative path (ls draa )
39. Move to the folder dbb then show the contents of the drcb using relative
path (cd drba/drbb ;ls ../../drca/drcb )
[first change current directory to the drbb then show the contents of the
drcb]
***** Copy , Move and remove(Delete) ;Verify the contents of relevant folder using
ls *****
40. Copy file1 from UHD to drad by same name (cp) using relative path [name of
file at destination is file1]
41. Copy file1 from UHD to drcb by different name[file19] (cp ) [name of file at
destination is file19]
42. Repeat Q41 for drab ,drbc, drcc [destination names file18, file17 and
file16 respectively. Use three cp commands, each for different source/destination
pair ]
43. Copy the directory drcb to drbd (cp -r) [drcb and its contents copied in drbd.
Now two drcb folders in directory structure of the UHD ,at different location]
44. Copy contents of the file18, file17 and file16 to a file file678 in UHD
(cp)[give relative paths for all these files]
45. Copy all files of the drca/drcb to the current directory (cp source-path/*
.)[message appears which validates your action]
46. Remove all contents of the drca/drcb [not to remove drcb itself] (rm -r) [it
results empty drcb]
47. Make copy of file2 as file21 ( cp) [both files are in the current
directory]
48. Rename file21 as file4 (mv) [there will be no file21 in the current directory
after this action but file4]
49. Move all contents of drbd to drca/drcb (mv)[all contents of drbd ,not drbd
itself, is now in the drcb. The drbd is now empty]
50. Move the file16 to UHD by same name
51. Move to drbb directory [NOW drbb becomes current working directory]
52. Copy all contents of drcb to current directory (cp -r) [copy all files and
subdirectories]
53. Copy all contents of drbb/drcb to drad directory (cp)[more than two drcc
folders but at different locations]
54. Move to UHD [NOW UHD becomes current working directory]
55. Remove the drbb/drcb/drcc (rmdir) [IS there ERROR message?]
56. Empty the drbb/drcb/drcc[Remove all contents of drbb/drcb/drcc] (rm -r)
[remove all contents of drcc including drcd. Now drcc is empty]
57. Remove drbb/drcb/drcc (rmdir)
58. Show the directory structure of your UHD (tree)
[Link] files file18, file17 and the folder drad to drbb in single command (cp
-r)[give relative paths for all these files and folders]
***** Hard and Soft Link *****
60. Concatenate files file18, file17 and file3 to drbb/drcb/file19 (cat) [in single
command, use relative paths of all the files]
61. Append file file17 to file4 (cat)
62. Make hard link of file4 by name file4h in drab (ln source link-at-destination)
[use ls -il for UHD and draa/drab to validate inodes of file4 and file4h are same]
63. Make soft link of file4 by name file4s in drbd(ln -s) [type the path of
source seen from destination][use ls -l to see blue marked softlink and leftmost
char l in line showing softlined file entry]
64. Make hard link of file4s by name file4sh in same folder[it is softlink]
65. Append a sentence to the file file4 using echo command (echo)
66. Display contents of file4h and file4s without changing directory (cat)[use
proper path for the files. Verify the contents of file4 are same as in file4h and
file4s]
67. Make hard link of drca/drcb in UHD as drhcb [Does it show ERROR message?]
68. Make soft link of drca/drcb in UHD as drhcb
69. Display directory structure of the drhcb
70. Execute following switches of command ls [use one/more switch at a time ]
ls -A ; ls -i; ls -l ; ls -lt ; ls -m ; ls -p ; ls -R ; ls -r ; ls -s ; ls -S ;ls
-X ; ls -f; ls -F; ls -1;
(ls -lsS etc )
***** access rights *****
[Currently UHD is present working directory]
[use chmod -c for more information of action of chmod. Verify effect of change in
access rights using ls -l, cat, cat>>, cd ,rm etc to read/write or to or delete /
create file/folder]
[ use symbolic characters ONLY to change access rights]
71. Change access mode of drad of owner and others to rw. Verify the access rights
of the drad (chmod -c access-right target) (Can you enter in drad]
72. Change access mode of drbc of owner and others to r. Verify the access rights
of the drbc.([Can you enter/create/delete the directory content of drbc?]
73. Change access mode of drac of owner and others to w. Verify the access
rights of the drac.[Can you enter/read in/of folder of drac?]
74. Change access mode of drcc of owner and others to x. Verify the access rights
of the drcc.[Can you read/create/delete a file folder of drcc?]
75. Change access mode of drca of owner and others to wx. Verify the access
rights of the drca.
76. Change access mode of drab [ALL -nested directories] to rwx of owner and rx of
group and others (chmod -R ..)
77. Change access mode of file1 to w for owner and others. Display the file.
78. Change access mode of file18 to r for owner and others. Concatenate a word
in the file and display it.
79. Change access mode of your UHD to --- for owner [from within UHD, using
relative path], display content of the UHD (chmod u-rwx ../UHD; ls)
[RECOVER access rights of UHD --> go to parent(cd .. ) then recover (chmod
u+rwx UHD)]
80. Change access mode of your UHD to rx for owner [from within]. Verify the
access rights of the directory
81. Change access mode of your UHD to rw for owner [from within]. Verify the
access rights of the directory
82. Change access mode of your UHD to wx for owner [from within]. Verify the
access rights of the directory
83. Change access rights of your UHD to rwx------.[No one can access your UHD]
[Link] a nested folders drda/drdb/drdc drea/dreb in UHD with access right of
child folders is write only(mkdir -p -m a-rx drda/drdb/drdc drea/dreb) [top folder
rights are as per umask- 755 for a folder and 644 for a file ]
86. Create a nested folder drea/dreb/drec with normal access rights as per
umask (mkdir -p drea/dreb/drec)[Does it show ERROR. Why?]
87. Remove the nested empty folder drda/drdb/drdc (rmdir -p drda/drdb/drdc)
[Link] the access right of hardlink file4h so other can not access it for
read/write. Verify it.
89. Change the access right of softlink file4s so other can not access it for
read/write. Verify it.
[In the following questions, you are permitting other users to alter the contents
of your UHD. BE AWARE. If you are not sure PLEASE DO NOT OPEN your UHD for
others]
90. Open your UHD for others by changing access rights for others [ your UHD is
now open to all. BE AWARE]
91. Request your friend to browse your sub directories and files [as per rights set
by you in Q90]
92. Let others to create/delete/link/appen/modify to one of your
directory/file into his/her UHD [BE AWARE. At your own risk]
93. Check the UHD of other users you can enter into [note down access rights of
destination]
94. Change access right of your UHD to rwx------