Linux Bash Cheat Sheet
Some tools command line params
Not a very comprehensive list, just some useful to me ones
Archiving & unarchiving
Compressing
tar -zcvf archive-name.tgz directory-name
Decompressing
tar -zxvf archive-name.tgz
Remote servers
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
Folders and files
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
Add command execution 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
Logs
Watch logfile live
sudo tail -f /var/log/megalog.log
Status code of curl
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
Leave ssh command running after logoff
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
Generate JSON
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
}
}
Format JSON
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
}
Parse JSON and return value of some field
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'
Count words in file
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"
Check how much space the directory takes on HDD
du ~/dirname
Get folder name where running script is located
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
Measure exexution time of the script
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))
Compare two files with vs code
code -d <file 1> <file 2>
Check available pachages in ubuntu repository
sudo apt-cache policy <packageName>