Creating & Viewing Files | touch, cat, head, tail, less, more | Mac/Linux Terminal
Video: Creating & Viewing Files | touch, cat, head, tail, less, more | Mac/Linux Terminal by Taught by Celeste AI - AI Coding Coach
Watch full page →Creating & Viewing Files with touch, cat, head, tail, less, and more
Learn how to efficiently create and inspect files using essential Mac and Linux terminal commands. This guide covers creating files with touch, viewing contents with cat, previewing file beginnings and endings using head and tail, and navigating large files interactively with less and more.
Code
# Create an empty file named example.txt
touch example.txt
# Create multiple empty files at once
touch file1.txt file2.txt file3.txt
# Update the modification timestamp of an existing file
touch example.txt
# Display the entire contents of a file
cat example.txt
# Display contents of multiple files sequentially
cat file1.txt file2.txt
# Show the first 10 lines of a file (default)
head example.txt
# Show the first 5 lines of a file
head -n 5 example.txt
# Show the last 10 lines of a file (default)
tail example.txt
# Show the last 20 lines of a file
tail -n 20 example.txt
# Follow a file in real-time (e.g., monitoring logs)
tail -f /var/log/system.log
# View a file interactively with navigation (scrolling, searching)
less example.txt
# View a file page-by-page (simpler navigation)
more example.txt
Key Points
touchcreates empty files or updates modification timestamps without altering content.catoutputs entire file contents and can concatenate multiple files, ideal for small files.headandtailpreview the start or end of files, with options to specify line counts.tail -fis invaluable for real-time monitoring of growing files like logs.lessoffers powerful, bidirectional navigation and searching for large files, whilemoreprovides simpler forward-only paging.