Link Search Menu Expand Document

Bash shell command line commands and tools

*nix

code

Here you will find an assortment of code snippets for a variety of terminal utilities and applications.

apt package manager

Apt is a software package manager.

  • Listing installed apps
sudo apt list --installed
  • Installing a new package
sudo apt update
sudo apt install PACKAGENAME
  • Searching for packages (packages may have different prefixes or unique naming conventions between linux distributions)
sudo apt search SEARCHTERM

Optionally, if there are too many results, you may pass the search results, using the | operator to more or less

sudo apt search SEARCHTERM | more
sudo apt search SEARCHTERM | less

grep

One applied use case of the powerful grep utility is recursively searching files for a given parameter or value.

grep -r "parameter=1000" .

The -r flag enables recursive searching in the location . (current directory).

pv

If you are copying a large file using cp, you will not have a progress indicator. pv performs the same function while echoing the output1.

pv input.file > output.file

rsync

Basic recursive file copying between a source and destination using the rsync utility.

rsync -avzh /source /destination

The option flags -avzh represent -a recursive archival copying, -v verbose output, -z compression and -h human readable STDOUT.

sshfs

Sshfs allows the local mounting of remote file systems. Mounts can be manual (session-based) or permanent (via fstab system entries)

  • Manual (session-based) - keyless (password) authentication
sshfs -o allow_other,default_permissions user@000.000.000.000:/remote/mount/ /local/mount/
  • Manual (session-based) - key-based (paswordless) authentication
sshfs -o IdentityKey=/path/to/key.pem user@000.000.000.000:/remote/mount/ /local/mount/
  • Unmounting the share
umount /local/mount

Note: sudo may be required depending on your chosen local mounting point. However, this will then require you to have sudo read/write privileges for full access to the share

tar/gzip

Folder archival and compression in linux is achieved using a combination of two common tools2.

  • Archive and compress
tar -zcvf archive.tar.gz source/ 
  • Decompress and expand
tar -zxvf archive.tar.gz

tmux

This tool is a terminal multiplexer allowing a user to run multiple parallel command line terminal prompts while allowing them to run in the background. Benefits include running time-consuming tasks on remote servers while being able to exit the ssh session.

  • Create a new session
tmux new-session -s SessionName
  • Detach from a running session CTRL + b followed by d
  • Attach to an existing session
tmux attach-session -t SessionName
  • List existing sessions
tmux ls
  • Remove session
tmux kill-session -t SessionName

For documentation material on the Bash cli, read the introduction to Bash page.

  1. https://askubuntu.com/questions/17275/how-to-show-the-transfer-progress-and-speed-when-copying-files-with-cp 

  2. https://unix.stackexchange.com/questions/93139/can-i-zip-an-entire-folder-using-gzip