Bash Commands

Updated: 2018-11-30

Not a comprehensive manual, but some notes and examples for quick lookup.

For the full manual, use man command.

Manual has many sections

  • Section 1: user commands
  • Section 2: system calls
  • Section 3: library functions
  • Section 4: special files
  • Section 5: file formats
  • Section 6: games
  • Section 7: conventions and miscellany
  • Section 8: administration and privileged commands
  • Section L: math library functions
  • Section N: tcl functions

e.g. to check manual for mount in system calls vs admin command:

$ man 2 mount
$ man 8 mount

bg/fg

Foreground to Background:

[Ctrl + z]

$ bg

Background to Foreground:

# something is running in background
$ fg

curl

Retrieve a page

$ curl www.example.com

Save to a file

$ curl -o example.html www.example.com

Save as the origin name

$ curl -O www.example.com/example.html

Redo the request if page was moved(3XX response code)

$ curl -OL www.example.com/example.html

Get

$ curl -X GET www.example.com

Put

$ curl -X PUT --data-binary @file.xml -H "Content-type: text/xml" http://example.com/put

Use -d(--data) or --data-binary to send data.

-d sends the Content-Type application/x-www-form-urlencoded, to specify another content type, use -H "Content-Type: application/json", e.g.

$ curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login

dd

The dd Unix utility program reads octet streams from a source to a destination, possibly performing data conversions in the process.

Create File With Zeroes

Creating a 1 MiB file, called foobar, filled with zeroes:

dd if=/dev/zero of=foobar count=1024 bs=1024

Note: The block size value can be given in SI (decimal) values, e.g. in GB, MB, etc. To create a 1 GB file one would simply type:

Test IO Performance

$ dd if=/dev/zero of=test bs=64k count=16k conv=fdatasync
16384+0 records in
16384+0 records out
1073741824 bytes (1.1 GB) copied, 3.94954 s, 272 MB/s

Read operations from /dev/zero return as many null characters (0x00) as requested in the read operation.

Unlike /dev/null, /dev/zero may be used as a source, not only as a sink for data.

dd if=/dev/zero of=/dev/<destination partition>

dd if=/dev/zero of=foobar count=1 bs=1GB

diff

diff sdiff vimdiff colordiff

dig

dig (domain information groper): DNS lookup utility

Unless it is told to query a specific name server, dig will try each of the servers listed in /etc/resolv.conf

Example

$ dig google.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13686
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;google.com.			IN	A

;; ANSWER SECTION:
google.com.		8	IN	A	xxx.xxx.xxx.xxx

;; Query time: 16 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Thu Jan 01 20:24:55 PDT 2020
;; MSG SIZE  rcvd: 55

env

Set PATH for sudo in Ubuntu

In Ubuntu, these commands are showing different results:

$ env
$ sudo env

This may cause a problem when you are setting some bin folder in your path while you need root permission to execute, for example, if you install node in your own folder while you need $ sudo node bin/www to serve it, you will get an error saying node cannot be found.

Solution

$ sudo env PATH=$PATH [COMMAND]

this will use your own PATH when executing the COMMAND, e.g.

$ sudo env PATH=$PATH node bin/www

or add this to ~/.bashrc

alias sudo='sudo env PATH=$PATH'

ldd

ldd: print shared library dependencies

$ ldd ./my-program
not a dynamic executable
$ ldd /usr/bin/gzip
linux-vdso.so.1 =>  (0x00007fff39fff000)
    libc.so.6 => /lib64/libc.so.6 (0x00007fb842afa000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb842ea0000)
$ ldd /usr/bin/ssh
linux-vdso.so.1 =>  (0x00007fffd0164000)
libfipscheck.so.1 => /lib64/libfipscheck.so.1 (0x00007fb62de63000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fb62dc43000)
libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007fb62d8a9000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007fb62d6a6000)
libz.so.1 => /lib64/libz.so.1 (0x00007fb62d48f000)
libnsl.so.1 => /lib64/libnsl.so.1 (0x00007fb62d276000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fb62d03f000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fb62ce24000)
libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007fb62cbe0000)
libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007fb62c8fa000)
libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007fb62c6cd000)
libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fb62c4c9000)
libnss3.so => /usr/lib64/libnss3.so (0x00007fb62c18d000)
libc.so.6 => /lib64/libc.so.6 (0x00007fb62bdf8000)
libplc4.so => /lib64/libplc4.so (0x00007fb62bbf3000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fb62b9ef000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb62e2da000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007fb62b78c000)
libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007fb62b581000)
libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fb62b37e000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb62b160000)
libnssutil3.so => /usr/lib64/libnssutil3.so (0x00007fb62af3a000)
libplds4.so => /lib64/libplds4.so (0x00007fb62ad36000)
libnspr4.so => /lib64/libnspr4.so (0x00007fb62aaf8000)

LD_LIBRARY_PATH to include this path for the application to successfully run.

ls

Set Color

$ ls --color=auto
$ ls --color=tty
$ ls --color=none

seq

Generate Sequence

Generate sequence

$ seq 5856 5859
5856
5857
5858
5859

Set delimiter by -s ('\n' As default)

$ seq -s ',' 5856 5859
5856,5857,5858,5859,

ss

ss vs netstat

  • netstat: read various /proc files to gather information. Slow when there are lots of connections to display. Now deprecated.
  • ss: get information directly from kernel space.

Commands

  • ss: get all connections
  • ss -t: get tcp connections not in listen mode (server programs)
  • ss -u: get udp connections not in listen mode
  • ss -x: get unix socket pipe connections
  • ss -ta: get all tcp connections
  • ss -au: get all udp connections
  • ss -nt: all tcp without host name
  • ss -ltn: listening tcp without host resolution
  • ss -ltp: listening tcp with PID and name
  • ss -s: prints statstics
  • ss -tn -o: tcp connection with domain host and show keepalive timer
  • ss -tl4: ip4 connections

tar

  • x: extract
  • z: compress archive using gzip program
  • c: create archive
  • v: verbose
  • f: archive file name

.tar

Create:

$ tar -cvf archive_name.tar directory_to_compress

Extract the archive:

$ tar -xvf archive_name.tar.gz

Extract the files to a different directory:

$ tar -xvf archive_name.tar -C /tmp/extract_here/

.tar.gz

Create and compress

$ tar -cvzf archive_name.tar.gz directory_to_compress

Decompress and extract:

$ tar -xvzf archive_name.tar.gz

Extract the files to a different directory:

$ tar -zxvf archive_name.tar.gz -C /tmp/extract_here/

.tar.bz2

Create

$ tar -jcvf archive_name.tar.bz2 directory_to_compress

Extract:

$ tar -jxvf archive_name.tar.bz2 -C /tmp/extract_here/

type

Shell Builtin

Linux built in commands:

$ type pwd cd
pwd is a shell builtin
cd is a shell builtin

And type itself is a shell builtin

$ type type
type is a shell builtin

Shell Keyword

Shell keywords are used in shell scripts

$ type if fi
if is a shell keyword
fi is a shell keyword

Alias

If you have alias defined in .bash_profile or .bashrc, for example

alias ls="ls -G"

where -G is set to enable colorized output. Then ls becomes an alias:

$ type ls
ls is aliased to `ls -G'

Executable

If you have docker installed, you will see something like this:

$ type docker
docker is /usr/local/bin/docker

Show all python executables

$ type -a python

xargs

Similar to “map”, apply functions on each item in the list

e.g. kill all ssh connections

$ ps -ax | grep ssh | cut -d ' ' -f 1 | xargs kill -9

zip

Compress

$ zip -r archive_name.zip directory_to_compress

Extract:

$ unzip archive_name.zip

Trouble Shooting

Error

$ unzip *.zip
error: caution: filename not matched:

Solution

$ unzip \*.zip