Bash Cheat Sheet - Linux Commands for Ubuntu, Mint, and Debian

Some tools command line params

Page content

Not a very comprehensive list, just some useful to me ones

bash and shell anniversary!

For more developer tools and workflows, see Developer Tools: The Complete Guide to Modern Development Workflows.

If you need an interactive prompt pattern in scripts, see Pause scripts with Press any key in Bash CMD PowerShell and macOS.

How to Check Linux Ubuntu Version

For more details on checking Ubuntu version, see Check linux ubuntu version.

Method 1

lsb_release -a

would produce something like

No LSB modules are available.
Distributor ID:	Linuxmint
Description:	Linux Mint 22.1
Release:	22.1
Codename:	xia

Method 2

cat /etc/os-release

would produce something like

NAME="Linux Mint"
VERSION="22.1 (Xia)"
ID=linuxmint
ID_LIKE="ubuntu debian"
PRETTY_NAME="Linux Mint 22.1"
VERSION_ID="22.1"
HOME_URL="https://www.linuxmint.com/"
SUPPORT_URL="https://forums.linuxmint.com/"
BUG_REPORT_URL="http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/"
PRIVACY_POLICY_URL="https://www.linuxmint.com/"
VERSION_CODENAME=xia
UBUNTU_CODENAME=noble

Bash Archiving and Unarchiving with Tar

Compressing

tar -zcvf archive-name.tgz directory-name

Decompressing

tar -zxvf archive-name.tgz

Bash Remote Server Access with SSH and SCP

Push user identity to remote server

ssh-copy-id user@10.0.0.225

So it will allow to login without password next time like

ssh user@10.0.0.225

Upload file

scp ~/file.ext user@host-ip:/home/user/file.ext

Download folder with nested stuff recursively

scp -r user@host-ip:/home/user/dir ~/dir

Bash Folders and File Management

Check for existence

# create a folder if it doesn't exist, with all intermediate folders
[ -d $repdir ] || mkdir -p $repdir

# or
if [ -d $fname ]; then
  echo "File doesn't exist: $fname"
  return
fi

Create folder for particular user

sudo mkdir dir1
sudo chown specific_user dir1
sudo chown :specific_group dir1

Iterate over files in the folder

# this one converts all jpg files in some folder to fits
for f in some-folder/*.jpg
do
  convert "$f" "${f/.jpg/.fits}"
done

Merge all files into one

cat ./* > merged.txt

Adding Commands to Crontab

(crontab -l 2>/dev/null | \
  grep -v control-marker-1; \
  echo '*/15 * * * * /bin/bash /home/user/stest/stest.sh /home/user/stest >> /home/user/stest/stest.log 2>&1 #control-marker-1') | \
  crontab -

here:

  • */15 - run every 15 mins
  • control-marker-1 - is identifier of this command in the cron config to update it next time with the same script
  • /bin/bash - command to execute
  • /home/user/stest/stest.sh - bash param - the bash will run this script
  • /home/user/stest - script param - to be accessed by $1
  • /home/user/stest/stest.log - log file with console output of stest.sh

Check

grep /home/user/stest/stest.sh /var/log/syslog
crontab -e

Watching Log Files in Bash

Watch logfile live

sudo tail -f /var/log/megalog.log

Getting HTTP Status Code with curl in Bash

response=$(curl --write-out '%{http_code}' --silent --output /dev/null servername)
  
# or

url='http://localhost:8080/'
status=$(curl --head --location --connect-timeout 5 --write-out %{http_code} --silent --output /dev/null ${url})

[[ $status == 500 ]] || [[ $status == 000 ]] && echo restarting ${url} # do start/restart logic

Keeping SSH Command Running After Logoff

https://stackoverflow.com/questions/954302/how-to-make-a-program-continue-to-run-after-log-out-from-ssh

Assuming that you have a program running in the foreground, press

  • ctrl-Z, then:

[1]+ Stopped myprogram

  • disown -h %1
  • bg 1

[1]+ myprogram &

  • logout

Generating JSON in Bash with jo

Install jo

sudo apt-get install jo
a="123"
b="456"
jo "model=a" "prompt=b" "stream=false"

would output

{"model":"a", "prompt":"b", "stream":false}

A bit more complex:

jo -p name=Jane point[]=1 point[]=2 geo[lat]=10 geo[lon]=20
{
   "name": "Jane",
   "point": [
      1,
      2
   ],
   "geo": {
      "lat": 10,
      "lon": 20
   }
}

Formatting JSON in Bash with jq

Use

| jq .

To format generated above json:

a="123"
b="456"
jo "model=$a" "prompt=$b" "stream=false" | jq .

The formatted json will be:

{
  "model": 123,
  "prompt": 456,
  "stream": false
}

Parsing JSON and Extracting Field Values in Bash

Install jq first

sudo apt-get install jq

Use

  | jq -r '.fieldName'

Like parsing output of a call to Ollama:

curl http://localhost:11434/api/generate -d '{
  "model": "llama3",
  "prompt": "Why is the sky blue?",
  "stream": false
}'  | jq -r '.response'

Counting Words in a File with Bash

Word count:

wc -w filename.txt

will return something like

261 filename.txt

If you want just an integer, you can cut out first word, which is a number

words=`wc -w filename.txt | cut -f1 -d' '`
echo "$words words"

Or use wc like:

words=`wc -w < filename.txt`
echo "$words words"

Checking Directory Disk Usage

du ~/dirname

Getting the Script Directory Path in Bash

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

Measuring Script Execution Time in Bash

One option is to use time function

time your_script.sh

Another way is just a bit more complicated:

start=`date +%s`

# very important code
# goes here

end=`date +%s`

runtime=$((end-start))

Comparing Two Files with VS Code

code -d <file 1> <file 2>

Checking Available Packages in Ubuntu Repository

sudo apt-cache policy <packageName>

Subscribe

Get new posts on AI systems, Infrastructure, and AI engineering.