This is an old revision of the document!
Table of Contents
dummy
tasks
what is my OS?
awk -F= '$1=="ID" {print $2}' /etc/os-release
Sample run:
% cat /etc/os-release PRETTY_NAME="Debian GNU/Linux 12 (bookworm)" NAME="Debian GNU/Linux" VERSION_ID="12" VERSION="12 (bookworm)" VERSION_CODENAME=bookworm ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/"
% awk -F= '$1=="ID" {print $2}' /etc/os-release debian
Ref:
- I came across it in https://github.com/trimclain/.dotfiles/blob/main/Makefile
last reboot times
last reboot --time-format full
Sample run
% last reboot --time-format full reboot system boot 5.10.0-17-amd64 Tue Dec 6 08:18:33 2022 still running reboot system boot 5.10.0-17-amd64 Tue Dec 6 08:11:34 2022 - Tue Dec 6 08:17:36 2022 (00:06) reboot system boot 5.10.0-17-amd64 Tue Dec 6 07:41:53 2022 - Tue Dec 6 08:11:01 2022 (00:29) reboot system boot 5.10.0-17-amd64 Sat Dec 3 13:18:10 2022 - Tue Dec 6 08:11:01 2022 (2+18:52) reboot system boot 5.10.0-17-amd64 Sat Dec 3 13:04:06 2022 - Tue Dec 6 08:11:01 2022 (2+19:06) reboot system boot 5.10.0-17-amd64 Wed Nov 16 11:18:41 2022 - Tue Dec 6 08:11:01 2022 (19+20:52) reboot system boot 5.10.0-17-amd64 Mon Sep 5 21:26:07 2022 - Wed Nov 16 11:18:01 2022 (71+14:51) reboot system boot 5.10.0-16-amd64 Mon Aug 15 05:27:05 2022 - Mon Sep 5 19:36:09 2022 (21+14:09) reboot system boot 5.10.0-16-amd64 Mon Aug 1 17:56:22 2022 - Mon Aug 15 05:26:20 2022 (13+11:29) ...
ls and mv
Sample command
ls -rt *.txt | tail -n5 | tr '\n' '\0' | xargs -0 -i% mv % x1
Notes:
- Works even if there are spaces in the filenames. Compare this with
mv `ls -rt *.txt | tail -n5` x1
which will not work if there are spaces in the filenames.
- Does not work if the filenames contain newline characters.
Ref:- https://stackoverflow.com/a/937965/6305733
Related commands
ls -rt *.txt | tail -n5 | tr '\n' '\0' | xargs -0 -i% md5sum %
remove large directories
Use rsync to delete large directories.
mkdir empty_dir rsync -a --delete empty_dir/ yourdirectory/
As per https://unix.stackexchange.com/questions/37329/efficiently-delete-large-directory-containing-thousands-of-files, it is more efficient than running “rm -rf” or some combination of find + “rm -rf”.
stackoverflow answers I came across
- remove leading and trailing spaces - https://unix.stackexchange.com/a/102021/198064
- search for a string and count the number of characters per line - https://unix.stackexchange.com/questions/400650/counting-the-characters-of-each-line-with-wc
get file modified time in shell script
date +%Y%m%d_%H%M%S -r $input_file
Used it in | https://github.com/KamarajuKusumanchi/rutils/blob/master/python3/black_on_selected.sh
Found it in | https://stackoverflow.com/questions/16391208/print-a-files-last-modified-date-in-bash
tput: unknown terminal "xterm-256color"
When I moved my miniconda3 installation from /home/rajulocal/miniconda3 to /opt/rajulocal/miniconda3, I started getting
tput: unknown terminal "xterm-256color"
To fix it, I did
conda install --force-reinstall ncurses
It turns out that the –fore-reinstall option was important since simply doing
conda install ncurses
was not installing ncurses as it was already uptodate.
Ref:- https://stackoverflow.com/questions/32798940/tput-unknown-terminal-xterm-256color
Why is the shell called as such?
From https://www.linuxdoc.org/HOWTO/Unix-and-Internet-Fundamentals-HOWTO/running-programs.html
The shell is Unix's interpreter for the commands you type in; it's called a shell because it wraps around and hides the operating system kernel. It's an important feature of Unix that the shell and kernel are separate programs communicating through a small set of system calls. This makes it possible for there to be multiple shells, suiting different tastes in interfaces.