Home Bash useful commands
Post
Cancel

Bash useful commands

Bash, or the Bourne-Again Shell, is a command-line interpreter and shell scripting language. It is the default shell for most Linux distributions and macOS. Bash is a powerful tool that can be used to automate tasks, manage files and directories, and interact with the operating system.

Bash Basic

CommandUsage
sudo su - <any user>Change user
ls / ls -l Show all files & directories
pwdShow current directory
cdChange directory: cd /opt to go up cd..
mvMove files and directories mv *.zip ../zip
mkdirCreate directory
findto find anything find . -name "*.py"

Zip my modules all at once

1
2
3
4
5
for i in *
 do
    zip "$i.zip" -r "$i"
 done
 # unzip module.zip

Search in all the code and get modules then move it

1
find odoo -type f -name "__manifest__.py" -exec dirname {} \; |  xargs -I % mv % code-lib

Remove all pyc

1
find . | grep -E "(/__pycache__$|\.pyc$|\.pyo$)" | xargs rm -rf

Move all the directories in the list

1
 while IFS= read -r dir; do mv -vt newdir/ "$dir"; done < dirlist.txt

Find a word inside all files and replace it

1
find ./ -type f -exec sed -i 's/mod/web/g' {} \;

find a word inside file name and replace it

1
2
find . -type f -name '*mod*' -exec  bash -c ' mv $0 ${0/\mod/web}' {} \;
find . -name 'odoo*' -exec bash -c 'mv "$1" "${1/odoo/modeem}"' -- {} \;

Count code lines

1
( find ./ -name '*.py' -print0 | xargs -0 cat ) | wc -l
This post is licensed under CC BY 4.0 by the author.