Showing posts with label Linux commands. Show all posts
Showing posts with label Linux commands. Show all posts

The Linux lp Printing Command


Linux Commands - Search


Linux Commands For Beginners


TL;DR
  • Basic Linux commands that we use literally all the time.
alias

alias grep=“grep –color” alias

ps
xargs
Sort files by date then grep
  • ls -rt *.log | xargs grep -l
List open files
  • ls -l /proc/${pid}/fd
    • Use this when lsof is not installed
  • lsof -p ${pid}
watch
curl
pbcopy (take standard input to clipboard) + pbpaste (take data from clipboard to standard output)
diff

sed

  • sed -n ‘16224,16482p’ filename > newfile
System
Check the age of the system
  • rpm -qi basesystem | grep “Install Date”
Misc
Get current working directory of a process
  • pwdx
  • lsof -p | grep cwd
  • readlink -e /proc//cwd ##### [View a range of bash history]
  • fl -l ${start} ${end}
  • history | sed ‘start,{end}p’
Brace expansion
  • mkdir -p {f1, f2}
  • for i in {1..3}; do echo $i; done
    • {START..END..INCREMENT}

Command History

!!:nwhere n is the 0-based position of the argument you want
!$last argument from previous command
!^first argument (after the program/built-in/script) from previous command
!!previous command (often pronounced “bang bang”)
!ncommand number n from history
!patternmost recent command matching pattern
!!:s/find/replacelast command, substitute find with replace
:pprint the command - !!:p, !!n:p

Bash Scripting Essentials


Brace Expansion
chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}

Special Variables
$? Exit value of last executed command.
pid=$!
wait $pid

$! Process number of last background command.
$0 First word; that is, the command name. This will have the full pathname if it was found via a PATH search.
$n Individual arguments on command line (positional parameters).
$# Number of command-line arguments.

“$*” All arguments on command line as one string (“$1 $2…”). The values are separated by the first character in $IFS.
“$@” All arguments on command line, individually quoted (“$1” “$2” …).

Tests
-n string is not null.
-z string is null, that is, has zero length

Testing for File Characteristics
-d File is a directory
-e File exists
-f File is a regular file
-s File has a size greater than zero
-r, -w, -x, -s - socket

[ -d "$dir" ] && echo "$dir exists." || echo "$dir doesn't exists."

Testing with Pattern Matches
== pattern
=~ ere
if [[ "${MYFILENAME}" == *.jpg ]]

-a, &&
-o, ||

if [ ! -d $param ]
if [ $? -ne 0 ]

Parsing
HEAP_DUMP_DIR=$(sed 's/-XX:HeapDumpPath=\([^ ]*\)/\1/' <<< $param)

Function
Use variable $1, $2..$n to access argument passed to the function.
Hello () {
   echo "Hello $1 $2"
   return 10
}
Return value in bash
echo in the function, capture the result in caller $()

Hello a b

for i in $( command ); do command $i; done

for i in $( command ); do
  command $i
done
if [ "$var" == "value" ];  then

else
fi

Google Shell Style Guide
quote your variables; prefer "${var}" over "$var",
Use $(command) instead of backticks.
[[ ... ]] is preferred over [test
- [[ ... ]] reduces errors as no pathname expansion or word splitting takes place between [[ and ]] and [[ ... ]] allows for regular expression matching where [ ... ] does not

Use readonly or declare -r to ensure they're read only.
Make variable readonly: readonly var=value
Make function readonly: readonly -f function
readonly -p/-f

if [[ -f ~/.bashrc ]]; then
   source ~/.bashrc
fi
[ ! -f $FILE ] && { echo "$FILE not found"; exit -1; }

$(( $a+$b )) to execute arithmetic expressions
Put ; do and ; then on the same line as the while, for or if.
Prefer brace-quoting all other variables.
Use "$@" unless you have a specific reason to use $*.
- "$@" will retain arguments as-is, so no args provided will result in no args being passed on;
- "$*" expands to one argument, with all args joined by (usually) spaces, so no args provided will result in one empty string being passed on.

while IFS=, read var1 var2 var3; do
  ...

done < file.txt

Use Local Variables
local var="something"
local var
var="$(func)" || return

if [[ "${my_var}" = "some_string" ]]; then
  do_something
fi
-z (string length is zero) and -n (string length is not zero)

if ! mv "${file_list}" "${dest_dir}/" ; then

fi

Use set -o errexit (a.k.a. set -e) to make your script exit when a command fails.
Then add || true to commands that you allow to fail.
set -e - enable exit immediately
set +e - disable exit immediately
set -x  - print a trace Use set -o nounset (a.k.a. set -u) to exit when your script tries to use undeclared variables.
Use set -o xtrace (a.k.a set -x) to trace what gets executed. Useful for debugging.
set -u   Fail for undefined variable (set -o nounset)

Use $(( ... )), not expr for executing arithmetic expressions. which is more forgiving about space
Use (( or let, not $(( when you don't need the result

Identify common problems with shellcheck.

Misc
while true; do some_commands_here; done
while true
do
  some_commands_here
done

Run command until success
until $the_command; do echo "Try again"; done
while [ -n $(the command) ]; do echo "Try again";done;

Related
Essential Linux Commands for Developers

Essential Linux Commands for Developers


Shortcuts:
Ctrl + a - Move to the start of line
Ctrl + e - Move to the end of line
Clear the screen:  Ctrl + l
Search as you type. Ctrl + r and type the search term; Repeat Ctrl + r to loop through results.

!$
tac: print a file line by line in reverse order.

List all open ports
netstat -ltn -- all process that listens on tcp ports
-udp|-utroub
--tcp|-t
--listeningtro
--program|-p

Which process opens 9160
lsof -i :9160
lsof -p pid1
lsof -Pani -p PID
lsof /var/log/system.log
List opened files under a directory
lsof +D /var/log/
List files opened by a specific user
lsof -u user
lsof -u ^user
lsof -i

iptables
To get a complete presentation of the netfilter rules
iptables -vL -t filter
iptables -vL -t nat
iptables -vL -t mangle
iptables -vL -t raw

iptables -vL -t security

nohup someCommand > someFile.log 2>&1 &

sort | uniq -d
     -u      Only output lines that are not repeated in the input.
     -d      Only output lines that are repeated in the input.
     -c      count
Sort the output with the most frequent lines on top

sort FILE | uniq -c | sort -nr

find /usr -size +10M
find . -exec xxx {} \;

Find configuration data
find /etc -type f -exec grep -FHi "ENCRYPTION_PASSWORD" {} +
grep -r ENCRYPTION_PASSWORD /etc 2>/dev/null

find /etc -type f -print0  | xargs -0 grep ENCRYPTION_PASSWORD

Find biggest files
find ~/ -type f -exec du -Sh {} + | sort -rh | head -n 5

How long a command takes
time curl ""

List all functions
declare -f
declare -F - only list function names
declare -f function_name

Truncate a file
cat /dev/null > file
> file

Generate random number
/dev/random block when entropy pool is exhausted
/dev/urandom will not block

echo $RANDOM
od -An -N1 -i /dev/urandom
od -An -N2 -i /dev/urandom
for i in {1..5}; do echo $RANDOM; done

Display disk usage
du -sm
List all directories and their total size:
du -sh *
-s: Display an entry for each specified file. (Equivalent to -d 0)
Show only total for each directories

du -h -d 1

List hidden files:  ls -a | grep "^\."
List files and sorted by size: ls -l | grep ^- | sort -nr
List link files: ls -l | grep '^l'
ls -l --block-size=M
find . -type d -maxdepth 1 -name "H2*"
ls -d H2*/

Grep:
-c --count: Show count of matched lines
-E, --extended-regexp - same as egrep
-n, --line-number
-l, --files-with-matches
-L, --files-without-match

grep -E '^abc(worda|wordb)' /etc/group
-n or --line-number
-A NUM, --after-context=NUM
       Print NUM lines of trailing context after matching lines.
-B NUM, --before-context=NUM
       Print NUM lines of leading context before matching lines.
-C NUM, --context=NUM
       Print NUM lines of output context. 
-w - searched as a word
-o, --only-matching
Prints only the matching part of the lines.
-l  -- only show matched file name
-w -- only if it's a whole word
-r  -- recursively search

grep -r --exclude=\*.{html,htm,js} pattern  rootdir

Grep file that contains binary data
cat -v tmp/test.log | grep regex
-v      Display non-printing characters so they are visible.

Search specific line ranges:
sed -n 'startLine,endLinep' a.txt | grep XX 

Use extended regular expression with grep -E

scroll results and pagination
grep ** | more/less 

sed s/word1/word2/g fileName
Only display nth line:         sed -n 'n p' file
Delete nth line:          sed 'n d' file > newFile
Delte nth line in place:  sed –i 'n d' file.txt

Remove last line:          sed –i '$ d' file.txt
-i change in place
Delete first line: sed –i '$ d' file.txt
sed –i 'm,n d' file.txt
sed -i '' 's/original/replace/' file.txt
sed -i.bak ...
sed -n 'n p' file.txt | wc -c
-i extension | --in-place
Edit files in-place, saving backups with the specified extension.  If a zero-length extension is given, no backup will be saved.


xargs
echo a b c | xargs echo
find /tmp -name "*.bk" -type f -print | xargs /bin/rm -f
find /tmp -name "*.bk" -print0 | xargs -0 -I {} mv {} ~/bk.files
-- better: find /tmp -depth -name "*.bk" -type f -delete
find /tmp -name "*.bk" -print0 | xargs -0 -I file mv file ~/bk.files
cut -d: -f1 < /etc/passwd | sort | xargs echo

-I replstr
--null, -0 - handle spaces in file name
Change xargs to expect NUL (``\0'') characters as separators, instead of spaces and newlines.  This is expected to be used in concert with the -print0 function in find

less +F the.log
Ctrl+c to normal less mode

pv - monitor the progress of data through a pipe
pv the.gz | gunzip
jq - sed for JSON
curl the_url | jq

Scp copy from local to remote:
scp /file/to/send username@remote:/where/to/put
Remote to local:
scp username@remote:/file/to/send /where/to/put
Send files between two remote hosts:

scp username@remote_1:/file/to/send username@remote_2:/where/to/put

Copy file from remote host to local via gateway 
scp -o "ProxyCommand ssh $USER@$bastion-host nc $destinationHost 22" $USER@$destinationHost:/home/$USER/heapdump.hprof heapdump.hprof

Copy file from local to remote host via gateway 
scp -o "ProxyCommand ssh $USER@$bastion-host nc $destinationHost 22" heapdump.hprof $USER@$destinationHost:/home/$USER/heapdump.hprof 

Netcat - nc
Listening on server
nc -l 2389 > test

nc -k -l 2389 - server would stay up
Connect to server on specific port

cat testfile | nc remoteHost 2389
Port Scanning
nc -zv remoteHost 20-30

Bulk rename files
brew install rename
rename -n -v 's/\.csv$/\.json/' *.csv
-n: --just-print/--dry-run

Brace Expansion
echo a{d,c,b}e

sleep 10 - sleep 10 seconds
wait pid - wait process finish
command &
wait $!

Find out current working directory of a running process
pwdx $PID
lsof -p $PID | grep cwd
readlink -e /proc/$PID/cwd

Setting an environment variable for one command only
FOO=bar bash -c 'somecommand someargs | somecommand2'
(export FOO=bar ; somecommand someargs | somecommand2)

Change working directory for current command only
(cd SOME_PATH && exec_some_command)

Check Linux System Info
lscpu
free -t -m
cat /proc/pid/smaps
pmap pid | grep total

uptime
sar
jstack -m
pstack

top
top -p PID 
-on mac: top -pid PID
top -c or Press 'c' in top view: to show full command
top -H -p $pid
sort on other fields (default by cpu)
Press "SHIFT + F" and Select your choice below and press ENTER.

Get the hostname of remote server
host ip-address

Check system time zone
date
date +%Z
cat  /etc/localtime

Create zip file
gzip -k the-file
- without the tar, -k: keep the original file

tar -czf my.tar.gz the-folder_or_file
gunzip file.gz
gzip -d file.gz
unzip –t file.zip
test whether zipfile is corrupted or not

awk, gawk
gawk 'match($0, pattern, ary) {print ary[1]}'

vi
Count the number of occurrences of a word
:%s/pattern//ng
:set all
- (no)nu, (no)ic,
Compound search on multiple lines
/pattern1/;/pattern2/
Bookmark
MA -> `A -> ``

dd, 5dd
d$  - delete to end of line
d0  - delete to beginning of line

1,$d - delete all
1,.d  - delete to beginning of file
.,$d  - delete to end of file
Y - copy
- p pastes after the cursor position
- P pastes before.

5Y
Y$ - the end of the line
G - go to the last line

ZZ in command line - :wq to exit vi
append a file to current file

:r file2

Misc
ps -ef | grep PROCESS | grep -v grep | awk '{print $2}' | xargs kill -9
tail **/*.log
cp
cp --parents a/b/c existing_dir
--no-target-directory | -T

clear log without delete it
cp /dev/null filename
> filename

su different_user -c "command"
mmv - Mass Move and rename
mmv "*.csv" "#1.xls"
To install mmv
yum install -y http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
yum install mmv

Compare difference of two folders
diff -rq folder1 folder2

apropos - search the whatis database for strings
apropos "kill process"

unzip '*.zip'
unzip *.zip would fail with error: "caution: filename not matched"
gunizp *.gz - don't add ' or ""

Add text to the beginning of a file
echo 'task goes here' | cat - todo.txt > temp && mv temp todo.txt

Java
Use jstack to generate thread dump
nohup jmap -F -dump:format=b,file=/root/heapdump.hprof pid &

Commands for troubleshooting
find class in jars
find . -name "*.jar" | xargs grep Hello.class
Search Contents of .jar Files for Specific String

gfind . -iname '*.jar' -printf "unzip -c %p | grep -q 'string_to_search' && echo %p\n" | sh

yum -y install java8-server-jre.x86_64 --nogpgcheck yum repolist
yum-config-manager --disable \*
yum-config-manager --add-repo repo_url

sudo without password
echo 'username ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
sed -i 's/.*requiretty$/#Defaults requiretty/' /etc/sudoers

permission denied when run sudo with redirection
-  Output redirection (via the > operator) is done by the shell, not by echo.
sudo bash -c "echo 'net.ipv6.conf.all.disable_ipv6 = 1' >>/etc/sysctl.conf"

Find hostname of remote ip address
host $ip
nslookup $ip

VI
:set ic|noic

Resources
Google Shell Style Guide

Labels

ANT (6) Algorithm (69) Algorithm Series (35) Android (7) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) JSON (7) Java (186) JavaScript (27) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) adsense (5) bat (8) regex (5) xml (5)