rename
Replace words
To replace the word old-word with new-word in all .txt files we can use sed:
_$: sed -i 's/old-word/new-word/g' *.txt
Examples:
Change the word .yaml for .yml in the main.yml file:
_$: sed -i 's/.yaml/.yml/g' main.yml
Change the word DBPASS for DB_PASSWORD in all .php files:
_$: sed -i 's/DBPASS/DB_PASSWORD/g' *.php
Change the CRLF to LF:
_$: sed -i 's/\r//g' file.log
Rename files
To mass rename files we can use the rename command. It is highly advised to first run it with the -n flag turned on.
Examples:
Remove the .txt extension from those files:
_$: rename 's/\.txt$//' *.txt
Change the .yaml extension to .yml
_$: rename 's/\.yaml$/\.yml/' *.yaml
Change the awstats.nginx.* files to awstats.web1.* in the current directory:
_$: rename 's/awstats\.nginx\./awstats\.web1\./' ./*
Change the awstats*.nginx.* files to awstats*.web1.* in the current directory:
_$: rename 's/awstats(.*)\.nginx/awstats$1\.web1/g' ./*
Search a word in files
We will use find and grep for this task.
Example: Search for word in all .py files:
_$: find . -name "*.py" -type f -exec grep "word" {} \; > output.txt
_$: find . -name "*.py" -type f -exec grep "word" {} +