lab4.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 (4) Date 30-04-2024** **[Typical LINUX Directory Structure]** **Description**: The students in this exercise, lists the LINUX file system directories and...
**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (4) Date 30-04-2024** **[Typical LINUX Directory Structure]** **Description**: The students in this exercise, lists the LINUX file system directories and it's typical contents, demonstrate the I-node structure, create Hard and Symbolic Links, searching for Files across the file system, Finding Text in Files, know how to Sorting files. **Lab Learning Outcomes:** The students are able to list the LFS directories, display the files I-node numbers, create Hard and Symbolic Links, Finding Files. Finding Text in Files, practice the facilities used to Sorting files. **Lab Instructions:** The lab instructor has to follow the steps below to demonstrate to the students the following: 1. **LINUX File System (LFS):** The LINUX filesystem is a hierarchical tree structure which is anchored at a special top-level directory known as the root (designated by a slash \'/\'). Because of the tree structure, a directory can have many child directories, but only one parent directory. To specify a location in the directory hierarchy, we must specify a path through the tree. The path to a location can be defined by an absolute path from the root /, or as a relative path from the current working directory. 2. **Listing LFS directories:** The ls command lists the current directory's contents. Typing ls will display for you the visible files and subdirectories in the current directory. \* Visible indicates entries whose names do not start with a period (.) while not visible entries is start with a period (.). \* To view all entries through ls, the option --a (*all*) is used to list the visible and hidden files, also list. (current directory) and.. (parent directory) **Example:** \$ ls -a / \$ ls --a /etc 3. **The file system in reality: I-node** The i-node is a data structure used to store file information, including pointers to its blocks or indirect blocks. For most users and for most common system administration tasks, it is enough to accept that files and directories are ordered in a tree-like structure. The computer, however, doesn\'t understand a thing about trees or tree-structures. In a file system, a file is represented by an *i-node*, a kind of serial number containing information about the actual data that makes up the file: to whom this file belongs, and where is it located on the hard disk. Every partition has its own set of i-nodes; throughout a system with multiple partitions, files with the same i-node number can exist. At the time a new file is created, it gets a free inode. Users can display inode numbers using the -i option to ls. The inodes have their own separate space on the disk. **Syntax**: \$ ls -i Will display the i-node number for the current directory. 4. **Hard and Symbolic Links:** [Link:] a combination of a file's name and the inode number that represents the file. \* The link provides a pointer to the file to [accessing] the [file] from [multiple locations]. \* The [inode number] is then used to index into or reach the inode itself. \* Using the link we can gain [access] to the [file's inode] and from the inode, to the file's blocks. \* Generally any file has a single link. \* You are allowed to [create duplicate links] to be stored in [other directories.] **There are two types of links:** 1. The hard link is a [duplicate of the original link]. 2- The soft (symbolic) link stores a [pointer to the original link]. \$ ln \[-s\] *existing file new file* **Example:** 1. **Create the following link:** \$ ln -s /usr/share/dict/words \~/words **Output**: creates a symbolic link in the home directory called words that links to the dictionary. 2. use ls --l command \$ ls -l The symbolic link is denoted in two ways: First, the [first character] in the list of [permissions] is a 'l' for link. Second, the [name] at the end of the line will have your symbolic link name, an arrow (-\>), [and the file being pointed to]. \* The ls -l command displays in the [second column] an integer [number] (number of hard links pointing at the file). **5- Finding Files- find command:** If you have a rough idea of the directory tree the file might be in you can use find command: \$ find *directory* -name *targetfile* -print find will look for a file called *targetfile* in any part of the directory tree rooted at *directory*. *targetfile* can include wildcard characters. **Examples:** 1- search all user directories for any file ending in \".txt\" and output any matching files (with a full absolute or relative path). \$ find /home -name \"\*.txt\" -print 2\>/dev/null Here the quotes (\") are necessary to avoid filename expansion, while the 2\>/dev/null suppresses error messages such as not being able to read the contents of directories for which the user does not have the right permissions). 2- find can in fact do a lot more than just find files by name. It can find files by type (e.g.-type f for files, -type d for directories), by permissions (e.g. -perm o=r for all files and directories that can be read by others), by size (-size) etc. 3- You can also execute commands on the files you find. For example: \$ find. -name \"\*.txt\" -exec wc -l \'{}\' \';\' counts the number of lines in every text file in and below the current directory. The \'{}\'is replaced by the name of each file found and the \';\' ends the -exec clause. 6. **Finding Text in Files:** The command grep (General Regular Expression Parser) is used to finding text in files. The general syntax is: \$ grep *options pattern files* grep searches the named files (or standard input if no files are named) for lines that match a given pattern. The default behavior of grep is to print out the matching lines. **Examples:** 1. searches all text files in the current directory for lines containing \"hello\". \$ grep hello \*.txt \* Some of more useful options that grep provides are: \ -c : (print a count of the number of lines that match), -i: (ignore case), -v: (print out the lines that don\'t match the pattern) and -n: (printout the line number before printing the matching line). 2. searches all text files in the current directory for lines that do not contain any form of the word hello (e.g. Hello, HELLO, or hELlO). \$ grep -vi hello \*.txt 3. If you want to search all files in an entire directory tree for a particular pattern you can combine grep with find using backward single quotes to pass the output from find into grep. So \$ grep hello \`find. -name \"\*.txt\" -print\` will search all text files in the directory tree rooted at the current directory for lines containing the word \"hello\". 7. **Sorting files:** Two facilities are useful for sorting files in LINUX: \$ sort *filenames* sort sorts lines contained in a group of files alphabetically (or if the -n option is specified) numerically. The sorted output is displayed on the screen, and may be stored in another file by redirecting the output. So \$ sort input1.txt input2.txt \> output.txt Outputs the sorted concatenations of files input1.txt and input2.txt to the file output.txt. \$ uniq *filename* uniq removes duplicate adjacent lines from a file. This facility is most useful when combined with sort: \$ sort input.txt \| uniq\> output.txt