Lab 02 - Linux Filesystem
Objectives
- To understand the different filesystems supported by Linux.
- To learn how to change the command-prompt setting.
- To learn how to change the directory and list the contents of the directories.
- To learn how to create and delete a directory.
- To learn how to set the file and directory permissions.
Software Required
- Ubuntu 12.x Server on the virtual machine.
- Use terminal software, such as PuTTY, to remotely connect to your Linux system.
Procedure
Lab 2.1 - Linux Filesystems
2.1.1 To view available filesystems
Linux support many different filesystems that can be mounted using the mount command. In this session, you use the mount command to determine that filesystems are available in your version of Linux.
- At the terminal window, type man mount and press Enter. Continue pressing spacebar, as necessary, to view the documentation for the -t parameter for the mount command. You can press q to exit the next display mode when you are finished.
- What filesystems can be mounted?
- Next, type mount and press Enter to determine what filesystems are actually mounted. By default, the output will include all file systems including the virtual ones such as cgroup, sysfs, and others. Each line contains information about the device name, the directory to which the device is mounted to, the type of the filesystem, and the mount options in the following form:
device_name on directory type filesystem_type (options)
- What filesystems do you see mounted on your system?
2.1.2 To view the PS1 variable's contents and then to configure the variable
The PS1 variable contains the configuration parameters for how your command-prompt line appears. You view the contains of the PS1 variable and then you configure the PS1 variable.
- Type echo $PS1 and press Enter.
- You see the contents of the PS1 variable as:
- To change your prompt to display the date and time, type PS1='\d \t>' and press Enter. Type the command with no spaces between the characters. What does your prompt look like now?
- To change your prompt to display the current working directory, type PS1='\w>' and press Enter. What does your prompt look like now? (The \w formatting character displays the ~ to represent the user's home directory)
- To change your prompt to display the current working directory, you must use another environment variable, PWD. The PWD variable contains the full pathname of the current working directory. To display the PWD variable in the prompt, type PS1='$PWD>' and press Enter. (Notice that you must place the $ in front of the environment variable name to extract its contents.) What does your prompt look like now?
- If you are using a remote SSH terminal connection, close and open a new terminal session, or log out then log back in and then access the command line, how does your prompt change from what you saw in step 5?
2.1.3 To display your current path
In this session, you use the pwd command to view the working directory.
- Type pwd and press Enter.
- What is your current directory path?
2.1.4 To use the cd command
For this project, you practice more with changing the PS1 variable, and you use the cd command.
- First, change your prompt so that you can view the directory path. At the $ command prompt, type PS1='$PWD>' and press Enter.
- Type cd /var/mail and press Enter. This moves you to the /var/spool/mail subdirectory. What is your prompt now?
- Type cd and press Enter. The change directory command (cd) without arguments returns you to your home directory. What is your prompt now?
- Log out and log back in to reset your prompt.
To navigate directories
Comparing the use of absolute versus relative paths can be handy for understanding how each works. In this session, you use both types of path addressing to navigate through a file system.
- If you are not in your home directory, type cd, and press Enter.
- The parent directory of your home directory is /home. /home is an absolute pathname. Type cd /home and press Enter. The system takes you to the /home directory.
- Type cd plus your username, such as user and press Enter. This step uses the relative path addressing to return to your home directory.
To use dot and dot dot to change your working directory
Navigating a file system using the dot (.) and dot dot (..) option can save you typing time. You will practice using both conventions. Make certain you are logged in to user account and not as root.
- If you are not in your home directory, type cd, and press Enter.
- Type cd . and press Enter. Because the . (dot) references your current directory, the system did not change your working location.
- Type cd .. and press Enter. The system takes you to the parent directory, which is /home.
- Type cd .. and press Enter. The system takes you to the root file system directory (/).
- Type cd and press Enter. The system takes you to your home directory.
2.1.5 To see a list of files and directories in your current working directory
The ls command is one of the most useful commands. You start by using ls to view your working directory. Next, you use ls with an argument to view a file and then a directory. For a more complete listing of information about the contents of a directory, you use the -l option, and finally, you use the -a option to include hidden files in a directory listing.
- If you are not in /home directory, type cd /home, and press Enter.
- Type ls and press Enter. Write down a list of file and directory names you see:
- Change to your home directory by typing cd, and press Enter.
- Type ls .local and press Enter. Write down your observation:
- To see the contents of a directory other than your current working directory, give the directory name as an option to the ls command. For example, to see the contents of the /var directory, type ls /var, and press Enter. Write down what you observe:
To use the ls command with the -l option
- Type ls –l / and press Enter to view the contents of the root file system directory. Write down your observation:
To list hidden files in your home directory
- If you are not in your home directory, type cd, and press Enter.
- Type clear and press Enter to clear the screen.
- Type ls -a after the command prompt, and press Enter. Write down observation:
2.1.6 To work with wildcards
Wildcards are handy to know when you want to find or work on files that have a specific sequence of characters or when you are searching to find a file and are not certain of the correct spelling of that file name. You will use the * and ? wildcard with the ls command.
- To practice using wildcards, you first must care a set of files with similar names. Use the cat command to create files:
- first_name: a file containing your first name.
- last_name: a file containing your last name.
- full_name1.txt: a file containing your full name.
- full_name22.txt: another file containing your full name.
For example, type cat > first_name, press Enter, type your first name, press Enter, and press Ctrl+D.
- type ls *name and press Enter. Write down files you see and explain why:
- Type ls full_name?.txt and press Enter. Write down your observation and explain why:
- Type ls *.txt and press Enter. Write down your observation and explain why:
2.1.7 To create new directories and phone files
Assume that you work for a company that is developing a telephone database and you are creating directories for the Mail and Receiving Departments, which are referenced in the company's budget and accounting systems as departments 4540 and 4550. After you create the directories, you begin creating files of department phone numbers to store in those directories. You use mkdir (make directory) command to create new directories, and then use the cat command to create the phone files. Also, don't delete the files you create because you use them in other projects.
- Type cd and press Enter to make certain you are in your home directory.
- Type mkdir dept_4540 and press Enter to make a new directory called dept_4540.
- Type ls and press Enter. Which directory do you see in the listing?
- Type cd dept_4540 and press Enter to change to the new directory. Now, you can use the cat command to create a file called phones1. The phone file contains fields for area code, phone prefix, phone number, last name, and first name. A colon (:) separates each field.
- Type these commands, pressing Enter at the end of each line:
cat > phones1
626:307:8877:Harrison:Joel
213:380:3644:Mitchell:Barvara
949:432:3996:Olson:Timothy - Press Ctrl+D.
- Type cat phones1 and press Enter to view and verify the contents of the file you created.
- Type cd and press Enter to return to your home directory.
- Type mkdir dept_4550 and press Enter to make a new directory called dept_4550.
- Type ls and press Enter. Which directory do you see in the listing?
- Type cd dept_4550 and press Enter to change to the new directory. Now you can use the cat command to create the file phones2, which contains the same fields as the phones1 file.
- Type these commands, pressing Enter at the end of each line:
cat > phones2
219:432:4591:Moore:Sarah
219:432:4522:Polk:John
219:432:4501:Robinson:Lisa - Press Ctrl+D.
- Type cat phones2 and press Enter to view and verify the contents of phones2 file.
- Type clear and press Enter to clear the screen for the next project.
To copy the phones1 file into a new directory, corp_db
- Type cd and press Enter to return to your home directory.
- Type mkdir corp_db and press Enter to make a new directory.
- Type cd corp_db and press Enter to change to the new directory.
- To copy the phones1 file from the dept_4540 directory to the current directory, type cp ~/dept_4540/phones1 . and press Enter.
- To copy the phones2 file from the dept_4550 directory to the current directory, type cp ~/dept_4550/phones2 . and press Enter.
- Type ls and press Enter. Write down your observation:
To concatenate the phones1 and phones2 files into one file
- Type cat phones1 phones2 > corp_phones and press Enter to add the contents of the two phone files to one new file called corp_phones.
- Type clear and press Enter to clear the screen.
- Type more corp_phones and press Enter to view the new file's contents. Write down your observation:
2.1.8 To change file and directory permissions
Assume you want all users to have access to the corp_phones file. You first grant access to your home directory. Next, you allow access to the corp_db directory, and then set the permission for everyone to read the corp_phones file. You use the chmod command with the x argument to grant access to directories.
- Make certain that you are in your home directory (type cd and press Enter).
- Type chmod go+x ~ and press Enter to allow access to your home directory.
This command means that "make your home directory (~) accessible (+x) to the group (g) and others (o)". - Type chmod ugo+x ~/corp_db and press Enter to allow access to the corp_db directory.
This command means "make the corp_db directory accessible (+x) for the owner (u), group (g), and others (o)." - Type chomd o+w ~/corp_db/* and press Enter to set permissions so that others can write to the files in the corp_db directory (owner and group already have to write permission by default).
This command means "make all files in the corp_db directory so that others (o) can write (+w) to them." - Type ls –l ~/corp_db to check the permissions you have set. Record your observation:
Report
Submit your report in PDF format before the due.