Skip to content

Instantly share code, notes, and snippets.

@hansrajdas
Last active September 30, 2021 15:49
Show Gist options
  • Save hansrajdas/b39fddbaa68782be5e000de5cca963fa to your computer and use it in GitHub Desktop.
Save hansrajdas/b39fddbaa68782be5e000de5cca963fa to your computer and use it in GitHub Desktop.
Some shell commands used frequently

Redo last command but as root

sudo !!

Open an editor(for composing) to run a long command

ctrl + x + e

Don't add command to bash history

Add a space before command

 ls -l

Fix a very long command you messed up

Fix command

fc

This will open last command in a file, you can edit that and exit. I will run the edited command

Quickly create multiple directories

mkdir -p folder/{sub1,sub2}/{sub1,sub2,sub3}

# Above command will create 6 directories

# folder/sub1/sub1
# folder/sub1/sub2
# folder/sub1/sub3
# folder/sub2/sub1
# folder/sub2/sub2
# folder/sub2/sub3

# This also works with numbers also like

mkdir -p folder/{1..100}/{1..100}

# This will create 100*100 = 10,000 new directories, 1 to 100 directories and each directory it will have 1 to 100 sub directories

Intercept stdout and log to a file

cat file.txt | tee -a log.txt | cat > /dev/null

# -a in command means append to log.txt

Exit terminal but all process running

disown -a && exit  # Disown all process then exit

Extract RPM package

rpm2cpio <package-name>.rpm | cpio -idmv
rpm2cpio php-5.1.4-1.esp1.x86_64.rpm | cpio -idmv

List contents of installed RPM

rpm -ql <package-name>
rpm -ql HSS

List contents of a RPM

rpm -qlpv <package-name>.rpm
rpm -qlpv HSS-2.5.6.0.0-1.x86_64.elf6.rpm

Check ports allocated to a process

semanage port -l | grep <process-name>
semanage port -l | grep http

Compile with gprof

Use gcc flags "-p -pg" for compiling and linking

Get top output on console

top -b -n 1 -p <pid>

List contents of tar

tar -tf <tar_name>

Run program with valgrind

valgrind --tool=memcheck --leak-check=yes --show-reachable=yes -v <program-to-run> <program-args>
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes -v ./hssStub -m Load

Callgrind

valgrind --tool=callgrind <program-to-run> <program-args>
callgrind_annotate --auto=yes callgrind.out.pid

# Other options
#   --tree=both (prints, for each functions, their callers and the called functions)
#   --threshold=100 (will print all the events)

export CVSROOT

export CVSROOT=:pserver:gur29510@10.203.155.20:/home/lte_epc

Share public key to destination machine to scp/ssh without password

  1. Generate public key
ssh-keygen
  1. Copy generated public key to destinations authorized key
cat /var/lib/jenkins/.ssh/id_rsa.pub | ssh klocwork@172.16.105.244 'cat >> .ssh/authorized_keys'

Delete all files/directories except one or more

rm -rf !(do_not_delete_this_file_or_directory)
rm -rf !(file1.txt|file2.txt)

For above command to work, we need to enable extended pattern matching in shell using below command

shopt -s extglob

Command to see all logged in users on machine and what they are doing

w

cut command

Use -c option to print selected characters from each line

cut -c1  # Prints only first character of each line
cut -c 2,7  # Select 2nd and 7th characters
cut -c 2-7  # Select from 2nd to 7th character, both included
cut -c 2-  # Select from 2nd char to end
cut -d$'\t'  -f 1-3  # Cut with tab and select first 3 words(tab splitted)

Get weather info

curl wttr.in/delhi?lang=en

Save git username and password for future

git config credential.helper store

Use -A, -B and -C in grep to print lines after, before and between searched string

grep -A 1 "some_text" <path>  # Print 1 line after each search pattern
grep -B 1 "some_text" <path>  # Print 1 line before each search pattern
grep -C 1 "some_text" <path>  # Print 1 above and below search pattern

Use -c in grep to count occurrences

grep -c "some_text" <path>  # Prints counts of search string in each file

Use -e in grep to give multiple patterns

grep -e "text_1" -e "text_2"  # Print all lines having text_1 or text_2

Search for consecutive same digit from 0-9 like 112... or 1 1 2...

grep -E '([0-9]) *\1' *
# -E to specify a regular expression
# * with \1 to search for 0 or 1 space
# \1 to search for same digit
# Last * for path

Open files with specific string in vim (or other editor)

grep -rl myString . | xargs -o vim

vim `grep -rl myString .`

HEAD/TAIL example

head -c10  # Print first 10 characters
tail -c10  # Print last 10 characters

Tab(\t) character in cut, sort commands is specified using $

sort -t $'\t' -k 2  # With with respect to second tab separated values

Compress to .zip:

zip -r roster.zip roster.sql

Decompress .zip file:

unzip roster.zip

SED - Stream editor

sed 's/old-string/new-string/g'  # Globally replace all old with new string
sed 's/old/new/ig'  # i, Ignore case
sed 's/old/new/2'  # Replace only second instance
sed 's/str/{&}/g'  # Enclose all occurrences of str in {}

AWK - Text formatter

awk '{print}'  # Print everything
awk '{print $1 $2}'  # Print only values from column 1 and 2
awk '/xyz/ {print}'  # Print lines having string xyz
awk '{if($4 == "") print}'  # Print columns whose 4th column is empty
awk '{if($2 >= 10 && $3 >= 10) print $0; else print $4}'  # If-else

BC - Calculator

echo "10 + 20" | bc  # Calculates to 30
bc filename.txt  # filename has mathematical expressions
bc  # Opens bc console

PASTE - Pastes multiple lines

paste file1.txt file2.txt  # Pastes each word from file1 and 2 column wise
paste -s file1.txt file2.txt  # Paste all lines from 1 file at a time
paste -d "|" file1.txt  # Use | as delimeter instead of tab(default).
paste file1.txt - -  # Paste 2 consecutive lines

Run a binary in gdb with command line args

gdb --args <binary> <command line args>
gdb --args python -V  # Run python binary with -V argument

Create private key and certificate signing request(CSR)

Below command creates 2 files - domain.key has private key and domain.csr is certificate signing request file.

openssl req -newkey rsa:2048 -nodes -keyout domain.key -out domain.csr

Check open ssl connection and fetch all certificates including all intermediates

openssl s_client -connect <hostname>:<port-number>

Open a file in default application like pdf

gio open <path to pdf file>

AB load testing tool for HTTP(s) based applications

ab -k -c 2 -n 10 -s 60 http://example.com/

# -k, keep alive
# -c, concurrent connections
# -n, total number of requests to be sent
# -s, timeout in seconds for ab to complete analysis(by default it is 30)

Useful links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment