DEV Community

Fred B.
Fred B.

Posted on • Edited on

Ubuntu/Linux cmd-line Cheat Sheet

Originally posted on Codepen Oct 30, 2015

Convert binary, hex, or any base, into decimal

from hex to decimal: $((16#62)) => 98 (note the 16#)
from binary to decimal: $((2#01110011)) => 115 (note the 2#)
Note: the expression $(()) will execute a command named according to what it returns, so echo it to avoid the prompt complaining that it knows no command named 98 or 115

picoplayer@challenge:~$ $((16#62))
98: command not found
picoplayer@challenge:~$ echo $((16#62))
98
Enter fullscreen mode Exit fullscreen mode

Convert ASCII hex value into the actual characters

note: also works with 0x prefix, ie: 0x62 0x64 0x61 0x36 0x38 0x66 0x37 0x35

$ echo "62 64 61 36 38 66 37 35" | xxd -p -r
bda68f75
Enter fullscreen mode Exit fullscreen mode

Convert to text from [binary|octal|hex]

$ x="01100011 01101111 01101101 01110000 01110101 01110100 01100101 01110010"
$ for a in $x; do printf "%x" $((2#$a)); done | xxd -r -p && echo " "
computer
$ x="160 145 141 162"
$ for a in $x; do printf "%x" $((8#$a)); done | xxd -r -p && echo " "
pear
$ x="66616c636f6e"
$ for a in $x; do printf "%x" $((16#$a)); done | xxd -r -p && echo " "
falcon
$
Enter fullscreen mode Exit fullscreen mode

Convert character into decimal ASCII value

$ printf "%d\n" "'a"
97
Enter fullscreen mode Exit fullscreen mode

Convert decimal ASCII value into a character

$ printf "%x" 97 | xxd -r -p
a
Enter fullscreen mode Exit fullscreen mode

Convert decimal ASCII value into hex value

$ printf "%x" 97
61
Enter fullscreen mode Exit fullscreen mode

List sudo available commands: sudo -l

Bypassing permissions

As can be seen in the output below, the challenge directory is not accessible since no permissions have been set (0 d--------- 1 root root 27 Aug 4 21:34 challenge).

picoplayer@challenge:~$ cd /
picoplayer@challenge:/$ ls -ls

total 0
0 lrwxrwxrwx    1 root   root      7 Mar  8  2023 bin -> usr/bin
0 drwxr-xr-x    2 root   root      6 Apr 15  2020 boot
0 d---------    1 root   root     27 Aug  4 21:34 challenge
0 drwxr-xr-x    5 root   root    340 Dec 13 17:55 dev
0 drwxr-xr-x    1 root   root     66 Dec 13 17:55 etc
0 drwxr-xr-x    1 root   root     24 Aug  4 21:32 home
0 lrwxrwxrwx    1 root   root      7 Mar  8  2023 lib -> usr/lib
0 lrwxrwxrwx    1 root   root      9 Mar  8  2023 lib32 -> usr/lib32
0 lrwxrwxrwx    1 root   root      9 Mar  8  2023 lib64 -> usr/lib64
0 lrwxrwxrwx    1 root   root     10 Mar  8  2023 libx32 -> usr/libx32
0 drwxr-xr-x    2 root   root      6 Mar  8  2023 media
0 drwxr-xr-x    2 root   root      6 Mar  8  2023 mnt
0 drwxr-xr-x    2 root   root      6 Mar  8  2023 opt
0 dr-xr-xr-x 2501 nobody nogroup   0 Dec 13 17:55 proc
0 drwx------    1 root   root     23 Aug  4 21:34 root
0 drwxr-xr-x    1 root   root     54 Dec 13 17:56 run
0 lrwxrwxrwx    1 root   root      8 Mar  8  2023 sbin -> usr/sbin
0 drwxr-xr-x    2 root   root      6 Mar  8  2023 srv
0 dr-xr-xr-x   13 nobody nogroup   0 Dec 13 17:55 sys
0 drwxrwxrwt    1 root   root      6 Aug  4 21:34 tmp
0 drwxr-xr-x    1 root   root     18 Mar  8  2023 usr
0 drwxr-xr-x    1 root   root     17 Mar  8  2023 var
Enter fullscreen mode Exit fullscreen mode

So the idea, is to use vi to access the contents of the challenge directory anyway, because as seen in the last line of the following output, vi can be used with sudo

picoplayer@challenge:/$ sudo -l
Matching Defaults entries for picoplayer on challenge:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User picoplayer may run the following commands on challenge:
    (ALL) /usr/bin/vi
Enter fullscreen mode Exit fullscreen mode

So, finally:

  1. type sudo vi,
  2. type :! ls -ls /challenge/
  3. read the following output
picoplayer@challenge:/$ sudo vi
[sudo] password for picoplayer: 

4 ---------- 1 root root 98 Aug  4 21:34 metadata.json
Enter fullscreen mode Exit fullscreen mode

I now even can print the contents of that file. So, still from vi, type :! cat /challenge/metadata.json

Convert string to MD5 hash

# to avoid the trailing newline added by the shell: '%s'
printf '%s' "money" | md5sum
Enter fullscreen mode Exit fullscreen mode

Determining the contents of non-text files, like a binary file for example.

strings -a -t x [path-to-binary-file-for-example]
Enter fullscreen mode Exit fullscreen mode

Create a binary (executable) file from a bash script (install shc)

# This will create 2 files, and the binary/exec one is 'add.sh.x'
shc -f add.sh
Enter fullscreen mode Exit fullscreen mode

View contents of binary file

# bvi, bview - visual editor for binary files
bvi ./add.sh.x
Enter fullscreen mode Exit fullscreen mode

Is package installed?

dpkg -s gimp
Enter fullscreen mode Exit fullscreen mode

Update all packages at once

Disclaimer: this command should probably never be used in an actual production server ;)

sudo apt-get install -f
Enter fullscreen mode Exit fullscreen mode

Free port

kill $(lsof -t -i:3000)
Enter fullscreen mode Exit fullscreen mode

LAN Access for WSL localhost

These are Powershell (run as admin) commands. (source1 | source2)

netsh advfirewall firewall add rule name="Allowing connections to WSL2 servers" dir=in action=allow protocol=TCP localport=3000

netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=[plug here the result of `wsl hostname -I`]
Enter fullscreen mode Exit fullscreen mode
To show added rule & proxy
netsh advfirewall firewall show rule name="Allowing connections to WSL2 servers"

netsh interface portproxy show v4tov4
Enter fullscreen mode Exit fullscreen mode

Install a package (.deb)

sudo dpkg -i /path/to/deb/file
Enter fullscreen mode Exit fullscreen mode

create a tree of directories in one command

mkdir -pv dir1/dir2/dir3/dir4/dir5
Enter fullscreen mode Exit fullscreen mode

The above commands will create directory(s) recursively inside a non-existent directory(s). You can use the 'tree' command to view the directory structure.

tree dir1/
Enter fullscreen mode Exit fullscreen mode
dir1/
└── dir2
 └── dir3
 └── dir4
 └── dir5
4 directories, 0 files
Enter fullscreen mode Exit fullscreen mode

Find files recursively in all subdirectories and list them by DESC file size

ls -lhR | grep -E "\.(jpg|zip)$" | awk '{print $5, $9}' | sort -hr
Enter fullscreen mode Exit fullscreen mode

Or,

find . -type f -exec du -h {} + | grep -E "\./.*\.(jpg|zip)" | sort -hr
Enter fullscreen mode Exit fullscreen mode

Find files by name in a directory tree

This search is case insensitive (-ipath) and recursively traverses the all subdirectories.
The path

find -ipath */ubeR-secret.txt
Enter fullscreen mode Exit fullscreen mode

Output:

./adequate_books/more_books/.secret/deeper_secrets/deepest_secrets/uber-secret.txt
Enter fullscreen mode Exit fullscreen mode

Using command output as the parameter of another command

Just place it between $([command]).
I should call that this works because I'm expecting a single result in the output. Unexpected results might happen if cat (or the encapsulating command) was fed a multiline result as parameters...

cat $(find -ipath */ubeR-secret.txt)
Enter fullscreen mode Exit fullscreen mode

List files that were created between 2 dates

find . -type f -newermt "2012-01-01" \! -newermt "2020-09-01"
Enter fullscreen mode Exit fullscreen mode

Search for file contents in a directory tree recursively

grep -r "pico"
Enter fullscreen mode Exit fullscreen mode

Output:

big-zip-files/folder_pmbymkjcya/folder_cawigcwvgv/folder_ltdayfmktr/folder_fnpfclfyee/whzxrpivpqld.txt:information on the record will last a billion years. Genes and brains and books encode picoCTF{gr3p_15_m4g1c_ef8790dc}

make script file executable

chmod +x [filename]
Enter fullscreen mode Exit fullscreen mode

Get local IP address

hostname -I | awk '{print $1}'
Enter fullscreen mode Exit fullscreen mode

Open current terminal location into file explorer

xdg-open file
Enter fullscreen mode Exit fullscreen mode

Download file to current directory, using the name and extension found in the URL

wget https://raw.githubusercontent.com/ariya/phantomjs/master/examples/printenv.js
Enter fullscreen mode Exit fullscreen mode

Create a symbolic link

ln -s /path/to/original /path/to/symlink
Enter fullscreen mode Exit fullscreen mode

Saving as unix file from VIM

:set fileformat=unix
Enter fullscreen mode Exit fullscreen mode

Find what PID is locking a port

netstat -tulpn | grep <port>
Enter fullscreen mode Exit fullscreen mode

Force kill a PID

kill -9 <PID>
Enter fullscreen mode Exit fullscreen mode

Find out what process is locking a file

fuser <file_path>
Enter fullscreen mode Exit fullscreen mode

Avoid being prompted for private key when using SSH or scp to connect to hosts with your public key.

 eval `ssh-agent`
Enter fullscreen mode Exit fullscreen mode
 ssh-add
Enter fullscreen mode Exit fullscreen mode

Command to kill a UI/Desktop Application

xkill
Enter fullscreen mode Exit fullscreen mode

Terminator preferences profile (~/.config/terminator/config)

[global_config]
[keybindings]
[profiles]
  [[default]]
    use_system_font = False
    background_image = None
    background_darkness = 0.91
    active_encodings = ANSI_X3.4-1968, UTF-8, ISO-8859-1
    foreground_color = "#839496"
    font = Ubuntu Mono 14
    background_color = "#002b36"
    scrollback_infinite = True
[layouts]
  [[default]]
    [[[child1]]]
      type = Terminal
      parent = window0
    [[[window0]]]
      type = Window
      parent = ""
[plugins]
Enter fullscreen mode Exit fullscreen mode

My custom bash prompt

########### current_dir(current_git_branch) ####################
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"

PS1="$GREEN\W$YELLOW\$(parse_git_branch)$NO_COLOR "
####################
Enter fullscreen mode Exit fullscreen mode

Cancel reverse-i-search

Ctrl+G
Enter fullscreen mode Exit fullscreen mode

Activate reverse-i-search

Ctrl+R
Enter fullscreen mode Exit fullscreen mode

Reload bash prompt after .bashrc modification

source .bashrc
Enter fullscreen mode Exit fullscreen mode

Google chrome path

which google-chrome
Enter fullscreen mode Exit fullscreen mode

Check ubuntu version

lsb_release -a
Enter fullscreen mode Exit fullscreen mode

Make umount Work with sshfs

fusermount -u /path/to/mounted-dir
Enter fullscreen mode Exit fullscreen mode

Get a report of the used and available space on all mounted drives

df -h
Enter fullscreen mode Exit fullscreen mode

Get a report of the size of data by folder

sudo du -sk /home/* > tmp3.txt
Enter fullscreen mode Exit fullscreen mode

Sort report

sort -n tmp.txt
Enter fullscreen mode Exit fullscreen mode

Get a list of processes, ssh in this case

ps -ax | grep ssh
Enter fullscreen mode Exit fullscreen mode

Print number of .tpl files (from current directory and in all nested directories..)

find -name '*.tpl' | wc -l
Enter fullscreen mode Exit fullscreen mode

Print a list of .tpl files (from current directory and in all nested directories..)

find -name '*.tpl'
Enter fullscreen mode Exit fullscreen mode

Look for the number of files (.tpl in this case) per directory

 for D in *; do echo $D; find $D -type f -name '*.tpl'| wc -l; done
Enter fullscreen mode Exit fullscreen mode

Top comments (0)