Read A File
Part of the Fundamentals section of Coddy's Terminal journey — lesson 11 of 82.
The cat command stands for concatenate. Its most common use is to read and display the contents of a file directly in the terminal.
Simply type cat followed by the filename:
cat readme.txtThe contents of the file will be printed to the terminal immediately.
Other useful ways to read files:
head — displays only the first 10 lines of a file:
head readme.txttail — displays only the last 10 lines of a file:
tail readme.txtYou can control how many lines to show with the -n option:
head -n 5 readme.txt # show first 5 lines
tail -n 3 readme.txt # show last 3 linesless — opens the file in a scrollable viewer. Press q to quit:
less readme.txtNote: cat is best for short files. For long files, less is more comfortable to read.
Challenge
BeginnerUse the cat command to read and display the contents of readme.txt.
Hint: Type
catfollowed by the name of the file you want to read.
Cheat sheet
The cat command reads and displays the contents of a file in the terminal:
cat readme.txtOther commands for reading files:
head displays the first 10 lines of a file:
head readme.txttail displays the last 10 lines of a file:
tail readme.txtUse the -n option to specify the number of lines:
head -n 5 readme.txt # show first 5 lines
tail -n 3 readme.txt # show last 3 linesless opens the file in a scrollable viewer (press q to quit):
less readme.txtTry it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline