0% found this document useful (0 votes)
18 views11 pages

Lab Practice for Linux Commands

Uploaded by

arehojayega
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
18 views11 pages

Lab Practice for Linux Commands

Uploaded by

arehojayega
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

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

4. At the command prompt enter the following command to check your


Present Working Directory. The $ is the Command Prompt and you just
need to type the pwd command and hit the Enter key. The present
working directory reported should be the logged in user’s home
directory which will be like /home/student where student is the name of
the logged in user.

$ 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

11. Use the cd command to change directory into the subdirectory


practice1 in your home directory and verify your present working
directory using the pwd command.
$ cd practice1
$ pwd

12. In your present working directory, which should be the practice1


subdirectory in your home directory, create a subdirectory with the
name demodir1 (using relative path name) by running the following
mkdir command.

$ mkdir demodir1

13. In your present working directory, which should be the practice1


subdirectory in your home directory, create a subdirectory with the
name demodir2 (using absolute path name) by running the following
mkdir command.

$ mkdir /home/student/practice1/demodir2

14.In your present working directory, which should be the practice1


subdirectory in your home directory, run the following command. This
command will result in an error message, since there is no subdirectory
dir1 below /home/student/practice1/demodir2 directory. Due to this
intermediate missing directory, an error message will be generated.

$ 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

$ touch file2.txt file3.txt file4.txt .file5.txt file6.txt file7.txt


18. Use the ls command to list files in the present working directory. The ls
command without any options will report only the filenames.
$ ls
19. Use the ls command along with -l option to list the files in the present
working directory. With the -l option the ls command will provide a long
listing which includes the File Attributes information also like File Type
and File Access Permissions in the first field followed by Link Count, User
Owner of the file, Group Owner of the file, Size of the file, Last Modified
Date and Time and the filename in the remaining fields.

$ 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

In the vi editor session to create demofile1.txt, add the following


content in the file demofile1.txt:

Unix Operating System was developed at the AT&T Bell Labs


Multics System was being developed by AT&T Bell Labs, GE and MIT
Solaris is a unix flavour from Oracle
HP-UX is HP’s flavour of Unix
Dennis Ritchie developed the C Programming Language
Linux is a Unix like Operating System
IBM’s flavour of Unix is AIX
There are many flavours of Linux
RedHat Enterprise Linux is from RedHat
Oracle Linux is from Oracle
Linux Mint is a Linux Flavour popular in the Desktop segment
Linux is a Multiuser, Multiprogramming System
27. Run the head command below to report the first ten lines in the file
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

35.Run the wc command below to report the number of characters in the


file demofile1.txt

$ wc -c demofile1.txt

36. Run the cp command below to create demofile2.txt as a copy of


demofile1.txt

$ cp demofile1.txt demofile2.txt

Notice that the above copy worked in a non-interactive manner. It is no


problem now since the target file demofile2.txt was not existing.

37. Run the cp command below to create demofile2.txt as a copy of file1.txt

$ cp file1.txt demofile2.txt

Notice that the above copy worked in a non-interactive manner, the


original contents of demofile2.txt will be lost and demofile2.txt will be
created as a copy of file1.txt. To avoid this, we can use -i option as
mentioned in the next task.

38.Run the cp command with -i option below to create demofile2.txt as a


copy of demofile1.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 file1.txt file2.txt demodir1

40. Run the cp command below to create demodir3 directory as a copy of


demodir1. The recursive option -r is required when copying a directory.

$ cp -r demodir1 demodir3

41. Run the mv command below to rename the file file1.txt to newfile1.txt

$ mv file1.txt newfile1.txt

Notice that the above rename operation worked in a non-interactive


manner. It is no problem now since the target file newfile1.txt was not
existing.

42. Run the mv command below to rename the file newfile1.txt to file2.txt
$ mv newfile1.txt file2.txt

Notice that the above rename operation worked in a non-interactive


manner. The existing file file2.txt will lose its original contents as a
result of this rename operation. To avoid this, use the -i option for
interactive mode.

43. Run the mv command with -i option below to rename file2.txt to


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

45. Run the mv command below to rename the directory demodir1 to


newdemodir1

$ mv demodir1 newdemodir1

46.Run the rm command below to remove the file file6.txt

$ rm file6.txt

Notice that the above rm command works in a non-interactive manner


and removes the file without any confirmation taken from the user.

47.Run the rm command below to remove the file file7.txt in an interactive


manner.

$ rm -i file7.txt

Notice that the above rm command works in an interactive manner and


asks for a confirmation for the removal of the file. If you provide a
response of y the file will be removed. You can respond with a n to
ensure that the file is not removed.

48. Create two files with names file8.txt and file9.txt using the touch
command.

$ touch file8.txt file9.txt


49. Now make these two files file8.txt and file9.txt read only for all
permission categories using the following chmod command. The chmod
command is discussed in a later lesson.

$ chmod 444 file8.txt


$ chmod 444 file9.txt

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.

52. Run the rmdir command to remove the directory demodir3

$ 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

The above rm command with -r (recursive) option will remove the


directory demodir3 and the contents of demodir3.

55.Execute the following command to verify that the demodir3 directory


does not exist anymore. The following command to provide a long
listing for the directory demodir3 will result in an error message
indicating that the “demodir3 directory does not exist” and this helps in
verifying that the directory demodir3 has been successfully removed by
the rm command in the previous step.

$ ls -l demodir3

You might also like