Sed
Replace the first occurrence of a string in a file, and print the result
sed 's/find/replace/' filename
Replace all the occurrences of a string in a file, and print the result
sed 's/find/replace/g' filename
Replace all occurrences of an extended regular expression in a file
sed -E 's/regular_expression/replace/g' filename
Replace in place all the occurrences of a string in a file and overwrite the file
sed -i '' 's/find/replace/g' filename
Add prefix to all files
sed -i '1s/^/# Copyright (c) MIT License\n\n/' *
Replace only on lines matching the line pattern
sed '/line_pattern/s/find/replace/' filename
Print only text between n-th line till the next empty line:
sed -n 'line_number,/^$/p' filename
Apply multiple find-replace expressions to a file:
sed -e 's/find/replace/' -e 's/find/replace/' filename
Replace separator / by any other character not used in the find/replace patterns, e.g. ;
sed 's;find;replace;' filename
sed '\;deleteme;d' filename
Replacing Newlines with sed
sed ':a;N;$!ba;s/\n//g' filename
Delete the line at the specific line number in a file:
- Any line number:
sed '<line_number>d' filename
- First line:
sed '1d' filename
- Last line:
sed '$d' filename
- Multiple lines:
sed '1d;3d;5d' filename
- Exclude lines:
sed '1,3!d' filename
- Delete empty lines in a file:
sed '/^$/d’ filename
Find
The find
command is an indispensable tool in the Linux toolkit, offering unparalleled ability to search and manipulate files across complex directory hierarchies. From finding files by name to searching based on file attributes or sizes, find
does it all.
Understanding Basic Syntax
The basic syntax of find
is straightforward: find [path] [options]
. For instance, to find all .txt
files in the current directory, you’d use:
find . -name "*.txt"
Advanced Search Criteria
find
goes beyond simple name searches. You can use it to locate files modified within a specific timeframe, such as files changed in the last 7 days:
find . -type f -mtime -7
Or, find files with specific permissions:
find /path/to/dir -type f -perm 0664
Executing Actions on Found Files
One of the most powerful aspects of find
is executing actions on the files it locates. For example, to delete all .tmp
files:
find . -name "*.tmp" -exec rm {} \;
Optimizing Search Performance
To enhance performance, limit the search depth. For instance, to search only the current and immediate subdirectories:
find . -maxdepth 2 -name "*.log"
Real-World Scenarios and Examples
In practical scenarios, find
can be a lifesaver. Imagine you need to locate all JPEG files over 5MB in size and move them to a separate directory. You can accomplish this with a single command:
find /photos -type f -name "*.jpg" -size +5M -exec mv {} /large-photos \;
Tail
Displays the end of a file.
tail -f
continuously monitors a file for new lines, great for monitoring log files in real-time.
Head
Shows the beginning of a file.
Specify the number of lines to display (e.g., head -n 5
for the first 5 lines).
Sort
Sorts the contents of a file.
Can sort numerically (-n
), reverse order (-r
), or even sort by a specific column (-k
).
Tee
Reads from standard input and writes to standard output and files simultaneously.
Useful for logging the output of a command to a file while also displaying it (e.g., ls | tee output.txt
).
Xargs
Builds and executes command lines from standard input.
Great for combining with other commands to process a list of items, like files or strings. For example, find . -type f | xargs grep "searchTerm"
to search for a term in multiple files.