Cheat Sheet :: Linux Shell Tricks

To find the corresponding hex code

1
echo -n "&" | hexdump -C

How to redirect standard error in bash

CommandDescription/Purpose
command 2>filenameRedirect stderr to filename
command >output 2>filenameRedirect stderr to file named filename and stdout to file named output
command &> filenameRedirect stderr and stdout to filename
command 2>&-Just suppress error messages. No file created. No error message displayed on screen
command 2>/dev/nullJust suppress error messages.
command 2>&1Redirect error messages to standard output. Useful in shell script when you need to forcefully display error messages on screen

Removing blank spaces and special chars

Note that we cannot just type the accent ^, instead we must use CTRL+V and then type CTRL+M.

Using Vim Editor

Using sed

1
sed -e "s/^M//" file-orig.sh > file-mod.sh

Using Dos2Unix

1
2
dos2unix input
dos2unix -b input

Using tr

To delete a CRLF:

1
tr -d '\r' < input > output

Using egrep

1
egrep -v "^#|^$" squid.original > squid.conf

Using grep

1
grep -v ^["#"] squid.original > squid.conf

Interactive shell

Using rlwrap

1
2
rlwrap nc -nlv 443
rlwrap ./exploit.sh

Using python

1
python -c 'import pty;pty.spawn("/bin/bash")'

Or

1
/usr/bin/python3 -c 'import pty;pty.spawn("/bin/bash")'
1
stty raw -echo

Backup of files permissions

1
getfacl -Rp /home/psylinux/Desktop > /home/psylinux/permissoes_desktop.txt
1
setfacl --restore=/home/psylinux/permissoes_desktop.txt -R

1
apt install stow
1
stow -v -t Target Folder1 Folder2 Folder3
1
stow -v -D -t Target Folder1 Folder2 Folder3
1
stow -v -R -t Target Folder1 Folder2 Folder3 Folder4
1
stow -v -R -t Target Folder1 --ignore='SubFolder1' --ignore='SubFolder2'
1
stow -v -R -t Target Folder1 --ignore='(?:\..*|[^+]*\+[^+]*)'