Skip to content

Instantly share code, notes, and snippets.

@imtiazShakil
Created May 12, 2025 17:49
Show Gist options
  • Save imtiazShakil/03b2c9d62d7b153007abdc00025e0874 to your computer and use it in GitHub Desktop.
Save imtiazShakil/03b2c9d62d7b153007abdc00025e0874 to your computer and use it in GitHub Desktop.
Shell cheat sheet

Shorten the depth of directory in command-line:

  • edit .bashrc file: nano ~/.bashrc
  • add this line: PROMPT_DIRTRIM=2
  • exit terminal and open again
  • now only last two directories will printed in terminal

more info: https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-PROMPT_005fDIRTRIM
https://unix.stackexchange.com/questions/381113/how-do-i-shorten-the-current-directory-path-shown-on-terminal

Search bash history

Bash also has a special “recall” mode you can use to search for commands you’ve previously run, rather than scrolling through them one by one.

To use this Press Ctrl+R

Visual Man Page

By using Yelp, also known as the GNOME Help Browser, we can see man page with a gui. Example:

yelp man:command &
yelp info:command & #get the interactive pages for some commands

Shell Cheatsheet

Run Command as different user

sudo -u new-user command
sudo -u www-data ls -lah .

File Tools

  • Get directory size
du -sh .
du -h --max-depth=1 . | sort -hr # get inside 1 sub level and print size of each
  • Check permission

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.

  • display file permissions

To easily display all the permissions on a path, you can use:

namei -om /path/to/check

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment