Member-only story
10 Frequently Used Bash Scripts and Commands
A cheat sheet of useful Linux scripts and commands
Sometimes you need to use Bash to accomplish tasks similar to ones you’ve done in the past. Did you have to google the same commands all over again? If you don’t know the syntax by heart, you can speed up your development with the help of templates. This way, it’s easier to adapt the code for future use.
In this tutorial, I’ll provide a collection of handy Linux scripts and commands. You can use it as a cheat sheet. I’ve gathered the most frequently used examples based on my experience.
Let’s get started!
1. Search for Files Recursively To Find an Expression
This command will list all filenames containing the string nltk
:
$ find . -type f -exec grep -l "nltk" {} \;
Note that the -l
option lists the filenames only.
2. Print the Elapsed Time of Code Execution
This script is useful when you want to track the code execution’s time:
3. Search and Replace Strings in Files
This command will replace strings equal to localhost:8000
with localhost:8080
in all files:
$ find . -type f -exec grep -l "localhost:8000" {} \; | xargs sed -i 's/localhost:8000/localhost:8080/g'
4. Delete Specific Files
- This command deletes all empty files ending with
.log
:
$ find . -type f -name "*.log" -exec rm {} \;
- To delete all files older than 25 days, run this command:
$ find . -type f -mtime +25 -exec rm {} \;
5. Execute Command on Each File if a Condition Is Met
This script loops through all files in the current directory and checks if the…