Run Command as different user
sudo -u new-user command
sudo -u www-data ls -lah .
File Tools
du -sh .
du -h --max-depth=1 . | sort -hr # get inside 1 sub level and print size of each
Check if different user has read/write permissions to a file
The "test" command is designed for this use case.
sudo -u otheruser test -r /path/to/file; echo "$?"
# test -w for write permission
# test -x for execute permission
will return 0 if otheruser can read the file, or 1 if otheruser cannot read the file.
To easily display all the permissions on a path, you can use:
grep
grep a text file, pipe it with cut command to retreieve substring, then do a reverse and again cut & finally sort
grep -i 'iframe sandbox=' index.html | cut -d " " -f 16 | cut -c 6- | rev | cut -c 2- | rev | sort -r | grep 'html'
grep -m2 hello #To show only the first match with grep, use -m parameter,
grep -A2 -B2 "hello" #Show first two lines and next two lines of matched result
Cut command
cut -d " " -f 16 # use " " as delimeter and parse, get word[16]
cut -c 6- # cut character starting from index 6
tr -s ' ' | cut -d " " -f7 # use tr command to squeeze multiple consecutive spaaces into single space, so cut can perform nicely
Redirect stderr to stdout: 2>&1
ln command
ln -s {source-filename} {symbolic-filename}
ln -s {source-dir-name} {symbolic-dir-name}
utility to investigate sockets
ss --listening
ss -t -a # Display all TCP sockets.
ss -nt # -n, --numeric don't resolve service names