Have a Question?
< All Topics
Print

Quick BASH Commands

Quick BASH Commands

1. Search for Files Recursively To Find an Expression

$ find . -type f -exec grep -l "nltk" {} \;

2. Print the Elapsed Time of Code Execution

3. Search and Replace Strings in Files

$ find . -type f -exec grep -l "localhost:8000" {} \; | xargs sed -i 's/localhost:8000/localhost:8080/g'

4. Delete Specific Files

$ find . -type f -name "*.log" -exec rm {} \;
$ find . -type f -mtime +25 -exec rm {} \;

5. Execute Command on Each File if a Condition Is Met

6. Download Files From a Remote Server

$ scp username@server:path/to/file destination_path

7. Upload Files to a Remote Server

$ scp -r /local/dir username@server:/remote/dir
$ scp file.txt username@server:/remote/dir/newfilename.txt

8. Copy Files Between Two Remote Servers

$ scp user1@server1:/dir1/file.txt user2@server2:/dir2

9. Pass an Argument to a Script

echo "The first argument is: $1"
./myscript.sh myargument

10. Assign a Variable to a Command Result

Conclusion

Table of Contents