DEV Community

yuna song
yuna song

Posted on

Linux Basics: File Viewing and Management Commands

1. touch — Create Empty Files or Update Timestamps

Creates an empty file or updates a file's timestamps.

Syntax

touch [options] <file>
Enter fullscreen mode Exit fullscreen mode

Common Usage

touch file.txt                    # Create an empty file
touch file1.txt file2.txt         # Create multiple files
touch existing.txt                # Update timestamps
touch file{1..5}.txt              # Create file1.txt ~ file5.txt
Enter fullscreen mode Exit fullscreen mode

Useful Options

touch -a file.txt                 # Update access time (atime)
touch -m file.txt                 # Update modification time (mtime)
touch -c file.txt                 # Do not create the file if it doesn't exist
touch -d "2026-07-04 12:30:00" file.txt
touch -t 202607041230 file.txt
touch -r source.txt target.txt    # Copy timestamps from another file
Enter fullscreen mode Exit fullscreen mode

Timestamp Types

  • atime : Last access time
  • mtime : Last modification time
  • ctime : Last metadata (status) change

View timestamps:

stat file.txt
Enter fullscreen mode Exit fullscreen mode

2. cat — Display and Concatenate Files

Displays file contents or combines multiple files.

Syntax

cat [options] <file>
Enter fullscreen mode Exit fullscreen mode

Common Usage

cat file.txt
cat file1.txt file2.txt
cat file1.txt file2.txt > combined.txt
Enter fullscreen mode Exit fullscreen mode

Create or Append Files

cat > new_file.txt        # Create or overwrite
cat >> file.txt           # Append to an existing file
Enter fullscreen mode Exit fullscreen mode

Press Ctrl + D to save and exit.

Useful Options

cat -n file.txt           # Show line numbers
cat -b file.txt           # Number non-empty lines only
cat -s file.txt           # Squeeze multiple blank lines
cat -E file.txt           # Show end-of-line markers ($)
cat -A file.txt           # Show all non-printing characters
Enter fullscreen mode Exit fullscreen mode

Notes

Avoid using cat for very large files. Instead, use:

head file.txt
tail file.txt
less file.txt
Enter fullscreen mode Exit fullscreen mode

3. less — View Large Files Efficiently

Displays large files one screen at a time without loading the entire file into memory.

Syntax

less file.txt
Enter fullscreen mode Exit fullscreen mode

Exit with:

q
Enter fullscreen mode Exit fullscreen mode

Navigation

Key Action
j / ↓ Next line
k / ↑ Previous line
Space / PgDn Next page
b / PgUp Previous page
g First line
G Last line

Search

/keyword      Search forward
?keyword      Search backward
n             Next match
N             Previous match
Enter fullscreen mode Exit fullscreen mode

Useful Options

less -N app.log           # Show line numbers
less -i app.log           # Case-insensitive search
less +F app.log           # Follow file like tail -f
ps -ef | less             # View command output page by page
Enter fullscreen mode Exit fullscreen mode

Notes

  • Press Ctrl + C to exit follow mode (+F).
  • Press Shift + F to resume following.

4. head — Display the Beginning of a File

Prints the first part of a file.

By default, head displays the first 10 lines.

Syntax

head [options] <file>
Enter fullscreen mode Exit fullscreen mode

Common Usage

head file.txt
head -n 5 file.txt
head -5 file.txt
head -c 20 file.txt
Enter fullscreen mode Exit fullscreen mode

Useful Options

head -q file1.txt file2.txt     # Hide file headers
head -v file.txt                # Always show file header
Enter fullscreen mode Exit fullscreen mode

Practical Examples

Display lines 11–15:

head -n 15 system.log | tail -n 5
Enter fullscreen mode Exit fullscreen mode

Show the five most recently modified files:

ls -lt | head -n 6
Enter fullscreen mode Exit fullscreen mode

5. tail — Display the End of a File

Prints the last part of a file.

By default, tail displays the last 10 lines.

Syntax

tail [options] <file>
Enter fullscreen mode Exit fullscreen mode

Common Usage

tail file.txt
tail -n 5 file.txt
tail -5 file.txt
tail -n +20 file.txt
Enter fullscreen mode Exit fullscreen mode

Real-Time Monitoring

tail -f app.log              # Follow file updates
tail -F app.log              # Continue following after log rotation
tail -f app.log --pid=1234   # Stop when process exits
Enter fullscreen mode Exit fullscreen mode

Stop monitoring with Ctrl + C.

Useful Options

tail -n 30 file.txt
tail -c 100 file.txt
Enter fullscreen mode Exit fullscreen mode

Practical Examples

Monitor only error messages:

tail -f app.log | grep "ERROR"
Enter fullscreen mode Exit fullscreen mode

Notes

  • -f follows a file as it grows.
  • -F is preferred for log files because it handles log rotation automatically.

Top comments (0)