sensharma
3/14/2018 - 1:51 PM

bash commands

bash commands

Some resources to incorporate:

Key point of scripting using sudo: sudo breaks into parts, for e.g. in sudo echo blah >> grok.txt the sudo may not apply to grok.txt and if it is a protected file, there may be problems. Best to write scripts without sudo, and then run them with sudo. Applies automatically to the entire script.

Specific functions related:

CommandDescription
... &
eg.
spyder &
returning to command line after launching an application

launches spyder & returns to command line
# blahsingle-line comment in bash script
: '
body
'
multi-line comment in bash script
NOTE: The space between : ' at the beginning of the comment is ESSENTIAL.

Nvidia GPU related

CommandDescription
nvidia-smishows GPU and driver being used etc.
nvidia-smi -l 5shows GPU usage updated every 5s in this case
lspci grep -i nvidiaShows gpu info. lspci gets hardware info, is piped through grep and -i is --ignore-case

Writing to and reading from files

CommandDescription
echo <text> >> <filepath>APPENDS the text as a new line in the file
echo <text> > <filepath>ERASES and ADDS the text as a fresh line in the file. BE CAREFUL with single > vs. double >>
echo 'deb blah blah...' | sudo tee --append /etc/apt/source.listwrite using echo & tee to a file
Especially useful when the destination is root contolled. sudo at the beginning doesn't apply after the pipe
sudo sh -c "echo 'something' >> /etc/privilegedfile"can also be used to write to root privileged files
Note: its like running a bash script from a command line
interesting to check out how expansion of variables within " " and non-expansion within ' ' for echo can be managed with this
cat <filename>displays contents of file in std out
more <filename>displays contents of file in std out as a buffer within std out (any key to quit)
less <filename>displays contents of file in separate buffer within terminal (like man command) (q to quit)

Operating system info related

CommandDescription
lsb_release -ashows all info about version of ubuntu on terminal
lsb_release -cshows codename of ubuntu version on terminal, like xenial for 16.04
lsb_release -rshows release no. of version of ubuntu on terminal, like 16.04
lsb_release -dshows description of version of ubuntu on terminal, like Ubuntu 16.04.3 LTS

Symlinks

CommandDescription
ln -s [path/to/target/directory] [path/to/symlink]creates a symlink to the target directory at the specified path
Note: The symlink name should not exist in advance, i.e. don't create a directory in advance. It will be created
Note: There are no / at the end of the directory names - VERY IMPORTANT
ln -s [path/to/target/directory] [path/to/destination/directory]A symlink to the target directory is created WITHIN the destination directory
Note: The destination directory NEEDS to exist in advance. Needs to be created in advance
Note: There are no / at the end of the directory names - VERY IMPORTANT
Think about it this way:If the 2nd argument is a directory (i.e. an existing one), then symlink goes INSIDE it. If it is not a directory, then a symlink is created there for it.

Environment and env variables related

CommandDescription
printenvdisplays full environment with all env variables
'<ENV_VARIABLE> = env_variable_value'sets the particular env variable (only for the session)
source ~/.bashrcreload ~/.bashrc

Files and directories related

CommandDescription
du -sh <dirpath/name>shows size of directory
du -h <dirpath/name>lists size of contents of directory

Search related

find: helps find files etc

CommandDescription
find . maxdepth 1 -name "*string*" -printsearches current position recursively (folder can be specified for search) where the string is in the name of the file and prints it to stdout. Maxdepth, if not provided means recursively without limit.

grep: finds text in files (ag & rg are faster versions)

Package management related

(Additional install: dctrl-tools)

CommandDescription
sudo apt install vs
sudo apt-get install
apt installs without asking for confirmation. I.e., is equivalent to apt-get -y
sudo gdebi -n <package.deb> vs
sudo dpkg -i <package.deb>
dpkg -i only "registers" the package (in /etc/apt/sources.list.d; where all info about apt package sources & ppas are) apt-get update & apt-get install then needs to be run. Does not pull in external dependencies if required.
gdebi does. Therefore gdebi preferred.
apt show <package>
can be grepped
apt show | grep build
Show package details for apt repo
Note: DOES NOT work with apt-get, just with apt
<- This can show, for example, if build-essential is in the characterisctic, i.e. if it belongs to it.
apt-cache policyView all ppas
apt-add-repository --remove <ppa>
or after installing ppa-purge on apt
sudo ppa-purge <ppa>
Remove ppas
Note: Listing and removing done much more reliably from software-properties gui. Have had problems with both the above
apt list --installed <google\*>Listing all installed packages.
Can include any globbing pattern in < >
dpkg -l | grep -i <google\*>Listing all installed packages.
Even more effective than above
sudo apt-get remove <application>Removes a package binaries, doesn't clean everything
sudo apt-get purge <application> OR
sudo apt-get remove --purge <application>
Removes a package, and everything associated with it except config files in the home directory & dependencies
sudo apt-get autoremoveRemoves all orphaned packages, not required after removing/purging
sudo apt-get --purge autoremove <packagename OR
sudo apt-get purge --autoremove <packagename>
Removes all orphaned packages, not required after removing/purging
grep-dctrl -sPackage . /var/lib/apt/lists/ppa.launchpad.net_<repo>Displays all packages that can be installed from that repo (ppa)
/var/lib/apt/lists/ppa.launchpad.net is where all the ppa info is at
dpkg -L <package>lists all files related to the package
dpkg -S <filename>reverse of the above, provides name of package associated with that file
`apt-cache --no-all-versions show $packagegrep '^Size: '`shows size of an installed package (can be explored for what else apt-cache can show)

Processes/PID related

CommandDescription
ps -elist all processes
ls lists files, ps lists processes, but needs -e flag
pgrep -nf "manage.py runserver"
pgrep -nf "firefox"
To get the PID for a process with a particular string in the CMD that launched the process
ps -e | grep "firefox"Does the same as above, piped through grep
ps -fp <PID>To get the full name of a PID

wget

CommandDescription
wget -r --no-parent <http://site.comdownloads all pages of site
wget -r --no-parent <http://site.com/songs/downloads all pages of site under songs

Users & groups related

CommandDescription
sudo usermod -aG <group> <user>add user to group
sudo gpasswd -a <user> <group>add user to group (using gpasswd)
sudo gpasswd -d <user> <group>delete user from group (using gpasswd; delete much easier with gpasswd than with usermod)
sudo usermod -G "" <user>removes user from all groups except their primary group
compgen -glists all groups
compgen -ulists all users
sudo groupadd <group>adds a group
groups <user>list all groups user is in
getent group <groupname>list all users in group