Linux Commands

Table of Contents

Misc. Linux commands.

Current directory

echo ${PWD##*/}

Find x-days before and action

In actuality, it should be (x-1).

/bin/find <path> -maxdepth 1 -mtime +<x> -type f -name "<pattern>" -exec rm -f {} \;

Find x-minutes before and action

/bin/find <path> -maxdepth 1 -mmin +<x> -type f -name "<pattern>" -exec rm -f {} \;

Change password last modify

chage -d YYYY-MM-DD <user>

Change password never expire

chage -I -1 -m 0 -M 99999 -E -1 <user>

Lock/Unlock Account

passwd -l <user>  # Lock
passwd -u <user>  # Unlock

usermod -L <user> # Lock
usermod -U <user> # Unlock

pam_tally2 -r -u <user> # Reset fail login count

iptable ban ip

iptable -A INPUT -s <IP> -j DROP
ip6table -A INPUT -s <IP> -j DROP

Fail2ban ban ip manually

fail2ban-client status # show jail list
fail2ban-client -vvv set <jail from list> banip <ip>

zsh range for loop

for i in {1..10};do .....

bash string comparison

if [ "$str" = "string" ]; then ...

bash/zsh numeric comparison

if [[ ${A} = 3 ]]; then echo yes; fi

Disable journal on ext4

tune2fs -O ^has_journal /dev/<disk>

Rotate frame buffer

echo 1 > /sys/class/graphics/fbcon/rotate_all
echo 1 > /sys/class/graphics/fbcon/rotate

Show all mDNS/Bonjour entries

avahi-browse -a
avahi-browse -a -d <domain> # specify domain other than .local

Create sparse file

truncate -s <size> <filename>

$ truncate -s 10G 10G.txt

$ ls -lah 10G.txt
-rw-r--r--    1 user user   10.0G Apr  1 00:00 10G.txt

$ du -sh 10G.txt
0       10G.txt

Ref: Sparse file wikipedia


rsync -vahpt --size-only --stats --del <source> <target>

<source> is put/sync INTO <target> directory, not replacing <target>.

Curl skip certificate checking

curl -k ... # Use -k to skip certificate check.

Resize Filesystem

Usuallly use after a partition / image resize

resize2fs <device>
resize2fs /dev/sda1
John Siu
Minimize the Effort, Maximize the Effect!
comments powered by Disqus