Linux Basics

April 1, 2020

These are some basics linux terminal commands which are of great use for developers and linux lovers.

Linux Generic Commands

$ cd : Go to home directory
$ cd <directory path> : Go to a paricular directory
$ cd .. : Go one directory back
$ cp <location of file to copy> <location to copy to> :Copy a file
$ mv <location of file to move> <location to move to> : Moving a file
$ mkdir <directory name> : Create a directory
$ rm <file name> : Delete a file
$ rm -r <directory path> : Delete a directory
$ ls <directory path> : List of files in the directory
$ find <location> -name <file name> : Find a file
$ sudo du -sh <file location> : Get the file size

Ubuntu Specific Commands

$ sudo apt update : Updates the list of available packages and their versions.
$ sudo apt upgrade : Installs newer versions of updated packages.
$ sudo apt clean : cleans packages and install script in /var/cache/apt/archives/
$ sudo apt autoclean : Cleans obsolete deb-packages
$ sudo apt autoremove : Removes the packages that are no longer required.
$ sudo apt install <package-name> : Install a package
$ sudo apt remove <package-name> : Uninstall a package
$ sudo apt purge <package-name> : Uninstall and remove saved configurations of a package.
$ sudo apt list --installed : List of installed packages
$ dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n |tail -n 10 : List of top ten installed packages ordered by space occupied.

Linux Window Managers

Screen Commands

$ screen -S <name> : Start a screen with a name
hold(ctrl + a) and then d : Detach that screen
$ screen -r <name> : Restore a detached screen again
$ screen -r -d <name> : Restore a attached screen again
hold(ctrl + a) and then K : Kill a particular screen

Tmux commands

In Tmux, Sessions- - > windows - - > panes order is there.

Generic Commands
tmux ls - -> Session list
tmux new -s -session_name -n window_name - -> New session
tmux new -d -s -session_name - -> New detached session
tmux attach -t session_name - -> Attach session

Windows Commands
(ctrl+b) and then c - - > Create a window
(ctrl+b) and then , - - > Rename current window - - >
(ctrl+b) and w - - > Choose window - ->
(ctrl+b) and then type window number - - > Choose window - ->
(ctrl+b) and & - - > Close window - ->

Panes Commands
(ctrl+b) and then % - - > Split vertically
(ctrl+b) and then “ - - > Split horizontally
(ctrl+b) and then x - - > Kill a pane
(ctrl+b) and then : and then resize-pane -U/D/L/R 10 - - > Resize panes

You can see title per pane in a status bar with:
$ t - - > For current window
$ tmux set -g pane-border-status top - - > For all windows

Disable status bar with:
$ tmux set pane-border-status off - - > For current window
$ tmux set -g pane-border-status off - - > For all windows

Change title of pane
$ tmux select-pane -t {pane} -T {title}
Examples: $ tmux select-pane -T title1 - - > Change title of current pane
$ tmux select-pane -t 1 -T title2 - - > Change title of pane 1 in current window
$ tmux select-pane -t 2.1 -T title3 - - > Change title of pane 1 in window 2

Trigger command from anywhere into tmux sessions
$ tmux send-keys -t session:window.pane command

Miscellaneous Commands

Kill a program

$ pidof <program-name> : Get pid of a program
$ sudo lsof -i:<port> : Get PID of program using a port
$ kill <pid> : Kill the program

Create a user

useradd user_name -m : create a user with home directory
useradd user_name -m -d <path> : create a user with custom home directory
passwd user_name : Set password for that user (optional)
su user_name : switch to that user
mkdir .ssh : create a .ssh directory
cd .ssh && nano authorized_keys : paste the public key

Give sudo access

sudo visudo : Open sudoers file in edit mode.
username ALL=(ALL) NOPASSWD: ALL : Add this line.

Set permission for a file

(u=user, o=others, g=group)
$ chmod u=rwx filename (file owner (user) can read write and execute)
$ chmod go=rwx filename ( group and others can read write and execute)
$ chmod o-r filename ( others can’t read)
$ chmod o+r filename ( others can read)

Miscellaneous

$ sudo -s : Become sudo user
$ sudo -i : Become sudo user
$ su user_name : Swith user
sudo chown -R user_name directory_path : Change owner of file/path $ ll <file name>: Read a file with permission
$ fuser -k /dev/ttyACM0 : Kill serial port if busy
$ jupyter lab --ip 0.0.0.0 --port <port> : Access jupyter lab remotely.
$ sudo chmod a+rw /dev/ttyACM0 : Grant permissions to read/write to the serial port