Interacting with Files
In Linux, files can be read, created, or manipulated in a variety of ways.
This guide introduces several commonly used commands and their most relevant
flags. We’ll skip deep dives on redirection (>
, >>
)
and piping (|
) for a separate page, but you’ll still see them
referenced in examples.
1. Introduction
Many day-to-day tasks revolve around reading, creating, or manipulating files. From quickly checking a file’s contents to copying or removing entire directories, these commands form the basis of Linux file operations.
2. Key Commands
Below is a collection of commands used for file interaction. Each includes a table of the most common flags or usage patterns.
2a. cat
The cat
command reads the contents of a file (or multiple files)
and outputs it to standard output. It’s commonly used to quickly display or
concatenate file contents.
Usage / Flag | Description |
---|---|
cat [file] |
Prints the contents of [file] to the screen. |
cat file1 file2 |
Concatenates file1 and file2 , outputting them together. |
cat file1 > file2 |
Redirects the contents of file1 into file2 (overwrites file2 ). |
2b. tail
tail
displays the last part of a file. It is particularly useful
for monitoring logs in real time when combined with the -f
option.
Flag / Usage | Description |
---|---|
tail [file] |
Shows the last 10 lines of [file] . |
tail -n 20 [file] |
Shows the last 20 lines of [file] . |
tail -f [file] |
Follows [file] in real time, displaying new lines as they appear. |
2c. grep
grep
searches for lines in a file (or stream) that match a given pattern.
It’s frequently piped after other commands (e.g., ls | grep txt
) to filter output.
Flag / Usage | Description |
---|---|
grep [pattern] [file] |
Searches [file] for [pattern] and prints matching lines. |
grep -i [pattern] [file] |
Case-insensitive search for [pattern] . |
grep -r [pattern] [dir] |
Recursively searches all files under [dir] for [pattern] . |
2d. echo
echo
outputs the given arguments to standard output.
Often used in scripts or combined with redirection to create or append files.
Usage | Description |
---|---|
echo "Hello World" |
Prints “Hello World” to the screen. |
echo "New line" >> file.txt |
Appends “New line” to file.txt . |
echo $PATH |
Prints the current PATH environment variable. |
2e. touch
touch
creates new empty files or updates timestamps of existing ones.
It’s handy for quickly generating placeholder files.
Flag / Usage | Description |
---|---|
touch [filename] |
Creates [filename] if it doesn’t exist, or updates timestamps if it does. |
touch file1 file2 |
Creates or updates multiple files at once. |
2f. cp
cp
(copy) duplicates files or directories to a new location. By default,
it leaves the original file(s) intact.
Flag / Usage | Description |
---|---|
cp [source] [dest] |
Copies [source] to [dest] . Multiple files can be copied if [dest] is a directory. |
cp -r [dir] [dest] |
Recursively copies the contents of [dir] to [dest] . |
cp -v [source] [dest] |
Verbose mode: prints each file as it’s copied. |
2g. mv
mv
can relocate files or directories to another path, or rename them
if the source and destination are in the same location.
Flag / Usage | Description |
---|---|
mv [source] [dest] |
Moves (or renames) [source] to [dest] . |
mv -i [source] [dest] |
Interactive mode: prompts before overwriting an existing file. |
mv -v [source] [dest] |
Verbose mode: prints each file as it’s moved or renamed. |
2h. rm
rm
(remove) deletes files or directories. Use with caution,
as deletions are typically permanent in Linux.
Flag / Usage | Description |
---|---|
rm [file] |
Removes [file] . Cannot remove directories without -r . |
rm -r [directory] |
Recursively removes a directory and all its contents. |
rm -i [file] |
Interactive mode: asks for confirmation before removal. |
3. Conclusion
With these file-based commands, you can view, create, move, copy, and remove files as needed. In later topics, we’ll explore redirection, piping, and advanced text processing to further expand your control over file manipulation.