lab2.docx
Document Details
Uploaded by StylishSpessartine
University of Science and Technology
2024
Tags
Full Transcript
**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (2) Date 24-04-2024** **[Directory and File Commands of Linux operating system]** **Description:** The students take from this exercise how to work with d...
**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (2) Date 24-04-2024** **[Directory and File Commands of Linux operating system]** **Description:** The students take from this exercise how to work with directory and file commands of Linux operating system which includes both graphical and terminal mode. The students get introduced to Linux file system and start working with files and directories commands. Students create directory and files. Modify copy, delete files and delete directory. During this lab session they practice on commands like cat, cp, mv , rm , mkdir ,cd ,cd.. , rmdir with attributes. **Lab Learning Outcomes:** Directory and File Commands of Linux operating system **Lab Instructions:** The lab instructor has to follow the steps below to demonstrate to the students the main file commands. 1. **Working with files:** File is a computer structure to store information in the electronic formats that a computer can use. i.e. in bits which can be either '0' or '1'. - **Rules for naming a file** There are few rules that must be followed when creating a file in Linux operating system. Few are listed below. - No space is allowed in file name. - File name can be the combination of alphabets, numbers and underscore. - First character of the file name should be alphabet. - Special characters such as \$, \*, %, \#, @, \\, /, +, -, =, :, ;, ", ', etc. are not allowed. - Instead of blank spaces you can use underscore ( \_ ). - File name are case sensitive in Linux. "City", "CITY" and "city" are treated as different files. 2. **Create a file:** 1. Select a usefull name for the file 2. Use the command cat to create the file The command is: \$ cat\> file\_name 3. Enter the file contents 4. Use (Ctrl + \^D) to save and exit 5. The system will prompt again. \$ - - To create a file with name "city", you will proceed as![](media/image2.png) \$ cat\> city kh omd bhrri (Ctrl + \^D) This command will create a text file with the text typed. - "cat" means "concatenation". - \^D (Ctrl + D) typed on a new line will signal the end of user input. - You can also give the extension with file name as \$ cat\> city.txt \* Here "city" is the file name and "txt" is the extension. **3- Display the contents of file:** Use the cat command to display the contents of the text file on screen as: \$ cat file\_name \$ cat city The output is kh omd bhrri **cat command Options/Flags:** -------- ------------------------------------------------------ **Meaning** **-b** Number all nonblank output lines, starting with 1. Print **$** at the end of each line. Squeeze down multiple blank lines to one blank line. Number all output lines, starting with 1. Print TAB characters as \^I. -------- ------------------------------------------------------ 4. **add data to the file: The command is:** 1. Use the cat command to add data to an existing file as: 2. **Enter the data** **1** **2** **3** 3. Use (Ctrl + \^D) to save and exit 4. The system will prompt again. 5. List the file content: **\$ cat filename** The output is kh omd bhrri **1** **2** **3** 5. **Display the contents of multiple files** Use the command cat to display the contents of multiple files as: \$ cat file1 file2 6. **creates an empty text file:** use the touch command to creates an empty text file. The syntax is: \$ touch filename Example: \$ touch student.tx 7. **Display the contents of file one screen full at a time** Use the command more to display the contents of the text file one screen at a time as: This command displays the contents of the file on screen and then you can scroll down to view the remaining contents. ---------------- ---------------------------------------- **Meaning** **+***num* Begin displaying at line number *num*. **-** *number* Set screen size to *number* lines. **-u** ---------------- ---------------------------------------- 8. **move and rename files/directories:** the mv command used to both move and rename files/directories. The standard format for mv is: \$ mv \[*options*\] *source destination* To move a file, source and destination must be in different directories. If destination is a directory, then the file's name is not changed **Example1:** move user's file file.txt, which is in the subdirectory temp, to the current working directory without changing the name. \$ mv \~user/temp/file.txt **.** - The mv command can operate on multiple files if you want to move them **Example2:** would move user's file1.txt file2.txt file3.txt from the current working directory to user2 home \$ mv file1.txt file2.txt file3.txt /home/user2 - To rename a file, then source and destination are the old name and the new name. **Example1:** move the file and rename it from file.txt to file2.txt. \$ mv file.txt \~/temp/file2.txt - You are not allowed to rename multiple files in a single mv command If we want to move a file to a location where a file with same name already exists, the -**i** option will prompt the user before overwriting the file (Default). - To force mv to operate without prompting the user using -f option. 9. **Copy files/directories:** the cp command used to copy files/directories. The standard format for cp is: \$ cp *\[options\] source destination* - There are three different combinations of source and destination that can be used: 1-The destination can be another directory; the file is copied with the same name **Example1**: cp file.txt \~/temp/ Outpu: the file file.txt will be copied in \~/temp/ **Example2**: cp \*.txt \~ Outpu: all the files.txt will be copied in the user's home directory. 2-The destination is both a directory and file; the file is copied with a different name **Example1**: cp file.txt \~/temp/file2.txt Outpu: the file file.txt will be copied in \~/temp/ with file2.txt name. 3-The destination is a filename; the file is copied into the current directory with new filename **Example**: cp file.txt file2.txt Outpu: the file file.txt will be copied in the current directory with file2.txt name. **10-File deletion Command:** rm command used to remove or delete a file or a directory. The rm command has the syntax: \$ rm \[*options*\] *file(s)* **Example1:** **Remove the file.txt from the current directory:** \$ rm file1.txt \* The rm command can work on multiple files \$ rm file1.txt file2.txt file3.txt **Example2:** Remove the file file2.txt from \~/tempt \$ rm \~/temp/file2.txt The -i option will prompt the user before delete the file (Default). The user responds with a 'y' or 'n' answer. - **Working with directories:** **Directory:** a user-specified location that allows a user to organize files. **11-Making Directories:** Use mkdir (make directory) command to create directory or subdirectory. The standard format is: \$ mkdir directory-name 1-make a directory named student t. Use the command as: \$ mkdir student \$ ls **Output:** find a directory with the name sudent. 2-Create multiple directories in one command. The required command is : \$ mkdir student teacher administration **Output**: making the above three directories in the current directory. 3-Given the path **/home/user/student/laguag** Suppose you are in **user** directory and you want to make a new subdirectory **Pascal** in the directory **language** in one step (without using **cd** command), use the command as: **\$ mkdir student/language/Pascal** **12- cd directory:** - **change to named directory** **\$ cd directory** **Example:** 1. ahmed\@the-game:\~\> cd ahmed **output**: ahmed\@the-game:\~/ahmed\> 2. ahmed\@the-game:\~\> cd /mnt/collection1/ogg **output** : ahmed\@the-game:/mnt/collection1/ogg\> - **change to parent directory** \$ cd.. **Example:** ahmed\@the-game:/mnt/collection1/ogg\> cd.. **output** : ahmed\@the-game:/mnt/collection1\> - change to home-directory \$ cd \$ cd \~ **Example:** ahmed\@the-game:/mnt/collection1/ogg\> cd ahmed\@the-game:/mnt/collection1/ogg\> cd \~ **output** : ahmed\@the-game:\~\> **13- Output the current working directory:** Use the pwd command to displays the path of the current directory. **Example 1 :** ahmed\@the-game:\~\$ pwd /home/mohd **Example 2 :** ahmed\@the-game:/mnt/collection1/ogg\$ pwd /mnt/collection1/ogg **14- remove directory:** Use the rmdir command to remove directory. The rmdir command has the syntax: \$ rmdir directoryname **Examples:** 1. Remove directory **student**,from the current directory: \$ rmdir student \$ ls **Output**: the directory will be deleted. Few points should be kept in mind when removing the directories. o You can't delete a directory that contains some other directories or files inside. You have to first empty it by removing all the subdirectories and files and then you can delete it. o You can't remove/delete a directory, when you are inside that directory. First come out of it and then delete that directory. 2. delete a directory that has subdirectories and files inside in one step, (*Be careful while applying this command*) \$ rm --r teacher **Output**: will delete the teacher directory and it's contents. 3. You also can specify the path name to delete a directory. Example: given the path /home/user/teacher/courses Suppose you are in **user** directory. You want to delete subdirectory **courses** in one step. Apply the following command \$ rmdir teacher/courses - **Exercises (To be solved in Classroom):** - Use different options of directory and file commands of Linux OS - Using the mentioned commands create the following tree in your home directory: ![](media/image3.png)