Lab Practice for Linux Commands
Lab Practice for Linux Commands
In this Lab Practice you will work with the different Linux Commands and
become familiar with those commands and their usage.
1. Login to your Lab System with the regular user account of student.
2. Open a Terminal Screen and use this Terminal Screen to execute the
following commands.
3. Change directory into the home directory of the logged in user using the
cd command.
$ cd
$ pwd
5. Run the id command to report the details about your user account.
$ id
6. Use the date command to report the date and time in the System
according to the Operating System Settings.
$ date
7. Run the cal command to report the calendar for the month and year
reported by the date command.
$ cal
8. Run the following cal command to report the calendar for the year 2022.
$ cal 2022
9. Run the following cal command to report the calendar for the month of
July in Year 2015.
$ cal 7 2015
10.In your present working directory, which should be your home directory,
create a subdirectory with the name practice1 (using relative path name)
by running the following mkdir command.
$ mkdir practice1
$ mkdir demodir1
$ mkdir /home/student/practice1/demodir2
$ mkdir /home/student/practice1/demodir2/dir1/dir2
15. You can take care of the above error by using the -p option of the mkdir
command. In your present working directory, which should be the
practice1 subdirectory in your home directory, run the following
command. This command will run successfully and create the
subdirectory dir1 below /home/student/practice1/demodir2 and then
creates dir2 as a subdirectory of this dir1 directory.
$ mkdir -p /home/student/practice1/demodir2/dir1/dir2
16. Run the following touch command to create a file with the name
file1.txt in the present working directory. This command will create the
file file1.txt as an empty or zero byte file.
$ touch file1.txt
17. Run the following touch command to create multiple files file2.txt,
file3.txt, file4.txt, .file5.txt (hidden file), file6.txt and file7.txt
$ ls -l
20. Use the ls -al command to include hidden files (files where the name
starts with .) in the output.
$ ls -al
21. Run the following ls -al command to list files (including hidden files) in
demodir2 subdirectory.
$ ls -al demodir2
22. Run the following ls -alR command to get a recursive listing for
demodir2 subdirectory and its contents including the hidden files.
$ ls -alR demodir2
23. Run the following ls -ld command to report the file attributes for the
demodir2 itself and not for its contents.
$ ls -ld demodir2
24. Run the following ls command for reporting the files in /var directory in
a long listing manner. With the -t option, the list will be presented in the
reverse chronological order of the time when the file was created,
meaning that the latest file will appear on the first line of output and the
oldest file will appear on the last line of output. With the -h option, the
file size will be reported in Human Readable format like Kilo Bytes, Mega
Bytes, Giga Bytes.
$ ls -lth /var
25. By including the -r option with the above command, you can get the
files in /var directory reported in chronological order meaning that the
oldest file will appear on the first line of output and the latest file will
appear in the last line of output.
$ ls -lrth /var
26. Run the following command to create a text file with the name
demofile1.txt using vi editor and add the content that is mentioned after
this command. Please refer to the demovi.txt file for the different
operations that you can perform in vi.
$ vi demofile1.txt
$ head demofile1.txt
28. Run the head command below to report the first four lines in the file
demofile1.txt
$ head -4 demofile1.txt
29. Run the tail command below to report the last ten lines in the file
demofile1.txt
$ tail demofile1.txt
30. Run the tail command below to report the last 2 lines in the file
demofile1.txt
$ tail -2 demofile1.txt
31. Run the tail command below to report from the 3rd line onwards up to
the last line in the file demofile1.txt
$ tail -n +3 demofile1.txt
32. Run the wc command below to report the number of lines, the number
of words and the number of characters in the file demofile1.txt
$ wc demofile1.txt
33. Run the wc command below to report the number of lines in the file
demofile1.txt
$ wc -l demofile1.txt
34.Run the wc command below to report the number of words in the file
demofile1.txt
$ wc -w demofile1.txt
$ wc -c demofile1.txt
$ cp demofile1.txt demofile2.txt
$ cp file1.txt demofile2.txt
$ cp -i demofile1.txt demofile2.txt
Since the target file demofile2.txt is already existing, the above copy will
work in an interactive manner. A Confirmation Question will appear to
check whether you want to overwrite the target file demofile2.txt. You
can respond with a y for proceeding with overwrite or n for not
proceeding with the overwrite.
39. Run the cp command below to copy the files file1.txt and file2.txt into
the subdirectory demodir1
$ cp -r demodir1 demodir3
41. Run the mv command below to rename the file file1.txt to newfile1.txt
$ mv file1.txt newfile1.txt
42. Run the mv command below to rename the file newfile1.txt to file2.txt
$ mv newfile1.txt file2.txt
$ mv -i file2.txt file3.txt
Since the target file file3.txt is already existing, the above rename will
work in an interactive manner. A Confirmation Question will appear to
check whether you want to overwrite the target file file3.txt. You can
respond with a y for proceeding with overwrite or n for not proceeding
with the overwrite.
44.Run the mv command with -i option below to move file4.txt to demodir1
directory. In case a confirmation message appears, provide y as the
response.
$ mv -i file4.txt demodir1
$ mv demodir1 newdemodir1
$ rm file6.txt
$ rm -i file7.txt
48. Create two files with names file8.txt and file9.txt using the touch
command.
50. Execute the following rm command to remove the read only file
file8.txt
$ rm file8.txt
Since the file file8.txt is a read only file, a Confirmation question will
now appear to the effect whether you want to remove this read only file
file8.txt. You can respond with y to proceed and remove this file
file8.txt. In case you want to avoid this kind of confirmation question
while removing a read only file, you can use the -f option of the rm
command which is shown in the next Lab Practice task.
51. Execute the following rm command with -f option to remove the read
only file file9.txt
$ rm -f file9.txt
Since the -f option has been used with the rm command, the read only
file file9.txt will be removed without asking for a confirmation.
$ rmdir demodir3
The above rmdir command will result in an error message since the
demodir3 directory is not empty. The rmdir command will only remove
a directory if the directory is empty.
53. Execute the following command to verify that the demodir3 directory
still exists. The following command will provide a long listing for the
contents of the directory demodir3.
$ ls -l demodir3
54. Run the rm command with -r (recursive) option to remove the directory
demodir3
$ rm -r demodir3
$ ls -l demodir3