Other Useful Linux Commands
A collection of other bash and Linux commands ... or solutions
There's obviously no point in detailing all of the Linux/Unix/Bash commands available. Here are a few commands or solutions, I wanted to remember.Get motherboard or DIMM info:
dmidecode reads information from the DMI (desktop management interface) table which is closely related to the SMBIOS (system management BIOS). Need sudo to run:
dmidecode -t 4 # for CPU info
dmidecode -t 2 # for motherboard info
dmidecode -t memory # for all memory
dmidecode -t 17 # for sodimm information
dmidecode -t 16 # for motherboard info on memory
There's also lshw, but it wasn't installed on my system.
Related commands: lspci, lsusb, lscpu, lsscsi, lsblk
(Other ls* commands of interest: lsmem, lslocks, lsns, lsipc, lslogins)
Weather on the command line:
curl wttr.in
curl wttr.in/Denver
While loop failing:
I had a problem where I wanted to check ssh on a number of hosts. Simple - cat the file, pipe into while, use ssh with timeout to do the check like this:
cat host_list.txt | while read h; do timeout 3 ssh $h; done
It didn't work. It just hung on the ssh command until timeout cut the command. ssh seemed to be grabbing standard input (and maybe stdout) and interfering with the while loops input. Switching to a for loop helped with the input. Then ssh didn't seem to be the best choice nor did telnet 22. Netcat in a for loop worked best:
nc -v $host 22
Problem solved and the hosts with ssh running were found....
ssh starting remote processes:
Here's another one that might be easy or not... With ssh, you can run a remote command:
ssh user@remote.host "ls -l"
What if you want to start a remote process and want it to keep running - something like this:
ssh user@remote.host "nohup java example.jar &"
The problem with the above is that it often won't work - the remote service won't be running. This mostly has to do with terminal control. Despite nohup saying that it is redirecting output to nohup.out, there's still a problem. Two easy solutions:
ssh user@remote.host "nohup java example.jar > ./output_file.out 2>&1 &"
This grabs both standard out and standard error and puts them into output_file.out. Of course, another option is to create a systemd (or init.d) script and use that.
inotify-tools:
Specifically, inotifywait which will watch file system objects for events that you specify.
inotifywait -r -e access,modify /var/log
This will watch for access or modification updates on files in /var/log.
Combine inotifywait with a while loop as in
while inotifywait -e access /var/log
do
some shell commands here
done
The only downside to inotifywait is that inotify-tools often needs to be installed (yum/dnf install or apt install).
History Time and Date
To have more info about when commands were run, set HISTTIMEFORMAT
HISTTIMEFORMAT="%d/%m/%y %T "
JQ json query
Query json files using jq. More complex example where top level json is a list of sets of fields with lists themselves which might be optional and require a select to handle:
jq '.[] | "\(.name), \(.projectKey), \(.team.name), \(.team.id), " + ((select(".source[].orgName") | .source[].orgName)//null)' pretty-c1.json
jq . json-in-one-line.json > pretty-c1.json
Comments
Post a Comment