When working in the Linux shell, you can combine simple commands to perform complex tasks quickly and efficiently. In this post, we’ll explore a few powerful tools—grep
, redirection (>
), logical AND (&&
), pipes (|
), and how they work together. We’ll also touch on nano -
and wc
with practical examples.
Let’s break it all down.
1. grep
: Find What You’re Looking For
grep
is used to search for text patterns inside files or output. You can think of it as a text filter.
Basic Usage:
grep "hello" file.txt
This command searches for the word “hello” in file.txt
.
Useful Flags:
-i
: Case-insensitive search-r
: Recursive search through directories-n
: Show line numbers--color=auto
: Highlights matches
Example:
cat notes.txt | grep -i "error"
This prints all lines from notes.txt
containing “error”, regardless of case.
2. Redirection (Output Insertion) ( >
)
The >
operator redirects output from a command into a file. If the file doesn’t exist, it’s created. If it does, it gets overwritten.
Example:
echo "This is a test" > test.txt
This writes “This is a test” into test.txt
, replacing its contents if the file already exists.
If you want to append instead of overwrite, use >>
:
echo "Another line" >> test.txt
3. Run Only If Previous Succeeds ( &&
)
The &&
operator runs the second command only if the first command is successful (i.e., it returns an exit status of 0).
Example:
mkdir new_folder && cd new_folder
This will only try to enter new_folder
if it was successfully created.
4. Piping — Chain Commands ( |
)
The pipe |
takes the output of one command and feeds it into another as input. This is where the real power of the shell comes in.
Example:
ls -l | grep ".txt"
This lists only the .txt
files from the ls -l
output.
Another:
cat bigfile.txt | grep "error" | wc -l
This counts how many lines contain the word “error” in bigfile.txt
.
5. Edit from Standard Input ( nano -
)
Normally, nano
is used to open files, but when you use a dash -
, it reads from standard input. Combine it with echo
or pipes to edit output directly.
Example:
echo "Hello everyone, what is up" | nano -
This opens nano
with the echoed string in a new, unnamed buffer.
You can save it manually from there if needed.
6. Count Words, Lines, and Bytes ( wc
)
wc
stands for “word count”, but it can also count lines and characters.
Common Usage:
wc file.txt
This gives line, word, and byte count of the file.
Count only lines:
wc -l file.txt
Combine with grep:
grep -i "warning" log.txt | wc -l
This counts how many lines contain “warning” in log.txt
.
Tying It All Together
Let’s say you want to search for the word “failed” in all .log
files, count how many times it appears, and then save it to a file:
grep -i "failed" *.log | wc -l > failure_count.txt
Or you want to run a command only if the previous one completes:
cp backup.db /mnt/drive && echo "Backup copied successfully"
And to edit something quickly without saving a temporary file first:
echo "Quick note" | nano -
Final Thoughts
Once you start combining these commands—grep
, >
, &&
, |
, and tools like nano
and wc
—you’ll be able to manipulate and process text like a pro right from the command line. These techniques are foundational and widely used in scripting, log analysis, automation, and more.
Master them now, and your shell workflow will get a serious boost.