Linux - 101

Freshman in the Linux world

When you start your Linux journey coming from another OS, which is mostly controlled by the GUI (Windows, OSX), you might have a lot of questions.

Although the modern Linux distros provide extremely rich GUIs, the terminal remains the most powerful part of Linux.

Here is some know-how, you might need.

NOTE: All examples below are only tested in Debian-like distros.


Aliases

Edit ~/.bashrc and uncomment the line for the ll alias
Or better extend it to:

alias ll='ls -lha'

Additionally, you might want to add the following function into  ~/.bash_aliases:

cdl() { cd "$1" && ll; }

This way, you can change dir and list content in one:

nikolay@blog:/ $ cdl /var
total 101M
drwxr-xr-x 11 root root  4.0K Aug 20 12:47 .
drwxr-xr-x 21 root root  4.0K Aug 20 12:47 ..
drwxr-xr-x  2 root root  4.0K Nov  7 06:25 backups
drwxr-xr-x  9 root root  4.0K Oct 25 11:08 cache
drwxr-xr-x 29 root root  4.0K Nov  2 18:56 lib
drwxrwsr-x  2 root staff 4.0K May 10 22:59 local
lrwxrwxrwx  1 root root     9 Aug 20 12:26 lock -> /run/lock
drwxr-xr-x  7 root root  4.0K Nov  7 00:00 log
drwxrwsr-x  2 root mail  4.0K Aug 20 12:26 mail
drwxr-xr-x  2 root root  4.0K Aug 20 12:26 opt
lrwxrwxrwx  1 root root     4 Aug 20 12:26 run -> /run
drwxr-xr-x  5 root root  4.0K Oct 25 11:08 spool
-rw-------  1 root root  100M Aug 20 12:47 swap
drwxrwxrwt  4 root root  4.0K Nov  7 00:00 tmp
nikolay@blog:/var $ 

Sudo

Asking for password on each sudo can be annoying. This is how to disable it:

$ sudo visudo

add this line:

username     ALL=(ALL) NOPASSWD:ALL

where "username" is your login.

System resources

The following commands return information about the distro you are using:

$ lsb_release -a
$ uname -a

These show information about the system resources (RAM, disk usage):

$ free -h
$ df -h
$ du -h /var

If you are interested in CPU usage, by process:

$ top

List file disks and partitions:

$ lsblk
$ sudo fdisk -l

Check RAM type:

$ sudo dmidecode --type 17

Check battery:

$ upower -d

Check interface speed:

$ ethtool enp3s0 | grep Speed
$ ethtool eth0 | grep Speed

Administration

Create a new user:

$ sudo adduser user

Change the password of a user:

$ sudo passwd user

Add a user to groups:

$ sudo usermod -aG group1,group2 user

Show the user's groups:

$ groups user

Change hostname:

$ hostnamectl set-hostname myhost

Change time and time zone:

$ timedatectl set-time ...
$ timedatectl set-timezone Europe/Sofia

Sharing files. First add a samba user:

$ sudo smbpasswd -a nikolay

Then configure the share in /etc/samba/smb.conf:

[MyShare]
comment = Some comment
path = /home/nikolay/MyShare
read only = no
browsable = yes
force user = nikolay
valid users = nikolay

Common commands

Create symbolic link:

$ ln -s target.sh link.sh

Add and extract to/from archive:

$ tar -xzvf archive.tar.gz -C /dest
$ tar -cf archive.tar /src

Mount / unmount:

$ sudo mount /dev/sda1 /media/mymount
$ sudo umount /media/mymount

Change all dirs recursive to mod 755:

$ find ~/Documents -type d -exec chmod 755 {} \;

Change all files recursively to mod 644:

$ find ~/Documents -type f -exec chmod 644 {} \;

Advanced commands

View the environment variables of a process:

$ xargs -n 1 -0 < /proc/22/environ

The command above xargs is pretty interesting one and probably deserves a post of its own.

Change the encoding of a text file:

$ iconv -f windows-1251 cyrillic.txt -o cyrillic_new.txt

Echo in the console with color 😀

$ echo -e "Default \e[31mRed"

The number between [ and m has the following meaning:
1-9, 2* text styles
3*, 9* colors
4*, 10* background colors

Services

To register, unregister, start and stop services:

$ systemctl status myapp.service
$ sudo systemctl enable myapp.service
$ sudo systemctl daemon-reload
$ sudo systemctl start myapp.service
$ sudo systemctl stop myapp.service

or

$ service myapp status
$ sudo update-rc.d myapp defaults
$ sudo service myapp start
$ sudo service myapp stop
$ sudo update-rc.d -f myapp remove

Some more info. The startup scripts for services are normally placed in /etc/init.d/
Symbolic links to these scripts are created in /etc/rc<LEVEL>.d/
More about this maybe in another post.

No wake on mouse and BT action

$ lsusb -t

Find Bus and Port of "Class=Human Interface Device" and "Class=Wireless, Driver=btusb".
If the Bus is 1 and the Ports are 5 and 6:

$ cat /sys/bus/usb/devices/1-5/power/wakeup
$ sudo echo disabled > /sys/bus/usb/devices/1-5/power/wakeup
$ cat /sys/bus/usb/devices/1-6/power/wakeup
$ sudo echo disabled > /sys/bus/usb/devices/1-6/power/wakeup

To view all:

$ cat /proc/acpi/wakeup

Disable Bluetooth visibility

$ sudo hciconfig -a

(get the device number, should be 0)
To disable:

$ sudo hciconfig hci0 noscan

To enable:

$ sudo hciconfig hci0 piscan

Workspaces in Linux Mint

To change and get the number of workspaces:

$ gsettings set org.cinnamon number-workspaces 12
$ gsettings get org.cinnamon number-workspaces

OR: Press Ctrl + Alt + Up and click the plus button until they become 12

The number 12 above is not a random one. 
It comes very handy to set the shortcuts to them: Win + F1-F12