Lab 4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Lab 4: Exploring Linux Files (part I)

In this lab, you will explore file commands and paths. Start your VM, log into your own account
and open a terminal window.

1. A relative path starts at the current directory. To move down, type cd directoryname.
To move up, type cd ... An absolute path starts at the Linux root level, / as in
/home/foxr/myfiles. NOTE: remember to use tab completion when possible!
a. Type cd /. This is the root level. Type cd etc followed by cd sysconfig and
cd networking. Type pwd. Where are you? Type cd /. To return to where you had
just been, combine the previous four cd commands into one by typing cd
/etc/sysconfig/network-scripts (do so). Type cd ../... Where are you?
Type cd ~. Where are you? To reach another use, use ~username. Type cd
~Student. You could also reach Student’s home directory from / using either of
cd /home/Student or cd home/Student (the initial / is not needed in this case
because home is a subdirectory of /).
b. From Student’s home directory, type ls, one item is the subdirectory FILES which has a
subdirectory called DUMMY-DIRECTORY. Enter a command to cd into DUMMY-
DIRECTORY using a relative path. What command did you enter? What directory are
you in according to your prompt?
c. We want to return to the network-scripts subdirectory (see 1a) from DUMMY-
DIRECTORY. Enter a cd command to do so using an absolute path. What command did
you enter? From here, what command would you enter using an absolute path to return
to Student’s DUMMY-DIRECTORY?
d. Your PATH variable was defined in /etc/profile and appended to in /etc/bashrc and in
again in .bashrc_profile. What are the contents of PATH? (hint: recall how to output the
value of an variable from lab 1 step 6). Every Linux command is an executable program.
When you enter a command, if the command is not in the current directory and you did
not specify a path to the command, all directories in PATH are searched one at a time for
the command. To discover where a command is, we can use which and whereis, as in
which wc or whereis wc. which will only search for the executable program in
directories stored in your PATH variable and whereis will search not just for the
executable but the man page and other related files. Where is wc stored? Where is ip
stored?
e. Type which systemd. What response did you get? systemd is one of the few
programs not stored in /usr/bin or /usr/sbin but instead is in /usr/lib/systemd. Type
PATH=$PATH:/usr/lib/systemd. Now retry which systemd. What response
do you get? Explain the assignment statement (PATH=$...).

2. Exploring touch, cp, mv, rm, rmdir. Remember tab completion in steps like 2b, 2c.
a. cd to your home directory. Type touch test1.txt test2.txt test3.txt
test4.txt to create these four files. Type ls –l. The fifth item in the long listing is
the file’s size. What are the sizes for these four files?
b. cd to ~Student. To copy a file, use cp currentfile newfile. Type ls FILES. Let’s copy
sales.txt to your home directory. The command is cp FILES/sales.txt ~ Give it
a try. Did it work? If you did not get an error message, it worked, but you can test it by
typing ls ~ to view the files in your home directory. We can copy multiple files at a
time using wildcards. Type cp FILES/*.txt ~ Make sure this worked as well. One
of Student’s directories is DUMMY-DIRECTORY which has a subdirectory called
directory2 which contains two files. We will copy these to your home directory, but do so
from your home directory. Type cd ~. To indicate that files should be copied here (your
current directory), the destination directory is denoted using . (period). Enter a cp
command to copy the files in Student’s DUMMY-DIRECTORY/directory2 to your
current directory. What command did you come up with?
c. From your home directory, type echo hi >> aa1.abC. This will add the word “hi”
to this empty file. Type ls –l and you will see that aa10.bBC is still empty while
aa1.abC has a size of 3. Repeat the last cp command from step 2b which copied those two
files to this directory but add the option –i (interactive mode). This option forces cp to
pause and ask permission to copy a file if that file already exists in the current directory.
Answer n when asked. Repeat the ls –l command and you will see nothing changed.
Repeat the cp –i command but this to answer y. Repeat ls –l and you will see that
aa1.abC is back to a size of 0. Repeat the cp command without the –i option. What
happens? Explain why and when you should use –i with cp.
d. The option –r performs a recursive copy. Type the command cp –r
~Student/DUMMY-DIRECTORY/directory3 temp. This instruction will
recursively copy everything in directory3, including subdirectories and their contents,
into a new directory called temp, stored in your home directory. You will receive a few
permission denied errors of items that you do not have access to copy, ignore those. Type
ls ~Student/DUMMY-DIRECTORY/directory3 and then ls temp and
compare what you find. In your own words, what does recursive mean?
e. The mv command moves files from one location to another or renames a file. With the
temp directory created from 2d, cd into temp and type mv DUMMY-DIRECTORY
temp2. What does this instruction do? Is temp2 there? What about DUMMY-
DIRECTORY? Type mv temp2 ... Type ls and then cd ~ and ls. What did this
second mv command do? In the first case, we used mv to rename the directory and the
second, we moved the directory to a new location.
f. In one cd command, move to temp2 (up 1 level and then down into temp2). What
command did you issue? Type ls and you should find 4 files ending in .dat (one file,
file3.dat is missing since you did not have permissions to copy it in step 2c). Let’s
assume we want to change these file names back to end in .txt. Enter mv *.dat
*.txt. What happened? Now type mv file1.dat file1.txt. This version
works by renaming one file so why didn’t mv work using the * to rename all 3 files?
Using command line editing, repeat this last mv command 3 times to rename the other 3
files to have .txt extensions.
g. In step 2b, you copied sales.txt to your home directory. Type cd .. followed by rm
sales.txt. Type ls to see if the file was deleted. Move back into temp2 where there
are several .dat files. Enter a command to delete these .dat file using a single rm

Page | 1
instruction and appropriate wildcards (only delete the .dat files). What command did you
enter?
h. As with cp, rm has a –i (interactive) option for safety. While still in temp2, type rm –i
*.txt. Answer n when prompted. How could you ensure that whenever you type rm,
the –i option is used without having to necessarily type rm –i?
i. One subdirectory of DUMMY-DIRECTORY (which is now temp2 in your directory) is
dir5. This is an empty directory. Type rm dir5. What happened? You cannot delete a
directory using rm. Instead, you can delete it using rmdir. Retry the delete command
using rmdir. What command did you use? Did it work? Now try to delete the directory
directory2. What response did you get? rmdir will only delete empty directories. cd into
directory2 and type rm *. This will delete all files. Now cd up 1 level and repeat the
rmdir command. Did it work? Another way to delete the contents of a directory and then
the directory itself is to use rm –r directory. Let’s try this on temp2 itself. cd up 1 level to
~ and type rm –r temp2. Type ls and explain what the rm command did.
j. The opposite of rmdir is mkdir, to create a new directory. This is somewhat like touch, it
creates an initially empty directory. From your home directory, type mkdir new. Type
ls –l. What size is this directory (even though it is empty)? Do its permissions (the
first 10 characters of the line) match the other directories? If not, how does it differ?
k. The files aa10.bBC, aa1.abC, and 3 .txt files were all copied from Student. Use a single
mv command with wildcards to move them all into new. Try your best to write a
command that only lists two items, one combining aa10.bBC and aa1.abC with a
wildcard, and one that references the 3 .txt files). What command did you use?

3. The commands head/tail display the first and last parts of a file, let’s explore them.
a. cd to /etc. Type cat passwd, head passwd and tail passwd. How did the
three commands’ outputs differ?
b. Examine the man pages of head and tail to explore the –c and –n options. Type tail –
c 100 passwd. What does this command do? Now try head –n 5 passwd.
What does this command do?
c. Refer back to both instruction’s man pages and you will see that you can add a +number
to tail and –number to head. Try these out: head –n -10 passwd and tail –c
+100 passwd. Explain what –n -10 and –c +100 do in these cases.
d. What command would list the passwd file starting at line 6?
e. The file command outputs information about files. While you are in /etc, type file a*.
This will output information about all of the entries in /etc that start with the letter ‘a’.
What types of entities do you find? Repeat this step using the option –i. How does the
information about the file type differ from the version without using –i? (examine file’s
man page if necessary)

4. More complex file commands: cmp, diff, join, paste, cut, split.
a. cd to your directory new in your home directory. We will create two files using cat. Type
cat << quit > temp1.txt and enter a list of 5 first names. Repeat this to create
temp2.txt with the same list of names except that temp2.txt should omit the fourth name
entirely and misspell the second name. Type cmp temp1.txt temp2.txt. What is

Page | 2
the output? Repeat using diff. What is diff’s output? Read diff’s man page and then
explain precisely what the output from the diff command means.
b. Now issue two more cat commands similar to those of 4a except that the first list of
names should precede each name with a number from 1-5 as in 1. Frank and for the
second file, do the same but last names, again, numbered 1-5, like 1. Zappa. Now issue a
join command where the command receives both file names, as in join temp3.txt
temp4.txt. What is the result? Explain what the join command does. A related command
is paste; issue the paste command on the same two files. How does the output differ?
c. The cut command allows you to cut and return part of the content of a file. One way to
control cut is to supply the field(s) you want returned. In the case of the two files you
created in 4b, we want to return the names but not the numbers. This would be field 2.
The command would be cut –f 2 filename. However, cut expects fields to be
separated by tabs instead of spaces. We need to modify the command to use a space as a
delimiter instead of a tab, using –d ‘ ’ (1 blank space). This should then return all of
filename without the numbers or space before each name. Try it. The output is sent to the
window. Issue two separate cut commands, one for each of the files from 4b, redirecting
the output to two new files. Now issue the paste command on those two files. Repeat the
same command but using join. How does the output of paste differ from the output of
paste from the end of 4b? How does the output of join on these two files differ from the
join command from 4b when you used the original files?
d. One file you had copied earlier is called addresses.txt which is in this current directory.
Let’s split the file using the split command. The syntax is split –l # file
prefix which splits file into multiple files of # lines naming each file using prefix as
the name of each file but adding to each file a two-letter or two-digit value like aa or 01.
We will split the original file into files of 3 addresses each, using –l 3, and with the
prefix addr. What command did you enter? What new files were created? Repeat your
command but add the option –d after –l 3. How are these new files named? What
does –d do? Delete all of these newly created files, including addresses.txt (you can
delete them all with one rm command using wildcards).

5. Links provide easy access to other files or moved content. The two types of links are hard
links and symbolic links. Read about links in the textbook and view the command ln’s man
page.
a. Type ls –l ~Student/FILES. The second item in the listing is the number of file
records (hard links) pointing at the given file. They should all say 1. From ~Student, enter
the command ln –s FILES/addresses.txt addresses. Type ls –l and
you should see a new entry for addresses. What is this new item? It is not a copy of the
file (we didn’t create this using cp) but instead a symbolic link. Again, type ls –l
~Student/FILES and compare what you see for addresses.txt with what you see for
addresses. Specifically, how do the first and last columns of the long listing of both items
differ? Type cat addresses. Does it display the contents of the file?
b. Now type ln sales.txt sales. This creates a hard link and inserts it in the same
directory. Type ls –l and compare how these two entries differ (if at all).
c. Delete sales.txt and type ls –l. What has changed? Type cat sales. Did it work?
Type su Student to switch to Student, the password is LinuxIsCool!, and cd to

Page | 3
~Students/FILES. Type cp addresses.txt addresses2.txt and then delete
addresses.txt. Type exit to return to your own account. Type ls –l and explain what
has happened to the addresses symbolic link. Delete the symbolic link. How did you do
that? su back to Student, return to the FILES directory and mv addresses2.txt to
addresses.txt to retain a copy.

Close your terminal window and shut down your VM.

Page | 4

You might also like