4  Input/Output

In this chapter, we will explore commands that will

Command Description
echo Display messages
cat Print contents of a file
head Prints first ten lines of a file by default
tail Prints last ten lines of a file by default
more Open a file for interactive reading, scrolling & searching
less Open a file for interactive reading, scrolling & searching
sort Sort a file in ascending order

4.1 Display Messages

The echo command prints text to the terminal. It can be used for writing or appending messages to a file as well.

Command Description
echo Display messages
echo -n Print message without trailing new line
echo > file Write message to a file
echo >> file Append message to a file
echo -e Enable interpretation of special characters

4.1.2 Redirect Output

What if we want to redirect the output? Instead of printing the text to the terminal, we want to write it to a file. In such cases, use > along with the file name to redirect the output to the file. Keep in mind that > will overwrite files. If you want to append to files instead of overwriting, use >>.

echo Great Truth > release.txt

4.4 Tail

The tail command displays the last 10 lines of a file(s) by default. It can be used to display the last few lines or bytes of a file as well.

Command Description
tail Display the last part of a file
tail -n num Show the last num lines of a file
tail -n +num Show all contents of the file starting from num line
tail -c num Show last num bytes of a file
tail -f Keep reading file until Ctrl + C
tail -F Keep reading file until Ctrl + C; even if the file is rotated

4.4.1 Display the last parts of a file

Let us use tail to display the last 10 lines of the file.

tail release_names.txt
Very Secure Dishes
Very, Very Secure Dishes
Supposedly Educational
Bug in Your Hair
Sincere Pumpkin Patch
Another Canoe
You Stupid Darkness
Single Candle
Short Summer
Kite Eating Tree

4.4.2 Display the last 5 lines of a file

As we did in the previous section, use n to specify the number of lines to be displayed.

tail -n 5 release_names.txt
Another Canoe
You Stupid Darkness
Single Candle
Short Summer
Kite Eating Tree

4.4.3 Display all contents from line 10

We can use tail to display all contents of a file starting from a specific line. In the below example, we display all contents of the file starting from the 10th line using the n option and + prefix while specifying the number of lines.

tail -n +10 release_names.txt
Good Sport
Frisbee Sailing
Warm Puppy
Spring Dance
Sock it to Me
Pumpkin Helmet
Smooth Sidewalk
Full of Ingredients
World-Famous Astronaut
Fire Safety
Wooden Christmas Tree
Very Secure Dishes
Very, Very Secure Dishes
Supposedly Educational
Bug in Your Hair
Sincere Pumpkin Patch
Another Canoe
You Stupid Darkness
Single Candle
Short Summer
Kite Eating Tree

4.4.4 Display the last 10 bytes of a file

Use the c option to display the last 7 bytes of a file.

tail -c 7 release_names.txt
 Tree

4.5 More

The more command displays text, one screen at a time. It opens a file for

  • interactive reading
  • scrolling
  • and searching

Press space to scroll down the page, the forward slash (/) for searching strings, n to go to the next match and q to quit.

Command Description
more Open a file for interactive reading, scrolling & searching
space Page down
/ Search for a string; press n to go the next match
q Quit

4.6 Less

The less command is similar to more but offers more features. It allows the user to scroll up and down, go to the beggining and end of the file, forward and backward search and the ability to go the next and previous match while searching the file.

Command Description
less Open a file for interactive reading, scrolling & searching
space Page down
b Page up
G Go to the end of file
g Go to the start of file
/ Forward search
? Backward search
n Go to next match
N Go to previous match
q Quit

4.7 Sort

The sort command will sort the contents of text file, line by line. Using additional options, we can

  • sort a file in ascending/descending order
  • ignore case while sorting
  • use numeric order for sorting
  • preserve only unique lines while sorting

Using the sort command, the contents can be sorted numerically and alphabetically. By default, the rules for sorting are:

  • lines starting with a number will appear before lines starting with a letter.
  • lines starting with a letter that appears earlier in the alphabet will appear before lines starting with a letter that appears later in the alphabet.
  • lines starting with a lowercase letter will appear before lines starting with the same letter in uppercase.

Using additional options, the rules for sorting can be changed. We list the options in the below table.

Command Description
sort Sort lines of text files
sort -r Sort a file in descending order
sort --ignore-case Ignore case while sorting
sort -n Use numeric order for sorting
sort -u Preserve only unique lines while sorting

4.7.1 Sort

Let us sort the contents of the pkg_names.txt file. It contains names R packages randomly selected from CRAN.

sort pkg_names.txt
ASIP
AdMit
AnalyzeTS
AzureStor
AzureStor
BIGDAWG
BIOMASS
BIOMASS
BenfordTests
BinOrdNonNor
BioCircos
ClimMobTools
CombinePValue
Eagle
FField
ICAOD
MARSS
MIAmaxent
MIAmaxent
MIAmaxent
MVB
MVTests
MaXact
MaxentVariableSelection
OptimaRegion
OxyBS
PathSelectMP
PropScrRand
RJDBC
RPyGeo
SCRT
SMARTp
SPEDInstabR
SemiParSampleSel
SetMethods
SmallCountRounding
SpatioTemporal
SphericalK
SuppDists
Survgini
TIMP
TSeriesMMA
VineCopula
WGScan
WPKDE
accept
accept
addhaz
alfr
aweek
aweek
bayesbio
blink
breakfast
cbsem
corclass
crsra
cyclocomp
dagitty
disparityfilter
edfReader
errorlocate
expstudies
fermicatsR
foretell
gLRTH
gazepath
generalhoslem
geoknife
hdnom
hindexcalculator
ibd
interplot
kfigr
logNormReg
ltxsparklines
lue
mbir
mcmcabn
mev
mgcViz
mined
mlflow
mongolite
mongolite
mvShapiroTest
odk
overlapping
pAnalysis
pls
pmdplyr
poisbinom
randtests
redcapAPI
rgw
rless
rsed
rstudioapi
solitude
splithalfr
sspline
sybilccFBA
tailr
tailr
tictactoe
viridisLite
vqtl
widyr
widyr

4.7.2 Descending Order

Using the -r option which stands for --reverse the contents of the file can be sorted in descending/reverse order. Let us now sort the contents of the pkg_names.txt file in reverse order.

sort -r pkg_names.txt
widyr
widyr
vqtl
viridisLite
tictactoe
tailr
tailr
sybilccFBA
sspline
splithalfr
solitude
rstudioapi
rsed
rless
rgw
redcapAPI
randtests
poisbinom
pmdplyr
pls
pAnalysis
overlapping
odk
mvShapiroTest
mongolite
mongolite
mlflow
mined
mgcViz
mev
mcmcabn
mbir
lue
ltxsparklines
logNormReg
kfigr
interplot
ibd
hindexcalculator
hdnom
geoknife
generalhoslem
gazepath
gLRTH
foretell
fermicatsR
expstudies
errorlocate
edfReader
disparityfilter
dagitty
cyclocomp
crsra
corclass
cbsem
breakfast
blink
bayesbio
aweek
aweek
alfr
addhaz
accept
accept
WPKDE
WGScan
VineCopula
TSeriesMMA
TIMP
Survgini
SuppDists
SphericalK
SpatioTemporal
SmallCountRounding
SetMethods
SemiParSampleSel
SPEDInstabR
SMARTp
SCRT
RPyGeo
RJDBC
PropScrRand
PathSelectMP
OxyBS
OptimaRegion
MaxentVariableSelection
MaXact
MVTests
MVB
MIAmaxent
MIAmaxent
MIAmaxent
MARSS
ICAOD
FField
Eagle
CombinePValue
ClimMobTools
BioCircos
BinOrdNonNor
BenfordTests
BIOMASS
BIOMASS
BIGDAWG
AzureStor
AzureStor
AnalyzeTS
AdMit
ASIP

4.7.3 Ignore case

To ignore case while sorting contents, use the --ignore-case option. Time to sort the pkg_names.txt file while ignoring case.

sort --ignore-case pkg_names.txt
accept
accept
addhaz
AdMit
alfr
AnalyzeTS
ASIP
aweek
aweek
AzureStor
AzureStor
bayesbio
BenfordTests
BIGDAWG
BinOrdNonNor
BioCircos
BIOMASS
BIOMASS
blink
breakfast
cbsem
ClimMobTools
CombinePValue
corclass
crsra
cyclocomp
dagitty
disparityfilter
Eagle
edfReader
errorlocate
expstudies
fermicatsR
FField
foretell
gazepath
generalhoslem
geoknife
gLRTH
hdnom
hindexcalculator
ibd
ICAOD
interplot
kfigr
logNormReg
ltxsparklines
lue
MARSS
MaXact
MaxentVariableSelection
mbir
mcmcabn
mev
mgcViz
MIAmaxent
MIAmaxent
MIAmaxent
mined
mlflow
mongolite
mongolite
MVB
mvShapiroTest
MVTests
odk
OptimaRegion
overlapping
OxyBS
pAnalysis
PathSelectMP
pls
pmdplyr
poisbinom
PropScrRand
randtests
redcapAPI
rgw
RJDBC
rless
RPyGeo
rsed
rstudioapi
SCRT
SemiParSampleSel
SetMethods
SmallCountRounding
SMARTp
solitude
SpatioTemporal
SPEDInstabR
SphericalK
splithalfr
sspline
SuppDists
Survgini
sybilccFBA
tailr
tailr
tictactoe
TIMP
TSeriesMMA
VineCopula
viridisLite
vqtl
WGScan
widyr
widyr
WPKDE

4.7.4 Numeric Order

To sort numerically, use the -n option which stands for --numeric-sort. In this example, we will use a different file, package_names.txt where the package names are prefixed by random numbers between 1 and 100.

sort -n package_names.txt
1. cyclocomp
2. odk
3. redcapAPI
4. TIMP
5. pls
6. BinOrdNonNor
7. bayesbio
8. MVTests
9. pAnalysis
10. aweek
11. hdnom
12. ltxsparklines
13. MaXact
14. RJDBC
15. MIAmaxent
16. randtests
17. ASIP
18. gazepath
19. mcmcabn
20. rless
21. corclass
22. vqtl
23. disparityfilter
24. SCRT
25. RPyGeo
26. blink
27. gLRTH
28. splithalfr
29. sspline
29. sspline
30. logNormReg
31. BIGDAWG
31. BIGDAWG
32. SPEDInstabR
33. tailr
33. tailr
34. ibd
35. fermicatsR
36. mlflow
37. CombinePValue
38. BenfordTests
39. mev
40. MaxentVariableSelection
41. rstudioapi
42. OptimaRegion
43. accept
44. expstudies
45. solitude
45. solitude
46. cbsem
47. SMARTp
48. geoknife
49. SemiParSampleSel
50. mbir
51. interplot
52. ClimMobTools
53. MVB
54. OxyBS
55. hindexcalculator
56. MARSS
57. generalhoslem
58. alfr
59. AdMit
60. Eagle
61. PropScrRand
62. lue
63. dagitty
64. viridisLite
65. mined
65. mined
66. SuppDists
67. tictactoe
68. AzureStor
68. AzureStor
69. FField
70. rsed
70. rsed
71. kfigr
72. overlapping
72. overlapping
73. VineCopula
74. crsra
75. pmdplyr
76. errorlocate
77. SetMethods
78. sybilccFBA
79. mvShapiroTest
80. SpatioTemporal
81. mgcViz
82. breakfast
83. WPKDE
84. BIOMASS
85. edfReader
86. mongolite
87. WGScan
88. SphericalK
89. foretell
90. widyr
91. rgw
92. BioCircos
93. PathSelectMP
94. ICAOD
95. TSeriesMMA
96. poisbinom
97. AnalyzeTS
98. SmallCountRounding
99. Survgini
100. addhaz

4.7.5 Preserve Only Unique Lines

The -u option which stands for --unique will preserve only unique lines while sorting the contents of the file. In the below example, we remove all duplicate lines from the pkg_names.txt while sorting.

sort -u pkg_names.txt
ASIP
AdMit
AnalyzeTS
AzureStor
BIGDAWG
BIOMASS
BenfordTests
BinOrdNonNor
BioCircos
ClimMobTools
CombinePValue
Eagle
FField
ICAOD
MARSS
MIAmaxent
MVB
MVTests
MaXact
MaxentVariableSelection
OptimaRegion
OxyBS
PathSelectMP
PropScrRand
RJDBC
RPyGeo
SCRT
SMARTp
SPEDInstabR
SemiParSampleSel
SetMethods
SmallCountRounding
SpatioTemporal
SphericalK
SuppDists
Survgini
TIMP
TSeriesMMA
VineCopula
WGScan
WPKDE
accept
accept
addhaz
alfr
aweek
bayesbio
blink
breakfast
cbsem
corclass
crsra
cyclocomp
dagitty
disparityfilter
edfReader
errorlocate
expstudies
fermicatsR
foretell
gLRTH
gazepath
generalhoslem
geoknife
hdnom
hindexcalculator
ibd
interplot
kfigr
logNormReg
ltxsparklines
lue
mbir
mcmcabn
mev
mgcViz
mined
mlflow
mongolite
mvShapiroTest
odk
overlapping
pAnalysis
pls
pmdplyr
poisbinom
randtests
redcapAPI
rgw
rless
rsed
rstudioapi
solitude
splithalfr
sspline
sybilccFBA
tailr
tictactoe
viridisLite
vqtl
widyr

4.8 Word Count

wc will print newline, word, and byte counts for file(s). If more than one file is specified, it will also print total line.

4.8.1 Count words, bytes and lines

wc release_names.txt
 30  73 546 release_names.txt

4.8.2 Count lines in a file

wc -l release_names.txt
30 release_names.txt

4.8.3 Count words in a file

wc -w release_names.txt
73 release_names.txt

4.8.4 Count characters(bytes) in a file

wc -c release_names.txt
546 release_names.txt

4.9 Exercises

  1. Run echo Great Truth > release_ex.txt followed by cat release_ex.txt — you should see just one line. Then run echo Action of the Toes >> release_ex.txt and cat release_ex.txt again — you should now see 2 lines. Explain in your own words the difference between > and >>, then clean up with rm release_ex.txt.
  2. In cline/, run cat -n release_names.txt (content with line numbers), then head -n 5 release_names.txt (first 5 lines) and tail -n 5 release_names.txt (last 5 lines). Use wc -l release_names.txt to get the total line count and check your head/tail outputs are consistent with it.
  3. In cline/, run sort pkg_names.txt, then sort -r pkg_names.txt (first and last lines swapped), then sort -u pkg_names.txt (duplicates removed — compare counts with wc -l). Then run sort -n package_names.txt on the number-prefixed file — lines should order numerically rather than alphabetically.

Worked solutions in solutions/input-output.md (attempt first).