┌──────────────────────────────────────────────┐
│ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ │
│ ░░░░ ╔═══╗ ░░░░░ AWESOME BASH COMMANDS ░░░░░ │
│ ░░░░ ╚═╦═╝ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ │
│ ░░░ ╒══╩══╕ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ │
│ ░░░ └────°┘ ░░ A curated list of awesome ░░░ │
│ ░░░░░░░░░░░░░░░░░ Bash useful commands ░░░░░ │
│ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ │
└──────────────────────────────────────────────┘
A curated list of awesome Bash useful commands. Inspired by awesome-shell and bash-handbook.
- Files and directories
- Paths
- Devices
- Users and Groups
- Date & Time
- Network
- Miscellaneous
- Other Awesome Lists
ls -l -X
# Or
ls -l --sort=extension
ln -s /var/www/html ~/www
ln -s ~/my/large/path/file.txt ~/myfile.txt
chmod -h 770 ~/www
chown -h www-data:www-data ~/www
stat -c "%a %n" /var/www/html
# 770 /var/www/html
find /var/www/html -type d -exec chmod g+s {} \
Note: Any new file created will have the same permissions as the root folder
find ./my/current/directory -type d -exec touch {}/.gitignore \;
find ./my/current/directory -name ".gitignore" -type f -delete
Copy shell command output to clipboard
cat myfile.txt | xclip -selection c
cp -avr /my/current/directory /destination/directory
# Or
rsync -av /my/current/directory /destination/directory
rsync -har --progress --exclude .git /current/directory/. /destination/directory
Show the space usage of file or directory (recursive) in human readable format.
du -sh /var/log/dnf.librepo.log
# 4,1M /var/log/dnf.librepo.log
du -sh /var/log
# 2,2G /var/log
du -Hsh $(which dart)
# 21M /usr/local/bin/dart
du -hS /home/user/some/directory | sort -rh | head -10
# 90G /home/user/some/directory/big-dir
# 10G /home/user/some/directory/subdir/another-big-dir
# ...
find /usr/local/apache/logs/archive/ -name '*2017.gz' -delete
This command move all *.js
files into *.ts
files (move equivalent)
find src/ -type f -name "*.js" -exec bash -c 'mv {} `echo {} | sed -e "s/.js/.ts/g"`' \;
rm -rf /tmp/* /tmp/.*
gzip -c FILENAME.txt | wc -c | awk '{
if ($1 > 1000 ^ 3) {
print($1 / (1000 ^ 3)"G")
} else if ($1 > 1000 ^ 2) {
print($1 / (1000 ^ 2)"M")
} else if ($1 > 1000) {
print($1 / 1000"K")
} else {
print($1)"b"
}}'
# 560K
which bash
# /usr/bin/bash
which git node
# /usr/bin/git
# /usr/bin/node
realpath ~/www
# /usr/share/nginx/html
pwd
# /home/my/current/directory
Show the file system disk space usage in human readable format.
df -h --total
# Filesystem Size Used Avail Use% Mounted on
# devtmpfs 487M 0 487M 0% /dev
# tmpfs 497M 0 497M 0% /dev/shm
# tmpfs 497M 508K 496M 1% /run
# tmpfs 497M 0 497M 0% /sys/fs/cgroup
# /dev/vda1 30G 2.7G 26G 10% /
# tmpfs 100M 0 100M 0% /run/user/0
# total 2.2T 600G 100G 20% -
free -h --total
# total used free shared buff/cache available
# Mem: 200G 60G 100G 262M 30G 180G
# Swap: 0B 0B 0B
# Total: 200G 60G 100G
or
cat /proc/meminfo
# MemTotal: 183815530 kB
# MemFree: 101918660 kB
# MemAvailable: 123712410 kB
# ....
Tip: Pipe grep
to filter your results. E.g cat /proc/meminfo | grep MemTotal
mount -t vfat /dev/sdb1 /media/usb
mount -o remount,size=5G /tmp/
sudo -Hu root fish
usermod -a -G ftp john
Show extended ISO format Date (ISO 8601)
date "+%Y-%m-%dT%H:%m:%S"
# 2018-09-13T10:09:26
ifconfig | awk '/<UP,BROADCAST,RUNNING,MULTICAST>/ { getline; print $2 }'
# or
ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1
a) Using hostnamectl
:
hostnamectl | grep -E "(.+)(System|Kernel|Arch)(.+)"
# Operating System: Arch Linux
# Kernel: Linux 5.1.7-arch1-1-ARCH
# Architecture: x86-64
b) Using cat /etc/os-release
:
cat /etc/os-release
# NAME="Arch Linux"
# PRETTY_NAME="Arch Linux"
# ID=arch
# BUILD_ID=rolling
# ANSI_COLOR="0;36"
# HOME_URL="https://www.archlinux.org/"
# DOCUMENTATION_URL="https://wiki.archlinux.org/"
# SUPPORT_URL="https://bbs.archlinux.org/"
# BUG_REPORT_URL="https://bugs.archlinux.org/"
# LOGO=archlinux
a)
od -vAn -N64 < /dev/urandom | tr '\n' ' ' | sed "s/ //g" | head -c 32
# 03121617301002504516642404031105
b)
env LC_CTYPE=C tr -dc "0-9" < /dev/urandom | head -c 32 | xargs
# 50569696992247151969921987764342
Change head
value to truncate the result's length.
a) Alphanumeric only
base64 /dev/urandom | tr -d '/+' | head -c 32 | xargs
# 3udiq6F74alwcPwXzIDWSnjRYQXcxiyl
b) Alphanumeric with a custom chars set
env LC_CTYPE=C tr -dc "A-Za-z0-9_!@#\$%^&*()-+=" < /dev/urandom | head -c 32 | xargs
# yiMg^Cha=Zh$6Xh%zDQAyBH1SI6Po(&P
Change tr -dc
char set to get a custom result.
od -vAn -N64 < /dev/urandom | tr '\n' ' ' | sed "s/ //g" | openssl dgst -sha256 | sed "s/-//g"
# 7adf57e0a90b32ce0e1f446268dbd62b583c649a2e71a426519c6e9c0006b143
Openssl digest algorithms supported: md5
, md4
, md2
, sha1
, sha
, sha224
, sha256
, sha384
, sha512
, mdc2
and ripemd160
uuidgen | tr "[:lower:]" "[:upper:]"
# D2DA7D0C-ABAA-4866-9C97-61791C9FEC89
This command generate one million of unique random phone numbers (random permutations) fast using GNU/Linux shuf command.
Use sed
command for customize each number format. For example for add some prefix or suffix. Remember shuf
is not limited to numbers only.
shuf -i 100000000-999999999 -n 1000000 | sed -e 's/^/51/' > gsm.txt
Please check out the contribution file.
To the extent possible under law, José Luis Quintana has waived all copyright and related or neighboring rights to this work.