Skip to content

Instantly share code, notes, and snippets.

@adamwespiser
Last active July 1, 2017 16:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamwespiser/fffa5c7140f1b9561a97 to your computer and use it in GitHub Desktop.
Save adamwespiser/fffa5c7140f1b9561a97 to your computer and use it in GitHub Desktop.
Ubuntu command line into
#!/bin/bash
echo "current dir"
ls ./
echo
echo "Root directory"
ls /
# Summary:
# hide dock
# terminator cmdline over terminal
# add 2x2 workspaces with alt-nav ctrl-alt-move
# Java oracle, not OpenJDK
# Eclipse
# Transmission
# VLC
# Dropbox (config with dropbox start -i, then you should be fine)
# openssh-server
# SAUCES: me, with some help from this website
# all modifications on ~/.bashrc happened in last lines 118+
# I hide your dock, just scroll to the left hand side of the screen, now 2 by 2 screen fit pretty well,
# especially if you use terminator, which will split one big screen into 4
# i added 3 more workspaces,(2x2 grid) just hit 'Alt' + arrow key you want to go
# to move a window to a new workspace 'ctrl'+'alt'+array_key for direction
# upgrade system
sudo apt-get install upgrade
# install (oracle version, not shitty OpenJDK for n00bs) java w/
$ sudo apt-get purge openjdk*
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get install oracle-java7-installer
# configure java
# Add add JAVA_HOME to your start up profile setting (~/.bashrc)
# get java install location
export java_location=$(update-alternatives --list java)
# only append if javalocation is not found
[ $(grep -c $java_location $HOME/.bashrc) -eq 0 ] && echo "export JAVA_HOME=$java_location" >> ~/.bashrc
# get eclipse
sudo apt-get install eclipse
#to start epclipse just do $ eclipse
# install google-chrome
# run from in the launcher, or invoke google-chrome on the command line
sudo apt-get install google-chrome-stable
# install Terminator, a way fucking better command line tool than stupid terminal
# Personally, my fav text editor for all purposes
sudo apt-get install terminator
$ terminator
# ==============================================
# terminator cheat sheet:
# ctrl + shift + e => split screen vertically
# ctrl + shift + o => split screen horizontally
# ctrl + '-' => make text smaller
# ctrl + '+' => make text larger
# ctrl + TAB => switch between sub windows
# ==============================================
# install vlc, which is the best video player, hands down for ubuntu and linux right now
sudo apt-get install vlc
# install bittorrent client, fastest way to get new distros
sudo apt-get install transmission
# download a bunch of compression software that will come in handy later
sudo apt-get install unace unrar zip unzip p7zip-full p7zip-rar sharutils rar uudeview mpack arj cabextract file-roller
# get ssh to connect to other computers, start service
sudo apt-get install openssh-server openssh-client
sudo service ssh restart
# install dropbox
#navigate to https://www.dropbox.com/install?os=lnx and find the right distro/arch, download
# mv ~/Downloads/dropbox_2015.02.12_i386.deb ~/bin
# cd ~/bin
# sudo dpkg -i dropdropbox_2015.02.12_i386.deb
# dropbox start -i # just for install(get your login, passwd, send you files)
# dropbox start # after install
# start dropbox daemon
# [ -e $(which dropbox) ] && dropbox start || echo "cannot find dropbox"
#!/bin/bash
# http://cli.learncodethehardway.org/bash_cheat_sheet.pdf
# help, try these three things to learn more about commands
man ls
info ls
ls --help
# open file viewer
nautilus .
#================================================================
# 1) Navivation/Looking at Files
#================================================================
# find out local directory
pwd
# change local directory (specify root for abosolute path, non root paths are relative
cd / # goto root directory
cd /home/adam/bin
cd ../ # go up one level towards root
cd ~ # go to home
cd bin # end up at /home/adam/bin
# list the contents of current directory
ls
ls /proc # list specified absolute directory
cd ~
ls bin # list directory relative to local dir
ls -l # list in long format
ls /home/adam/
ls ~/bin/*.tar.gz # list all find in bin that end with ".tar.gz"
# File Manipulation
mkdir ~/sandbox; cd ~/sandbox # make test directory and cd to it
echo "asdf" > ./file1 # print "asdf" to del.me
# show file's contents
cat del.me # print the contents of del.me to screen
cat /etc/passwd #show username and hashed password
grep "username" /etc/passwd # show only lines that contain "username"
# look at long file
export longfile=/var/lib/dpkg/available # set file to variable
cat $longfile # wooahhh, that's way too long to scroll up for
wc $longfile # get the "lines words characters" of a file
less $longfile # open the file in a viewer, 'j' is down, 'k' is up, 'ESC' is quit
cat $longfile | less # same as previous
head -n 10 $longfile # first 10 lines
tail -n 10 $longfile # last 10 lines
# copy files (runs slow)
cp file1 file2 # file1 exists
# move files (runs fast)
mv file1 file3 # file1 is gone
# remove files (runs fast)
rm file* # remove all files starting with 'file'
#================================================================
# 2) Installing new software
#================================================================
# try ubuntu software center, gui way to do this
# show all packages installed on your system
sudo dpkg --list # show all packages installed
# show version of ubuntu (useful for finding software)
lsb_release -a
# show version of kernal, and general system info
uname -a
# apt-get is the package manager, it can find, install, and remove packages
# it must be run as sudo, which gives the user root access, but requires
# a password
# update list of available packages
sudo apt-get update
# search for a package
sudo apt-cache search "libgcc"
# download a package
sudo apt-get install sshd
# remove a package
# sudo apt-get remove sshd
#================================================================
# 3) Writing and Running Scripts
#================================================================
cd ~/bin
# make the demo script (don't worry about this now, it just creates a script
echo -e '#!/bin/bash\necho\necho "directories in /"\nls /\necho\necho "directories in home:"\nls ~' > ~/bin/first_script.sh
# edit files
nano first_script.sh # edit old or create new file
# navigate back and forth text with arrows
# ctrl-X (^X) is exit
# ctrl-O (^O) is write-out or 'save'
# make a script run
chmod u+x first_script.sh # chmod = "change modification", u+x means "user" + "executable",
# execute script:
./first_script.sh
# more:
# trying editing files with vi
#================================================================
# 4) Interacting with the system
#================================================================
# get the system uptime
uptime
# get an kernal error log
dmesg
dmesg -dT # same as above, except show time stamp and delta-time between events
# get the system memory info
cat /proc/meminfo
cat /proc/meminfo | grep "Mem"
# list the usb devices
lsusb -t
# show where a command is:
which ls
# show running processes
ps # just the procs in your shell
ps -A # all processes
ps -A | less #
ps -A | grep 'init' # get a single process
ps -u root # get all of root's processes(run with sudo privilige
ps -u "user" # get all of users's processes
ps aux # different output, shows all processes
# search for process
pgrep bash # returns the [pid]
# get extensive info on process
ls /proc/[pid] # where pid is from ps or pgrep
# get memory map for process (see what's taking up your memory)
pmap [pid]
# kill process
kill -9 [pid]
pkill [process name]
# get an interactive view of process memory/cpu/disk usage
# good for seeing who's eating your ram or Disk IO
top # note the '%CPU' and '%MEM' columns, which are cpu and ram usage
top -u root # procs for user=root
#================================================================
# 6) Internet Networking
#================================================================
# get internet connections
ifconfig # shows interfaces, local ip addresses for each, MAC addresses too
sudo ifconfig eth0 down # turn off eth0 connection
sudo ifconfig eth0 up # turn on eth0 connection
# scan for wireless networks
sudo iwlist scan
# connect to wireless network via commandline
#where networkname and password are what they sould, and IFACE is the
#interface, like eth0, wlan0, etc
nmcli d wifi connect NETWORK-NAME password PASSWORD iface IFACE
# get your hostname, what you appear as on the local network
hostname
# get your IP address as it appears to your local network
ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
# get your IP address as it appears outside your local network(to websites, etc)
curl curlmyip.com # nice little web service, we could write a better one!
# host your current directory to your local network
python -m SimpleHTTPServer 8000 # host current directory on port 8000
sudo python -m SimpleHTTPServer 80 # host on port 80, need root
# print out of localip:port, anyone on local network can enter this into browser
# and see your files
echo $(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1') ":" 8000 | sed 's/ //g' #print out of localip:port to share with
# download url
wget http://www.cnn.com -O cnn.html # download cnn to cnn.html
curl http://www.cnn.com > cnn.html # same, just use curl
# check to see if a server is responding:
ping 127.0.0.1 # Ctrl-C to stop and get stats
ping cnn.com
# check network bandwidth of computer
sudo apt-get install ifstat
ifstat -wT
# check which processes are using network io
sudo apt-get install nethogs
sudo nethogs wlan0 # where wlan0 is interface form ifconfig
# see who owns the server: good for checking sources of new software
whois cnn.com
nslookup cnn.com
#================================================================
# 7) Bash environment
#================================================================
[ ! -e ~/sandbox ] && mkdir ~/sandbox ; cd ~/sandbox
# ~/.bashrc, contains script that is executed on startup of bash session
# set variables
a=1 #local
export a=1 #goes into environment
touch ~/sandbox/fav.txt
export myfavefile=~/sandbox/fav.txt
alias userbin="cd ~/bin" # set userbin as a command
# show all varibale in bash session
printenv
printenv | grep myfavefile # value of specific variable
touch ~/sandbox/dont_like.txt
forget_file=~/sandbox/dont_like.txt
printenv | grep forget_file # value of specific variable not found, need export
# all variables from printenv can be accessed
echo $PATH
printenv|grep path
# exact shell you are using
echo $SHELL
# your pwd result
echo $PWD
# change command line prompt
echo $PS1 # what is shown at each line
export ps_old=PS1
export PS1="$USER@$(hostname):\W$ "
# what i like for network troubleshooting
export PS1="$USER@$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'):\W$ "
# find out where you are
export PS1="\w$ "
export PS1="$(pwd)$ "
#================================================================
# 8) Advanced Bash techniques
#================================================================
# pipe output to waste
ls > /dev/null # don't see anything
# re-direct outputs
# wait, this contains error, and directories
# everythin on the screen is not STDOUT, but STDERR
# pipe STDERR to a file
wc /var/lib* 2> ./var_lib_err.txt
# pipe All (STDERR + STDOUT) to a file
wc /var/lib/* &> var_lib_all.txt
# run wc ./var_lib* and you'll see *err.txt + *lib.txt entries var_lib_all.txt
# look for file
find . -iname "*.txt" # find all files in sub directories that end in ".txt"
# piping: all input/output can be passed to/from programs
ls -tr *.txt | tail -n 1| wc -l # list words in ascending order of data modifined, take the last, get it's word count
# sub expressions
echo $(ls) # run ls as subexpression, print result
echo $(pwd) # run pwd as subexpression, list result
wc -l $(ls -tr *.txt | tail -n 1) # run ordered list, take last result, pass as first arg to wc
# xargs
# xargs takes each input line, stores it in {} using the -I{} option, then lets you pass
# it to a function
ls | xargs -I{} echo {} # echo {} which is each file
ls | xargs -I{} echo wc -l {} # echo 'wc -l' before each file
ls | xargs -I{} wc -l {} # actually run the wc -l
# run string as command
# another really powerful tool is to run a command that is encountered as a string
echo "ls" # prints out ls
echo "ls" | bash # passes "ls" into sh, which runs the string as a shell command
ls *.txt | xargs -I{} echo wc -l {} | bash
## run condition, if true, run command
touch cond.text.txt # create an empty file
[ ! -e cond.test.txt ] && echo "first statment" || echo "second statment"
[ -e cond.test.txt ] && echo "first statment" || echo "second statment"
[ -e cond.test.two.txt ] && echo "first statment" ; echo "do this anyway"
touch cond.test.two.txt
[ -e cond.test.two.txt ] && echo "first statment" ; echo "do this anyway"
# used for things lie [ -e inputForProgram ] && wget input > inputForProgram ; pythonrunAnysls ./
# command 1: if file isn't found, download it ; command 2, run wc on file
# if no file, we download it, then always run wc
[ ! -e cnn.dump.txt ] && wget cnn.com -O cnn.dump.txt 2>/dev/null; wc cnn.dump.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment