echo Action of the ToesAction of the Toes
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 |
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 |
Let us start with a simple example. We will print the text Action of the Toes to the terminal. It is the release name for R version 3.6.1.
echo Action of the ToesAction of the Toes
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.txtThe cat command reads data from files, and outputs their contents. It is the simplest way to display the contents of a file at the command line. It can be used to overwrite or append new content to files as well. cat stands for catenate and can be used to
| Command | Description |
|---|---|
cat |
Print & concatenate files |
cat > |
Concatenate several files into the target file |
cat >> |
Append several files into the target file |
cat -n |
Number all output lines |
Let us print the content of the release_names.txt file (it contains R release names).
cat release_names.txtUnsuffered Consequences
Great Pumpkin
December Snowflakes
Gift-Getting Season
Easter Beagle
Roasted Marshmallows
Trick or Treat
Security Blanket
Masked Marvel
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
If you want to number the output line, use the -n option.
cat -n release_names.txt 1 Unsuffered Consequences
2 Great Pumpkin
3 December Snowflakes
4 Gift-Getting Season
5 Easter Beagle
6 Roasted Marshmallows
7 Trick or Treat
8 Security Blanket
9 Masked Marvel
10 Good Sport
11 Frisbee Sailing
12 Warm Puppy
13 Spring Dance
14 Sock it to Me
15 Pumpkin Helmet
16 Smooth Sidewalk
17 Full of Ingredients
18 World-Famous Astronaut
19 Fire Safety
20 Wooden Christmas Tree
21 Very Secure Dishes
22 Very, Very Secure Dishes
23 Supposedly Educational
24 Bug in Your Hair
25 Sincere Pumpkin Patch
26 Another Canoe
27 You Stupid Darkness
28 Single Candle
29 Short Summer
30 Kite Eating Tree
To concatenate the contents of several files into a target file, use >. In the below example, we concatenate the contents of the files release_names_18.txt and release_names_19.txt into a single file release_names_18_19.txt. In this case we are not printing the contents of the file to the terminal and instead we concatenate the contents from both the files and redirect the output to the target file.
cat release_names_18.txt release_names_19.txt > release_names_18_19.txt
cat release_names_18_19.txtSomeone to Lean On
Joy in Playing
Feather Spray
Eggshell IglooGreat Truth
Planting of a Tree
Action of the Toes
The head command will display the firt 10 lines of a file(s) by default. It can be used to display the first few lines or bytes of a file as well.
| Command | Description |
|---|---|
head |
Output the first parts of a file |
head -n |
Output the first n lines of a file |
head -c |
Output the first c bytes of a file |
head -n -x |
Output everything but the last x lines of a file |
head -c -x |
Output everything but the last x bytes of a file |
Let us use head to display the first 10 lines of the release_names.txt file.
head release_names.txtUnsuffered Consequences
Great Pumpkin
December Snowflakes
Gift-Getting Season
Easter Beagle
Roasted Marshmallows
Trick or Treat
Security Blanket
Masked Marvel
Good Sport
Using the n option, we can specify the number of lines to be displayed. In the below example, we display the first 5 lines.
head -n 5 release_names.txtUnsuffered Consequences
Great Pumpkin
December Snowflakes
Gift-Getting Season
Easter Beagle
The c option can be used to display characters or bytes instead of lines. Let us display the first 5 bytes of the release_names.txt file.
head -c 5 release_names.txtUnsuf
To display the last parts of a file, use - while specifying the number of lines. In the below example, we display the last 5 lines of the file.
head -n -5 release_names.txtUnsuffered Consequences
Great Pumpkin
December Snowflakes
Gift-Getting Season
Easter Beagle
Roasted Marshmallows
Trick or Treat
Security Blanket
Masked Marvel
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
In this example, we display the last 3 bytes of the file using the c option and - while specifying the number of bytes.
head -c -3 release_names.txtUnsuffered Consequences
Great Pumpkin
December Snowflakes
Gift-Getting Season
Easter Beagle
Roasted Marshmallows
Trick or Treat
Security Blanket
Masked Marvel
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 Tre
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 |
Let us use tail to display the last 10 lines of the file.
tail release_names.txtVery 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
As we did in the previous section, use n to specify the number of lines to be displayed.
tail -n 5 release_names.txtAnother Canoe
You Stupid Darkness
Single Candle
Short Summer
Kite Eating Tree
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.txtGood 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
Use the c option to display the last 7 bytes of a file.
tail -c 7 release_names.txt Tree
The more command displays text, one screen at a time. It opens a file for
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 |
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 |
The sort command will sort the contents of text file, line by line. Using additional options, we can
Using the sort command, the contents can be sorted numerically and alphabetically. By default, the rules for sorting are:
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 |
Let us sort the contents of the pkg_names.txt file. It contains names R packages randomly selected from CRAN.
sort pkg_names.txtASIP
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
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.txtwidyr
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
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.txtaccept
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
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.txt1. 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
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.txtASIP
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
wc will print newline, word, and byte counts for file(s). If more than one file is specified, it will also print total line.
wc release_names.txt 30 73 546 release_names.txt
wc -l release_names.txt30 release_names.txt
wc -w release_names.txt73 release_names.txt
wc -c release_names.txt546 release_names.txt
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.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.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).