Directory and File Commands 6
Objectives
Upon completion of this module, you should be able to
• Control screen output using control characters
• Determine a file’s type with the file command
• Display the contents of text files using the cat, more, head, and tail commands
• Determine word, line, and character count using the wc command
• Compare the contents of text files using diff and cmp
• Create empty files or update access time of existing files using the touch command
• Create and remove directories using mkdir and rmdir
• Manage files and directories using the mv, cp, and rm commands
• Save the output from a command into a file
• Pass output from one command to another using a pipe
• Use the tee command within a pipeline to create text within a file
Discussion – What tasks do you need to complete to manage your files and directories?
- Create , edit, delete, copy, ..a file
- Identity size of file.
- View content of a file.
- Compare content of two files
Control Characters
Control characters are used to perform specific tasks such as stopping and starting screen output.
When displayed on the screen, the Control key is represented by the caret symbol (^). To enter a
sequence of control characters, hold down the Control key and press the appropriate character on
the keyboard.
The Control-s and Control-q characters were originally needed by teletype
operators and are rarely used today.
The following control characters can be used:
Table 6-1 Control Characters
Control Characters Purpose
Control-s Stops screen output
Control-q Resumes screen output
Control-c Interrupts current activity
Control-d Indicates end-of-file or exit
Control-u Erases the command line
Control-w Erases the last word on the line
Control-l Clear screen
Shift-Page Up Scroll up one page
Shift-Page Down Scroll down one page
The actual character in the shell appears as ^C , even though you press the
Control key and the c key at the same time.
Determining File Type
There are many types of files found on a Linux system. The type of file can be determined by
using the file command. This information can be important when a user is attempting to open
or read a file. Determining the file type can help a user decide which program or command to use
to open the file.
The output from this command will most often be one of the following:
• Text – Examples include ASCII text, English text, commands text, and executable shell
scripts. The text file type also includes executable shell scripts. This type of file can be
read using the cat or more commands, which are discussed in this module, and edited
using vi or another editor.
The file command determines file type by referencing the first two bytes of the
file. See the contents of /etc/magic .
• Executable or Binary – Examples include 32-bit executable and extensible
linking format (ELF) code files and other dynamically linked executables. This file type
indicates that the file is a command or program. The strings command, shown on the
next page, will print out readable characters in this type of file. (The
output produced by strings is easily interpreted by someone with a programming
background. The command is introduced here solely as a method for demonstrating the
printable characters of an executable file.)
• Data – Data files are those which are created by an application running on the system. In
some cases the type of file is indicated; for example, FrameMaker document. When the
application in which this file was created cannot be determined by the file command,
the output will simply indicate data file. The only way to read a data file is to determine
which application created it and open the document with that application. If you have
many applications on your system, this can be a time consuming process.
For information on other file types, see the man pages.
Command Format
file filename( s)
Example Text File
$ file dante
dante: English text
Example Data File
$ cd /home/user2/dir1/coffees
$ file beans
beans: Frame Maker Document
FrameMaker must be used to read the beans file.
Example Executable File
$ file /usr/bin/cat
/usr/bin/cat: ELF 32-bit MSB executable SPARC
Version 1, dynamically linked,stripped
The strings command must be used to read /usr/bin/cat. For this
example, use strings followed by the file name as the command format.
$ strings /bin/cat
/lib/ld-linux.so.2
libc.so.6
stpcpy
stdout
putc_unlocked
memmove
getopt_long
__fpending
__ctype_b
puts
mbrtowc
malloc
abort
iswprint
<some output omitted>
Displaying Files
Using the cat Command
The cat (concatenate) command displays the contents of a text file on the screen. It is
often used to display short text files; because cat flashes through the entire file rapidly
without pausing, it is unsuitable for files longer than one screen in length. The cat
command is more often used to join two or more files into one large file.
Command Format
cat filename(s)
Using the cat Command to Display a Short Text File
$ cat dante
The Life and Times of Dante
by Dante Pocai
Mention “Alighieri” and few may know about whom you
are talking. Say “Dante,” instead, and the whole world
knows whom you mean. For Dante Alighieri, like Raphael
.
.
.
$
If the file fills more than one screen, the data scrolls off the screen—unless you are using
a scrolling window, such as a terminal window, within the CDE environment.
Using the cat Command to join two files into one
$ cat filename1 filename2 > file3
This example joins filename1 and filename2 files into filename3 file.
Note – If filename3 is existed, this command will overwrite it. You can use the same type
to join more files.
Using the more Command
Use the more command to display the contents of a text file to the screen one screen at a
time. If the information in a file is longer than one screen, the following message appears
at the bottom of the screen: --More--(n%)
where n is the percentage of the file already displayed.
The on-line manual pages use the more utility for display purposes, so the scrolling keys
in the following table are the same ones you used to display man pages.
Note – Using cat or more to read executable or binary files can cause a terminal or
window to hang.
Command Format
more filename(s)
At the --More-- prompt, you can use the following keys to control the scrolling
capabilities:
Table 6-2 Scrolling Keys
Scrolling Keys Purpose
Spacebar Scroll to the next screen
Return Scroll one line at a time
b Move back one screen
f Move forward one screen
h Display a Help menu of more features
q Quit and return to the shell prompt
/string Search forward for string
n Find next occurrence of string
Using the head Command
Use the head command to display the first n lines of one or more files. The first 10 lines
are displayed by default if the – n option is omitted.
Command Format
head [ - n ] filename(s)
Displaying a Specific Number of Lines at the Beginning of a File
$ head -6 /usr/dict/words
10th
1st
2nd
3rd
4th
5th
$
In this example, the head -6 command displays the first six lines of the
/usr/dict/words file.
Displaying a Specific Number of Lines at the Beginning of a Files
$ head -6 file1 file2 file3
==> file1 <==
xxxx
xxxxxxx
xxxxxxxxxxx
==> file2 <==
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxx
==> file2 <==
xxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x
Using the tail Command
Use the tail command to display the last n lines of a file. The last 10 lines are
displayed by default if the -n option is omitted.
Command Format
tail [ -n ] filename(s)
tail [ +n ] filename(s)
Displaying a Specified Number of Lines at the End of a File
$ tail -5 /usr/dict/words
zounds
z‘s
zucchini
Zurich
zygote
$
In this example, the tail -5 command displays the last five lines of the
/usr/dict/words file.
Displaying Lines From a Specific Point in the File
$ tail +23 /usr/dict/words
<Output not shown>
$
In this example, the tail +23 command displays lines 23 through the end of the
/usr/dict/words file.
Using the wc Command
Use the wc command to display a line, word, or character count of a file. This command
is useful when trying to determine characteristics of a file. Whereas the size can be
determined in bytes by using the ls -l command, much more information is obtained
with wc.
Command Format
wc [ options] filename(s)
Options
-l Counts lines
-w Counts words
-c Counts bytes
-m Counts characters
Using wc Without Options
$ wc dante
33 223 1320 dante
Using wc without options will give a line, word, and byte count of the contents of the
file.
Determining the Number of Lines in a File
$ wc -l dante
33 dante
Comparing Files
Locating Text Differences With the cmp Command
Comparing files to determine differences between them can be done using the cmp
command. This command produces no output if the files are exactly the same, or it
provides the byte and line numbers at which the first difference occurred if there are
discrepancies between the files.
Command Format
cmp file1 file2
Using the cmp Command to Compare Files That Appear to be the
Same
You would:
1. Do a long listing and compare the file sizes.
-rw-r--r--1 user2 edu 3528 July 6 16:19 /home/user2/cshrc
-rw-r--r- 1 user2 edu 3528 July 6 16:19 /home/user2/.cshrc
2. From the long listing, identify the date the files were last changed. If this date signifies
that the files are the same, use the cmp command.
$ cmp ~/cshrc ~/.cshrc
cshrc /home/user2/cshrc differ: char 1638, line 42
This output identifies the first occurrence of a difference between the two files. The
difference occurred at the 1,638th character position and was found on line 42.
You can now manually locate any further discrepancies and take whatever action you
deem necessary.
Note – The cmp command is quite useful when comparing binary files, but less useful
for text files. For more information on this command, see the man pages.
Using the cmp -l to see the differences in details
$ cmp file1 file2
2 157 117
7 47 107
15 147 142
. . .
. . .
. . .
offset character in file2 (in decimal)
character in file1 (in decimal)
Using the diff Command
Another command used for finding differences between files is the diff command. The
output of this command will display line-by-line differences between two text files.
Command Format
diff [option] file1 file2
Options
-i Ignores the case of letters; for example, A is equal to a.
-c Produces a listing of differences with three lines of context. With this option,
output format is modified slightly: output begins with identification of the files
involved and their creation dates. Following a
line of a dozen *s, the line numbers of file1 that are to be displayed are listed.
The actual lines from file1 are then displayed, appended by a – for those lines
that are different from file2. The same display follows as it applies to file2,
with a + appended for those lines that are different from file1.
Using diff to Compare Files
$ diff -c fruit fruit2
*** fruit Fri May 8 11:32:49 1998
--- fruit2 Fri May 8 14:55:21 1998
************
***2, 8****
orange
apple
banana
-pear
-mango
tomato
pomegranate
---2, 8----
orange
apple
banana
tomato
+guava
+mango
pomegranate
$ cat fruit
lemon
orange
apple
banana
pear
mango
tomato
pomegranate
$ cat fruit2
lemon
orange
apple
banana
tomato
guava
mango
pomegranate
File and Directory Naming Conventions
These include:
• File names are made up of a maximum of 255 alphanumeric characters.
• Some non-alphanumeric characters, such as underscores ( _ ), hyphens (-), and periods (.),
are allowed.
• While the shell will allow asterisks (*), ampersands (&), pipes (|), quotes (“ ”), and dollar
signs ($) to be used in a file name, this is not recommended, as these characters have
special meaning to the shell.
• File names may contain one or more extensions, usually appended to file by an
application. Extensions are usually one to three characters that are appended to the end of
a file name and are preceded by a period (.). You may choose to use this convention when
naming files, but it is not a necessary part of a file name.
• Directory names generally do not contain extensions, but there are no rules against it.
Note – To help distinguish between files and directories, some site administrators prefer their
users to start filenames with lowercase letters and directory names with capital letters.
Creating Files
You can create new, empty files using the touch command.
Command Format
touch filename(s)
Absolute and relative pathnames can be specified when creating files or directories.
Creating Empty Files
$ cd ~/practice
$ touch mailbox project research
$ ls
mailbox project research
Note – The touch command creates an empty file if the file name specified does not
exist. Otherwise, the access/modification time of the existing file is updated.
Creating and Appending Using the tee Command
Suppose you want to create a file which contains the names and phone numbers of your co-
workers. You can use the tee command as follows: