Useful Linux CLI commands

Last times during my regular development duties, I got a bit of administrative spice. I have found some new interesting Linux CLI commands and also refreshed a few, that I have already known, but haven’t been using for some time.
I have decided to write this note as a way to memorize them better.

1. Checking last executed commend exit code.


$?

2. Checking if variable is unset or empty string in bash.
Similar switches that returns true if file exists are: -a -e .


 if [ -z $VARIABLE ]; then
     command
 fi

3. Print user main group or uid.


#print user 'username' main group
id -g username
#print user 'username' uid
id -u username

4. Mount Samba share through fstab. Important here is if that after any configuration change umount needs to be done. https://wiki.samba.org/index.php/LinuxCIFS_troubleshooting


# a)
sudo apt-get install cifs-utils
# b) create credentials file in format (chmod 0600)
# `username=shareuser
#  password=sharepassword
#  domain=domain_or_workgroupname`
# c) edit /etc/fstab
# `//example.com/target/path /mount/point cifs credentials=/path/to/credentials/file 0 0`
# d)
sudo systemctl daemon-reload
# e)
sudo mount -a

5. Removing some text from file using sed.


sed "s/^text to remove$//g" /path/to/file

6. Find pid of running command/program


pidof 

7. Create iso file of directory contents.


genisoimage -o ./file.iso -V VOL_NAME -r -J ./directory/path/to/create/from

8. Set system wide proxy settings that will survive restart.


# edit `/etc/environment`:
http_proxy="http://proxy.example.com:8080"
https_proxy="http://proxy.example.com:8080"

9. Running commands in the background.


command &
# check jobs in background
jobs -l
# put into foreground
fg
fg %1 #(number from jobs -l command)
#remove command from current shell control so we can leave
disown
disown %1 #(number from jobs -l command)

10. Pass password from file to ssh command.


sshpass -f "/file/with/password" ssh "your command"

11. Using multiline string in bash command.


`command <<DELIMITER
    some commands in string
DELIMITER`

12. Read in sudo password from STDIN.


echo password | sudo -S 

13. Check last logins to system.

last

14. Add url search domain in Ubuntu (unwrapping short domain names like help/)


# /etc/systemd/resolved.conf
# uncomment and fill 'Domains'



«