Chapter 4 Input/Output
In this chapter, we will explore commands that will
- display messages
- print file contents
- sort file contents
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.1 Print Message
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 Toes
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.2 Print & Concatenate Files
The 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
- display text files
- copy text files into a new document
- append the contents of a text file to the end of another text file, combining them
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 |
4.2.1 Print Content
Let us print the content of the release_names.txt
file (it contains R release names).
cat release_names.txt
## Unsuffered 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
4.2.2 Number All Output Lines
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
4.2.3 Concatenate Several Files
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.txt
## Someone to Lean On
## Joy in Playing
## Feather Spray
## Eggshell IglooGreat Truth
## Planting of a Tree
## Action of the Toes
4.3 Head
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 |
4.3.1 Output the first parts of a file
Let us use head
to display the first 10 lines of the release_names.txt
file.
head release_names.txt
## Unsuffered Consequences
## Great Pumpkin
## December Snowflakes
## Gift-Getting Season
## Easter Beagle
## Roasted Marshmallows
## Trick or Treat
## Security Blanket
## Masked Marvel
## Good Sport
4.3.2 Output the first n lines of a file
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.txt
## Unsuffered Consequences
## Great Pumpkin
## December Snowflakes
## Gift-Getting Season
## Easter Beagle
4.3.3 Output the first c bytes of a file
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.txt
## Unsuf
4.3.4 Output everything but the last 5 lines of a file
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.txt
## Unsuffered 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
4.3.5 Output everything but the last 3 bytes of a file
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.txt
## Unsuffered 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
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