Lab 4 - More Linux Commands
Lab 4 - More Linux Commands
This Lab will be mostly over the contents of Chapter 4 of our textbook, so please use the
textbook if you do not understand the command.
In this exercise, we will explore the Unix Filesystem. Answer the following questions.
Provide a snippet for each question. A portion of a directory can be snipped, but for larger
directories, not the entire directory.
In this exercise, we will explore the Unix Filesystem. Answer the following questions.
Provide a snippet for each question. A portion of a directory can be snipped, but for larger
directories, not the entire directory.
3. Create the following directories in the hierarchy shown. (Remember Unix is case
sensitive)
a. mkdir unix1
b. cd unix 1
c. mkdir project1 project2 project3 project4 project5
d. mkdir projects
4. Now let’s move the Project Directories into the “projects” directory
a. mv project1 projects
b. Confirm. Do an “ls -al”, did the directory move? snippet.Yes the folder moved
2 of 13
ITSC 1307 – Lab 4
c. Change directory to projects and see if project1 directory has moved under
“projects”.
d. Go back to the previous level (cd ..) and try to move all the other directories
under “projects” at the same time.
e. Snippet
5. So, now that we have renamed directories try a file. Let’s write our first script.
6. First, change directory to the location we want to work in.
a. cd projects/project1 OR cd /home/<youruserid>/unix1/projects/project1
b. Try both forms. One is the absolute path (FULL) path, the other was relative.
7. Use the VI editor to create the following script, name the file “script.sh”.
#!/bin/bash
#
echo "This is a really cool Lab!."
3 of 13
ITSC 1307 – Lab 4
9. Provide a “cat” of the script, run the script, and also provide the output.
Cat
Output
4 of 13
ITSC 1307 – Lab 4
In this exercise, we will explore Finding Files. Answer the following questions. Provide a
snippet for each question. A portion of a directory can be snipped, but for larger directories,
not the entire directory.
1. One command that we could utilize is the “locate” command. However, this has to be
setup correctly.
2. Take a look at the locate command under Section 4-3 “Finding Files”.
a. We are going to do a slightly different locate command. Remember that
“locate” build a database that stores the location of files on your system. It was
designed to quickly locate files by querying the database with the “locate”
command.
3. Try this:
a. locate passwd | grep -v 202 (What is the passwd file?)
The "passwd" file is a system file in Unix-like operating systems that stores essential information
about user accounts. Each line in the "passwd" file represents a user account and contains
various details such as the username, user ID (UID), group ID (GID), user's full name or
description, home directory, and default shell.
b. One more…..
c. locate fstab | grep -v 202 (What is fstab used for?)
5 of 13
ITSC 1307 – Lab 4
The "fstab" file, short for "File System Table", is a configuration file found on Unix-like
operating systems, including Linux. It is located at /etc/fstab. This file is essential for defining
how file systems are to be mounted and managed by the operating system during the boot
process.
4. Now, let’s use a different command, the “find” command. Not database, it just does a
massive search from the place in the file system hierarchy that you start the search, so
it’s slower and we don’t want to search thousands of files to figure out the locating of
the file we are looking for.
a. cd ~
b. find . -ls (This find command says find at my current locating, and do a “ls”)
This will show everything in the directory, and the relative path.
6 of 13
ITSC 1307 – Lab 4
This command searches the /var directory looking for file sizes over 4096K
(kilobytes). Notice the 2>, what is that? Try with and without “2> /dev/null”,
and explain the difference.
The command `find /var -size +4096k 2> /dev/null` searches the "/var" directory
for files larger than 4096 kilobytes. The "2>" redirects any error messages
(stderr) to `/dev/null`, effectively suppressing them. Without "2> /dev/null", error
messages was displayed, cluttering the output.
d. Review section 4-3 in our textbook (CH4), pick a Find command of your own
choosing, and pick a specific file. Also, suppress and permission denied
messages.
find /etc -name "hosts" -exec ls -l {} + 2>/dev/null;
This command searches the "/etc" directory for files named "hosts" and executes the "ls -l" command
on each found file. The 2>/dev/null part redirects any permission denied messages to /dev/null,
suppressing them from the output.
7 of 13
ITSC 1307 – Lab 4
In this exercise, we will explore Linking files by creating a Symbolic Link, and a Hard link.
Answer the following questions. Provide a snippet for each question. A portion of a directory
can be snipped, but for larger directories, not the entire directory.
1. Review Section 4-4, Chapter 4, Linking Files.
2. Let’s create a hard link to our script above. Change to the directory and do an “ls -l”
3. cd ~/unix1/projects/project1
4. ln script2.sh script3.sh
5. Do a listing again, notice that we have created a new file called “script3.sh”, notice
that the file sizes of script2.sh and script3.sh are the same. Also, notice the “2”, next to
the permission, this indicates both files are sharing the same inode.
6. Edit script2.sh and add another line. What happens to script3.sh?
In the previous example, both files share the same inode, meaning they point to the same data on
the disk. Any changes made to one file will be reflected in the other since they are essentially the
same file under different names.
7. In this previous example, both files are sharing the same “inode”,
Each Filesystem in Unix has a superblock that contains general information about the
Filesystem, for example the number of inides and data blocks in kilobytes. An “inode table”
consists of information nodes that describes one file or directory on the filesystem. Each inode
contains unique number for identification and the inode stores metadata about the directory or
file. NOTE: A hard linked file must reside on the same filesystem.
8. Now, let’s take a look at a Symbolic Link
9. In the same previous directory, let’s create a Symbolic Link.
10. ln -s script3.sh script4.sh
8 of 13
ITSC 1307 – Lab 4
11. Once created, do a listing (ls -al), notice that a Symbolic Link is a pointer to another
file or directory. Also, with Symbolic links, we can cross to other filesystems.
12. Create some other Symbolic links and Hard Links on your own
In this exercise, we will explore File and Directory Ownership. Answer the following
questions. Provide a snippet for each question. A portion of a directory can be snipped, but
for larger directories, not the entire directory.
1. Review Section 4-5, Chapter 4, File and Directory Ownership
2. What are your login credentials? “whoami” (username) lishaqzai
9 of 13
ITSC 1307 – Lab 4
10 of 13
ITSC 1307 – Lab 4
6. What are the permissions of the file? Provide detailed information and a snippet. The
permissions of "file1" are read and write for the owner (lishaqzai) and the group
(lishaqzai), with read-only permissions for others.
7. Change the permissions of the file to be executable for owner and group, but no
permissions for “other”, world privs.
8. Since, we are not “root” on the ACC system, these commands can’t be done;
However, answer the following questions for our textbook. You can also try it on
Fedora or CentOS as “root”.
e. Can I combine both the owner and group into a single command that changes
both at the same time? Show an example. chown -R lisha:admins
/var/www/html
In this exercise, we will explore managing file and directory permissions. Answer the
following questions. Provide a snippet for each question. A portion of a directory can be
snipped, but for larger directories, not the entire directory.
11 of 13
ITSC 1307 – Lab 4
3. Change permissions of a file or directory to the following. Utilize both the binary
mode and the plus-minus mode for each:
a. Read Execute for User, Group, and Other
b. Read, Write Execute for User, Read Execute for Group, and Read only for
Other.
c. Read, Write Execute for User, No privs for Group and Other
12 of 13
ITSC 1307 – Lab 4
e. Read, Write for user, Read Only for Group and Other
Which one do you like better? Remember, in Unix there are always more than one way to do
something.
13 of 13