Skip to content

Latest commit

 

History

History
352 lines (253 loc) · 6.73 KB

Bash.md

File metadata and controls

352 lines (253 loc) · 6.73 KB

Bash

Files

Symlink content of directory

cp --symbolic-link /path/to/source/* /path/to/destination

Create new file with content

echo '<text>' > <file>

Use >> to append text to existing file

Download a file

wget -P <destination-path> <file-url>

Make a file executable

chmod +x <file>

Search

Return filenames with searched text in subfolders

grep -lr '<word>' .

Search specific words in given files

grep -n '\(<word1>\|<word2>\|<word3>\)' <path>/*.ext

Find empty subfolders

find <path> -type d -empty

Find file by extension

find <path> -type f -name "*.<extension>"

Count files in subfolders

find <path> -type f | wc -l

List files recursively

find . -type f

Filter unique grep results

grep <word> <file> | sort | uniq

Search latest occurences

tac <file> | grep -m 10 '<word>'

Search in compressed file

zgrep '<word>' <file>

Security

Change recursively permissions and group inside a folder

chmod -R <octal> <path>
chgrp -R <group> <path>

Change recursively permissions and group inside a folder excluding some files

'.?*' for hidden files

find <path> \( -name '.?*' -o -name '*.<extension>' \) -prune -o -exec chmod <octal> {} +
find <path> \( -name '.?*' -o -name '*.<extension>' \) -prune -o -exec chgrp <group> {} +

Packages

Install a package

sudo apt install <package>

Update and upgrade

sudo apt update && sudo apt full-upgrade

Clean cache

sudo apt clean

Search in packages list

apt search <value>

Check if a package is present

dpkg -l <package>

Uninstall a package

sudo apt purge <package>
sudo apt autoremove

Add key

curl --silent <url>.key | sudo apt-key add -

Logs

Display Apache logs in realtime

tail -f /var/log/apache2/error.log
tail -f /var/log/apache2/access.log

Disk

Display disk free

df -h

Display disk free inodes

df -i

Display directory usage

du -hs <path>

Display subdirectories usage

Ordered by size

du -h --max-depth=1 <path> | sort -hr

Restart services

Apache

sudo /etc/init.d/apache2 graceful
sudo /etc/init.d/apache2 restart

PostgreSQL

sudo /etc/init.d/postgresql restart

Tomcat

sudo /etc/init.d/tomcat-tomcat1 restart

Proxy

Most programs

File /etc/environment

http_proxy=http://<server>:<port>/
https_proxy=http://<server>:<port>/
ftp_proxy=http://<server>:<port>/
HTTP_PROXY=http://<server>:<port>/
HTTPS_PROXY=http://<server>:<port>/
FTP_PROXY=http://<server>:<port>/
no_proxy="127.0.0.1, localhost"

Advanced Packaging Tool and Update Manager

File /etc/apt/apt.conf.d/95proxies

Acquire::http::proxy "http://<server>:<port>/";
Acquire::ftp::proxy "http://<server>:<port>/";
Acquire::https::proxy "http://<server>:<port>/";

GTK based programs

gsettings set org.gnome.system.proxy mode "manual"
gsettings set org.gnome.system.proxy.http host "<server>"
gsettings set org.gnome.system.proxy.http port <port>
gsettings set org.gnome.system.proxy.ftp host "<server>"
gsettings set org.gnome.system.proxy.ftp port <port>
gsettings set org.gnome.system.proxy.https host "<server>"
gsettings set org.gnome.system.proxy.https port <port>

Git

git config --global url."https://".insteadOf git://

Monitoring

Get HTTP status

curl -Is <url> | head -n 1

Miscellaneous

Check OS version

lsb_release -a

Pass HTTP Referer in URL

curl --referer <referer> "<url>"

Get current public IP

curl ipinfo.io/ip

Locate executable

which <executable>

Bash scripts

Exit on error

set -e

Display script duration

echo "Script finished in $SECONDS seconds"
echo "Script finished in $(($SECONDS / 3600))hrs $((($SECONDS / 60) % 60))min $(($SECONDS % 60))sec"