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

Lab01

Uploaded by

ixvvy1012
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab01

Uploaded by

ixvvy1012
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Qatar University CMPS 101: Intro.

to CS
College of Engineering Lab. Section
Department of Computer Science & Engineering Fall 2024

Linux Lab#01: File Management


Objectives

• Install Linux ubuntu on lab’s computer


• Introducing Linux CLI
• Create, move, rename, and delete files and directories

In Our Computer Lab with Windows 10

To install the Ubuntu on Windows 10, follow the steps listed in the Install_ubuntu.pdf available on the
Blackboard, Course Contents.

Utility Installation

In Linux, the tree command is a utility that is used to display directory and file structures in a tree-like
format. It provides a clear and structured view of the directory hierarchy, making it easier to understand
the organization of files and directories.

To install tree utility, enter the following commands in ubuntu command window

sudo apt-get update


sudo apt-get install tree

Preparation

1. Run the mkdir (make directory) to create a new directory (or folder), name as lab01

mkdir lab01 make directory

2. Run the cd (change directory) command to switch to this new directory

cd lab01 change directory

Now, the prompt should indicate the current working directory (the label just before the $
character)

3. Run the pwd (print working directory) command to check that you are in lab01 directory.

4. Run the tree command. The lab01 directory should be empty. You will get:

0 directories, 0 files

Page 1 of 9
CMPS 101 / Lab Section / Fall 2024 / © Amelle Bedair
Exercise 1

Let’s start with simple commands

1. Run the echo command to print out the text arguments to the display. After the command, you
can put any text. Try this;
echo Hello, World should be in double quotation

2. Run the date command to display the current time and date. The output may look as this

Sat Aug 24 09:53:37 +03 2024


The output displays; the day of the week, day of the month, month, year, current time, and time
zone. By default, the date command is set to the time zone of the operating system.

3. To format the date command’s output, you can use control characters preceded by a + sign.
Format controls begin with the % symbol and are substituted by their current values where the
%Y character is replaced with the current year, %m with month, and %d with the day. Try this:

date +"Year:%Y, Month:%m, Day:%d"


Output:
Year:2024, Month:08, Day:24 date --help

4. Try this reformatting the date output using this command. The %n inserts a new line so the time is
printed on the following line
wrong here should be + then "
date "+DATE: %D%nTIME: %T" %n means new line
Output:
DATE: 08/24/24
TIME: 10:27:01

These are the most common formatting characters for the date command:
• %D – Display date as mm/dd/yy
• %Y – Year (e.g., 2020)
• %m – Month (01-12)
• %B – Long month name (e.g., November)
• %b – Short month name (e.g., Nov)
• %d – Day of month (e.g., 01)
• %j – Day of year (001-366)
• %u – Day of week (1-7)
• %A – Full weekday name (e.g., Friday)
• %a – Short weekday name (e.g., Fri)
• %H – Hour (00-23)
• %I – Hour (01-12)
• %M – Minute (00-59)
• %S – Second (00-60)

Notice the difference between %D (upper case) and %d (lower case)

Page 2 of 9
CMPS 101 / Lab Section / Fall 2024 / © Amelle Bedair
ctrl C to break
^C : to write what i wrote earlier

clear to clean
Your Turn

Find the correct format to display the output similar to the following;

Format Example Linux command


Date: dd/mm/yyyy Date: 26/08/2024 date +"Date:%d/%m/%Y"
Date: dd-mm-yyyy Date: 26-08-2024 date +"Date:%d-%m-%Y"
Date: dd-<full month’s name>-yyyy Date: 24-August-2024 date +"Date:%d-%B-%Y"
Date: <month’s abbr.>, dd, yyyy Date: Aug,24,2024 date +"Date:%d-%b-%Y"

Note: To get the complete list of date formatting, ask for help; date --help

Directory Tree

At the end of Exercise-2, you should have the following file structure where lab01 is our working
directory
home

pwd : means where am i right now


lab01

touch for file


dir1 file1

file2 mkdir dir 1


to check tree
dir2 file3

dir3 file4

dir4 file5
dir5

file6

Based on the given tree structure, answer the following;

1. Inside lab01 directory, how many files and directories are there?
2. Inside lab01 directory, how many directories (folders) are there?
3. Inside dir1 directory, how many directories (folders) are there? white is a file and the blue is
directory
4. What is there inside dir2 directory?
5. What is there inside dir3 directory?
6. What is there inside dir5 directory?

Page 3 of 9
CMPS 101 / Lab Section / Fall 2024 / © Amelle Bedair
General
• Enter the clear command to clear the screen
• Use the arrow-up to display the line you want to repeat.
• To copy a code from Linux window, high-light then hit the Enter (New Line) key
• Most commands have many useful options, to get more information, use the help command.

WARNING!
• Linux command line does not have an undo feature. Perform destructive actions carefully.
• Be cautious when using the remove (rm) command, as it will permanently delete the specified
file, and there is no straightforward way to recover it once deleted.
• Be careful with move (mv) command as you may end up, in some cases, completely
overwriting/deleting your files.

The touch Command

The touch command is used to create, change and modify the timestamps of a file. It does not delete an
existing file. Its primary purpose is to update the timestamps (access and modification times) of a file. If
the file does not exist, touch will create an empty (without any content) file with the specified name.

Exercise 2

Step-1: In lab01 directory create all the directories and files. Enter the tree command to display the
contents of the lab01 directory

Design Steps

1. Enter the tree command to list the contents of the lab01 directory
2. With the touch command, create six empty files, file1 to file6 (all names in lower-case).
Note:- If you do not want to re-type the same command, hit the up arrow key to show the last
command and then modify it.
3. Enter the tree command to list the contents of the lab01 directory
4. With the mkdir (make directory) command, create five directories; dir1 to dir5 (all names in
lower-case).
5. Enter the tree command to list the contents of the lab01 directory
6. Take a snapshot of the screen and paste it in your document file. Use the Snipping Tool which
will be useful to add images in the document.

Page 4 of 9
CMPS 101 / Lab Section / Fall 2024 / © Amelle Bedair
Fig.1: After creating directories and files

Step-2: Move directory dir4 and put it inside dir3 directory

1. With the mv (move) command, move files dir4 into dir3. Use the given syntax;

mv <folder_name> <destination>

2. Enter the tree command to list the contents of the lab01 directory
3. Take a snapshot of the screen and paste it in your document. mv file1 dir1

Fig.2: Directory dir4 is inside directory dir3

Page 5 of 9
CMPS 101 / Lab Section / Fall 2024 / © Amelle Bedair
Step-3: Place the file1, file2, file3 and file4 files as indicated on the chart.

1. With the mv (move) command, move file1 file into dir1 directory. Use the given syntax:
mv <file_name> <destination>
2. With the mv (move) command, move file2 file into dir1 directory.
3. Enter the tree command to list the contents of the lab01 directory.
4. With the mv (move) command, move file3 file into dir2 directory.
5. Enter the tree command to list the contents of the lab01 directory.
6. With the mv (move) command, move file4 file into dir3 directory.
7. Enter the tree command to list the contents of the lab01 directory.

Fig.3: Files file1 and file2 are inside directory dir1

Step-4: Place the contents of dir3 directory.

Read this carefully: since dir4 is inside dir3 directory and the pwd location is lab01, then, linux will
need the exact path from lab01 to dir4

1. With the mv (move) command, move file5 file into dir4 directory. Indicate the path. Use the
given syntax:
mv <file_name> <path/destination>
2. Enter the tree command to list the contents of the lab01 directory.

Fig.4: The complete tree structure of the file system


Page 6 of 9
CMPS 101 / Lab Section / Fall 2024 / © Amelle Bedair
Exercise 4

Rename file1 to file01 while staying in the lab01 working directory,

Do NOT change the working directory. For these activities, it should always be lab01. Use the pwd
command to check.

1. Since the mv (move) command removes the file to put it in its new location, then it can be used to
rename a file. Use the following syntax:
mv <old_file_name> <new_file_name>

2. Try the following command, you will get an error

Fig.5: An error message because file1 is not in the working directory, lab01

3. Why? Because file1 is in dir1 directory and not in the lab01 directory.

4. What is the correct way? Add the complete path.

Fig.6: File file1 is renamed as file01

Page 7 of 9
CMPS 101 / Lab Section / Fall 2024 / © Amelle Bedair
Exercise 5

1. Rename all files using same format; i.e. file5 to file05


2. Repeat for all directories; e.g. rename dir1 as dir01.
3. After each command, enter the tree command to list the contents of the lab01 directory and make
sure of the correct operation.

Fig.7: After renaming all files and directories

Exercise 6

Finally, delete all files and directories you have created today, including lab01.

• Linux rule: You cannot delete a non-empty directory


• To delete a file, enter the rm (remove file) command.
• To delete a directory, enter the rmdir (remove directory) command.
• Do not forget to put the path when deleting a file inside a sub-directory

Design Steps:

1. Use the rmdir (remove directory) command to delete the dir5 directory.

Fig.8: The tree structure after deleting dir05

Page 8 of 9
CMPS 101 / Lab Section / Fall 2024 / © Amelle Bedair
Note:- Don’t use the rm command to delete a directory. Linux will generate an error message

rm: cannot remove 'dir5': Is a directory

2. Use the rm (remove file) command to delete the file01 directory. Don’t forget to add the path.
rm dir01/file01

Fig.9: The tree structure after deleting file01

3. To delete lab01 directory, you will have to move up to the home directory using the cd command
4. After completing all operations, enter the exit command to close the Ubuntu window

Page 9 of 9
CMPS 101 / Lab Section / Fall 2024 / © Amelle Bedair

You might also like