Wednesday, September 18, 2013

Living In Linux

Check is something is installed.

yum list installed | grep std

show line numbers in vi

:set nu

Secure Copy between machines

scp user@box2:/folderOnBox2/myfile.txt /folderOnThisBox
scp /folderOnThisBox/myfile.txt user@box2:/folderOnBox2

# if you see this error, it's because scp doesn't exist on box2
bash: scp: command not found

# here's how to install scp
yum -y install openssh-clients

Look inside a Jar file

unzip -t file.jar | grep -i 'searchterm' 

Basics

pwd                                # present working directory
ls -la                             # list all, i.e. show details
kill -9 SOME_PID                   # tell pid to shutdown
rm -rf /your/stuff                 # recursive remove including folders
mkdir /newdir                      # new directory
stty erase HIT_BACKSPACE_KEY       # usually not required on linux, tells it which key is backspace
ln -s /existing /newlink           # create symlink
grep -r "whatever" /search/here    # find text in files
find /here -type f -name 'myfile'  # find files by name
df -h /                            # report on disk space
uname -a                           # show machine name, os info, etc
cat /proc/version                  # os info on most linux
cat /etc/issue                     # os info on most linux
od -t x1 YOUR_FILE                 # binary dump
ps -ef                             # show running pids
ps -fL SOME_PID                    # get treads for a pid, the thread id can be used like a pid
env                                # show environment variables
export YOURVAR=YOUR_VALUE          # set and export an environment variable
tail -n 200 -f /whatever.txt       # show active contents of a file

Users and Groups

id                                 # current: user, group, etc
who am i                           # current: user, machine, etc
who -q                             # names and count of current users
su - SOME_USER                     # switch users
cat /etc/groups                    # show existing groups info
groupadd NEW_GROUP                 # create a new group
usermod -g NEW_GROUP EXISTING_USER # change the primary group of an existing user

Archives

cp /whatever/your.tar /tmp
cd /tmp
tar -xf your.tar        # extract this file, result is probably a folder named "your"
/bin/rm your.tar        # remove source without warning
tar -zxf your.tar.gz    # extract this file, result is probably a folder named "your"

unzip -q your.zip       # extract this file, result is probably a folder named "your"

cd /
tar -zcf /out/your.tar.gz your/folder # tar and zip folder, note lack of leading slash in source

Who is connected on what port? Note: you have to be root for the -p switch to show you the pids with the -p switch.

netstat -aop
netstat -atn
netstat -at
lsof | head -1
lsof | grep TCP

Printing

# Use this to print double sided -- From: D. Tyner (http://www.blogger.com/profile/05747875860218795349)
lpr -P[printer name] -o sides=two-sided-long-edge [file name] -o page-ranges=N-M 

Processes

Show the column that will be lost during the sort below.

ps -e --format=pid,euser,%cpu,%mem,sz,vsz,cmd | head -1

Show processes by percent ram usage.

ps            get process information
-e            show information for all processes
--format=...  only show the specified columns
| sort        pipe the output to sort because the ps --sort option can't sort "cooked" values
-nrk          numeric reverse (descending) sort on the specified column
4             sort on the fourth column, i.e. %mem
| head        pipe the output to head because we only care about the large results
-10           only show the first ten results

ps -e --format=pid,euser,%cpu,%mem,sz,vsz,cmd | sort -nrk 4 | head -10  

Show total percent ram usage.

ps -e --no-headers --format=%mem | awk '{ SUM += $1} END { print SUM }' 

Disk Space

df -h /

Line Endings / Line Breaks

If you don't want to use dos2unix, you can use tr -d '\015' < /tmp/infile > /tmp/outfile

function fix_line_breaks
{
  echo "$1"

  tr -d '\015' < "$1" > "/tmp/temp.$$"
  if [ $? -ne 0 ]; then return 1; fi

  rm -f "$1"
  mv "/tmp/temp.$$" "$1"
  if [ $? -ne 0 ]; then return 1; fi

  return 0;
}

Get Pid

ps -ef gives you a list of current processes including their PID and CMD which equates to their name. Hence you can grep the output for just the line you care about. Unfortunately the grep will see itself so to exclude itself from the results you can square-bracket the first letter of your expected CMD thus causing the grep to not include itself in the list of reqults. Finally we use awk to select only the 2nd column which is the PID.

ps -ef | grep [y]ourBinary | awk '{ print $2 }'
{ "loggedin": false, "owner": false, "avatar": "", "render": "nothing", "trackingID": "UA-36983794-1", "description": "Tips and examples for how to do stuff in Linux bash and bourne shell.", "page": { "blogIds": [ 70 ] }, "domain": "holtstrom.com", "base": "\/michael", "url": "https:\/\/holtstrom.com\/michael\/", "frameworkFiles": "https:\/\/holtstrom.com\/michael\/_framework\/_files.4\/", "commonFiles": "https:\/\/holtstrom.com\/michael\/_common\/_files.3\/", "mediaFiles": "https:\/\/holtstrom.com\/michael\/media\/_files.3\/", "tmdbUrl": "http:\/\/www.themoviedb.org\/", "tmdbPoster": "http:\/\/image.tmdb.org\/t\/p\/w342" }