20 Super-Helpful Bash Commands

Here’s a list of some of the bash commands that I use the most and a very brief description of what I use them for.

ifconfig

Used to find out your IP address. (look at wlan0, the line beginning with ‘inet addr’)

[kmurray@radon ~]$ /sbin/ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:19266 errors:0 dropped:0 overruns:0 frame:0
TX packets:19266 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:963988 (941.3 KiB) TX bytes:963988 (941.3 KiB)

wlan0 Link encap:Ethernet HWaddr 00:1A:73:0F:0E:FD
inet addr:192.168.1.139 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::21a:73ff:fe0f:efd/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8712 errors:0 dropped:0 overruns:0 frame:0
TX packets:8944 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:8002586 (7.6 MiB) TX bytes:3113446 (2.9 MiB)

route -n

Used to find the IP address of your default gateway. (look for the line with a G in the Flags column}

[kmurray@radon ~]$ /sbin/route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 wlan0

ip

I use this one similarly to ifconfig and route.

[kmurray@radon ~]$ /sbin/ip route
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.139
default via 192.168.1.1 dev wlan0 proto static

[kmurray@radon ~]$ /sbin/ip addr
1: lo: mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:16:d4:ec:25:4b brd ff:ff:ff:ff:ff:ff
3: wlan0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:1a:73:0f:0e:fd brd ff:ff:ff:ff:ff:ff
inet 192.168.1.139/24 brd 192.168.1.255 scope global wlan0
inet6 fe80::21a:73ff:fe0f:efd/64 scope link
valid_lft forever preferred_lft forever

ping

Used to test network connectivity.

[kmurray@radon ~]$ ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=1.23 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.17 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=1.14 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=1.19 ms
^C
— 192.168.1.1 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3268ms
rtt min/avg/max/mdev = 1.149/1.190/1.238/0.032 ms

traceroute

Another network connectivity tool. This one tells you how many routers you have to go through in order to connect to a particular host.

[kmurray@radon ~]$ traceroute simplehelp.net
traceroute to simplehelp.net (74.52.95.42), 30 hops max, 60 byte packets
1 192.168.1.1 (192.168.1.1) 1.121 ms 2.010 ms 2.218 ms
2 24.68.178.1 (24.68.178.1) 25.950 ms * *
3 * * *
4 * * *
5 rc2bb-pos0-1-5-0.vc.shawcable.net (66.163.77.185) 39.178 ms 128.371 ms 50.787 ms
6 rc2sj-pos6-0-0.cl.shawcable.net (66.163.77.70) 119.703 ms 76.364 ms 88.337 ms
7 rc2sj-ge2-0-0.cl.shawcable.net (66.163.67.10) 110.862 ms 55.853 ms *
8 * rx0sj-comcast.cl.shawcable.net (66.163.67.110) 61.930 ms 109.439 ms
9 pos-0-8-0-0-cr01.losangeles.ca.ibone.comcast.net (68.86.85.82) 79.587 ms 102.306 ms 81.225 ms
10 pos-0-9-0-0-cr01.dallas.tx.ibone.comcast.net (68.86.85.142) 120.064 ms 125.584 ms 118.207 ms
11 68.86.88.50 (68.86.88.50) 107.133 ms 109.350 ms 113.994 ms
12 te9-1.dsr02.dllstx3.theplanet.com (70.87.253.22) 87.928 ms 87.949 ms te9-1.dsr01.dllstx3.theplanet.com (70.87.253.6) 89.500 ms
13 76.fd.5746.static.theplanet.com (70.87.253.118) 99.789 ms 97.329 ms 100.789 ms
14 po1.car03.dllstx6.theplanet.com (12.96.160.5) 91.323 ms 91.788 ms po2.car03.dllstx6.theplanet.com (12.96.160.37) 89.571 ms
15 2a.5f.344a.static.theplanet.com (74.52.95.42) 92.695 ms 92.601 ms 91.871 ms

host

Used to look up the IP address of a fully qualified domain name.

[kmurray@radon ~]$ host simplehelp.net
simplehelp.net has address 74.52.95.42
simplehelp.net mail is handled by 10 aspmx.l.google.com.

dig

A more advanced DNS query tool.

[kmurray@radon ~]$ dig simplehelp.net a

; <<>> DiG 9.5.0-P2 <<>> simplehelp.net a
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25242 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;simplehelp.net. IN A ;; ANSWER SECTION: simplehelp.net. 86238 IN A 74.52.95.42 ;; Query time: 1 msec ;; SERVER: 192.168.1.1#53(192.168.1.1) ;; WHEN: Fri Oct 31 20:21:49 2008 ;; MSG SIZE rcvd: 48 This shows that the IP address of the server where simplehelp.net resides is 74.52.95.42. [kmurray@radon ~]$ dig simplehelp.net mx

; <<>> DiG 9.5.0-P2 <<>> simplehelp.net mx
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17277 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2 ;; QUESTION SECTION: ;simplehelp.net. IN MX ;; ANSWER SECTION: simplehelp.net. 86317 IN MX 10 aspmx.l.google.com. ;; ADDITIONAL SECTION: aspmx.l.google.com. 180 IN A 209.85.199.27 aspmx.l.google.com. 180 IN A 209.85.199.114 ;; Query time: 40 msec ;; SERVER: 192.168.1.1#53(192.168.1.1) ;; WHEN: Fri Oct 31 20:21:51 2008 ;; MSG SIZE rcvd: 98 This shows that mail for simplehelp.net is managed by aspmx.l.google.com and that aspmx.l.google.com has two IP addresses, 209.85.199.27 and 209.85.199.114. [kmurray@radon ~]$ dig simplehelp.net ns

; <<>> DiG 9.5.0-P2 <<>> simplehelp.net ns
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62957 ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 8 ;; QUESTION SECTION: ;simplehelp.net. IN NS ;; ANSWER SECTION: simplehelp.net. 70617 IN NS ns1.theplanet.com. simplehelp.net. 70617 IN NS ns2.theplanet.com. ;; ADDITIONAL SECTION: ns1.theplanet.com. 172785 IN A 70.86.61.133 ns2.theplanet.com. 172785 IN A 70.86.61.135 ns1.theplanet.com. 172785 IN A 70.86.61.134 ns2.theplanet.com. 172785 IN A 70.87.7.73 ns1.theplanet.com. 172785 IN A 70.87.7.71 ns1.theplanet.com. 172785 IN A 70.87.7.70 ns2.theplanet.com. 172785 IN A 70.86.61.136 ns2.theplanet.com. 172785 IN A 70.87.7.72 ;; Query time: 33 msec ;; SERVER: 192.168.1.1#53(192.168.1.1) ;; WHEN: Fri Oct 31 20:21:55 2008 ;; MSG SIZE rcvd: 209 This shows that DNS for simplehelp.net is handled by ns1.theplanet.com and ns2.theplanet.com. It also shows that these nameservers have multiple IP addresses.

iwlist

Get information from a wireless interface. (this one needs to be run as root)

[root@radon ~]# iwlist wlan0 scan
wlan0 Scan completed :
Cell 01 – Address: 00:10:18:90:20:DB
ESSID:”Coffee”
Mode:Master
Channel:6
Frequency:2.437 GHz (Channel 6)
Quality=96/100 Signal level:-37 dBm Noise level=-70 dBm
Encryption key:off
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
48 Mb/s; 54 Mb/s
Extra:tsf=0000017f91cc818a
Extra: Last beacon: 19ms ago
Cell 02 – Address: 00:11:50:6E:BC:3A
ESSID:”Belkin Traveler”
Mode:Master
Channel:1
Frequency:2.412 GHz (Channel 1)
Quality=88/100 Signal level:-44 dBm Noise level=-70 dBm
Encryption key:off
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
12 Mb/s; 24 Mb/s; 36 Mb/s; 9 Mb/s; 18 Mb/s
48 Mb/s; 54 Mb/s
Extra:tsf=0000017f13571181
Extra: Last beacon: 601ms ago
Cell 03 – Address: 00:17:9A:9C:14:28
ESSID:”Abby”
Mode:Master
Channel:6
Frequency:2.437 GHz (Channel 6)
Quality=50/100 Signal level:-79 dBm Noise level=-70 dBm
Encryption key:off
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
48 Mb/s; 54 Mb/s
Extra:tsf=00000000cbe364d6
Extra: Last beacon: 272ms ago
Cell 04 – Address: 00:18:D1:A8:E8:1A
ESSID:”Gigaset815″
Mode:Master
Channel:4
Frequency:2.427 GHz (Channel 4)
Quality=45/100 Signal level:-83 dBm Noise level=-70 dBm
Encryption key:on
IE: WPA Version 1
Group Cipher : TKIP
Pairwise Ciphers (1) : TKIP
Authentication Suites (1) : PSK
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 22 Mb/s
6 Mb/s; 9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s
36 Mb/s; 48 Mb/s; 54 Mb/s
Extra:tsf=0000015633a50e12
Extra: Last beacon: 571ms ago

This shows that my computer currently sees four different wireless networks in the area along with their names, channels, signal strength, and whether or not they are encrypted or open.

iwconfig

Like ifconfig, but for information about your wireless settings.

[kmurray@radon ~]$ /sbin/iwconfig
lo no wireless extensions.
eth0 no wireless extensions.
wlan0 IEEE 802.11bg ESSID:”Coffee”
Mode:Managed Frequency:2.437 GHz Access Point: 00:10:18:90:20:DB
Bit Rate=54 Mb/s Tx-Power=27 dBm
Retry min limit:7 RTS thr:off Fragment thr=2352 B
Link Quality=96/100 Signal level:-37 dBm Noise level=-70 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0

This shows that I’m currently connected to a wireless network called Coffee at 54 Mb/s and the signal strength is very good at 96%.

free

How much memory and swap space is used/free.

[kmurray@radon ~]$ free -m
total used free shared buffers cached
Mem: 2017 1659 357 0 42 1078
-/+ buffers/cache: 538 1478
Swap: 1983 0 1983

mount

Without any parameters, used to display mounted filesystems.

[kmurray@radon ~]$ mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw,noatime)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda2 on /boot type ext3 (rw,noatime)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
gvfs-fuse-daemon on /home/kmurray/.gvfs type fuse.gvfs-fuse-daemon(rw,nosuid,nodev,user=kmurray)
/dev/sda1 on /media/disk type fuseblk (rw,nosuid,nodev,allow_other,blksize=4096)

The interesting lines above are the ones indicating /dev/mapper/VolGroup00-LogVol00 on /, /dev/sda2 on /boot, and /dev/sda1 on /media/disk.

df

Show disk space usage.

[kmurray@radon ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 190M 19M 162M 11% /boot
tmpfs 1009M 1.1M 1008M 1% /dev/shm
gvfs-fuse-daemon 60G 44G 16G 74% /home/kmurray/.gvfs
/dev/sda1 31G 29G 2.5G 93% /media/disk

This shows that /dev/sda1 is almost full at 93%. Time to delete some files or upgrade the disk.

du

Show disk space usage in a directory.

[kmurray@radon ~]$ du -hc Documents/N95/tutorials/
380K Documents/N95/tutorials/done/bluetooth gps
480K Documents/N95/tutorials/done/putty fonts
1.4M Documents/N95/tutorials/done/gparted
1.1M Documents/N95/tutorials/done/remote filesystem gnome
1.4M Documents/N95/tutorials/done/gnome keyboard shortcuts
2.1M Documents/N95/tutorials/done/download helper
992K Documents/N95/tutorials/done/mconnection
1008K Documents/N95/tutorials/done/snap links
5.0M Documents/N95/tutorials/done/watermark gimp
1.2M Documents/N95/tutorials/done/gnome disk usage
208K Documents/N95/tutorials/done/enhanced calculator
544K Documents/N95/tutorials/done/putty/orig
964K Documents/N95/tutorials/done/putty
848K Documents/N95/tutorials/done/wifi ap
1.4M Documents/N95/tutorials/done/imap gmail
872K Documents/N95/tutorials/done/unsigned apps
884K Documents/N95/tutorials/done/taskman
1.1M Documents/N95/tutorials/done/downthemall
916K Documents/N95/tutorials/done/tethering bt
432K Documents/N95/tutorials/done/install apps wifi http
924K Documents/N95/tutorials/done/tethering usb
320K Documents/N95/tutorials/done/install stuff via bluetooth
416K Documents/N95/tutorials/done/smb4s60
1.1M Documents/N95/tutorials/done/download statusbar
308K Documents/N95/tutorials/done/install stuff via usb
828K Documents/N95/tutorials/done/nokia step counter
4.3M Documents/N95/tutorials/todo
52M Documents/N95/tutorials/
52M total

This shows that my N95 tutorials directory is using a total of 52M and the breakdown of each directory’s size.

head

Display the first few lines of a file.

[kmurray@radon ~]$ head /var/log/yum.log
Jun 13 22:30:33 Installed: bash-completion-20060301-10.noarch
Jun 13 22:31:31 Installed: yum-downloadonly-1.1.13-2.fc9.noarch
Jun 14 06:20:11 Installed: swfdec-0.6.6-1.fc9.i386
Jun 14 06:20:13 Installed: swfdec-gtk-0.6.6-1.fc9.i386
Jun 14 06:20:14 Installed: swfdec-mozilla-0.6.0-1.fc9.i386
Jun 14 06:34:41 Updated: libxml2-2.6.32-2.fc9.i386
Jun 14 06:34:41 Updated: dbus-glib-0.74-8.fc9.i386
Jun 14 06:34:42 Updated: libselinux-2.0.64-2.fc9.i386
Jun 14 06:34:43 Updated: e2fsprogs-libs-1.40.8-3.fc9.i386
Jun 14 06:34:43 Updated: audit-libs-1.7.4-1.fc9.i386

By default, head displays the first 10 lines. head -20 /var/log/yum.log would show the first 20 lines. You can use any arbitrary number.

tail

Display the last few lines of a file.

[kmurray@radon ~]$ tail /var/log/yum.log
Oct 30 23:19:56 Updated: 6:kdebase-libs-4.1.2-5.fc9.i386
Oct 30 23:19:57 Updated: 6:kdemultimedia-libs-4.1.2-2.fc9.i386
Oct 30 23:19:57 Updated: ksysguardd-4.1.2-6.fc9.i386
Oct 30 23:19:58 Updated: phonon-backend-xine-4.1.2-5.fc9.i386
Oct 30 23:20:08 Updated: kdebase-runtime-4.1.2-5.fc9.i386
Oct 30 23:20:11 Updated: kdebase-workspace-libs-4.1.2-6.fc9.i386
Oct 30 23:20:21 Updated: kdebase-workspace-4.1.2-6.fc9.i386
Oct 30 23:20:27 Updated: 6:kdebase-4.1.2-5.fc9.i386
Oct 30 23:20:39 Installed: kdebase-runtime-libs-4.1.2-5.fc9.i386
Oct 30 23:20:42 Updated: 6:kdemultimedia-4.1.2-2.fc9.i386

By default, tail displays the last 10 lines. tail -30 /var/log/yum.log would show the last 30 lines. You can use any arbitrary number.

grep

Search for a specific string within a file. (use zgrep to search a gzipped file)

[kmurray@radon ~]$ grep firefox /var/log/yum.log
Jun 19 16:19:33 Updated: firefox-3.0-1.fc9.i386
Jul 24 04:48:06 Updated: firefox-3.0.1-1.fc9.i386
Oct 18 03:13:52 Updated: firefox-3.0.2-1.fc9.i386

This shows that “firefox” is listed three times in this file.

type

Determine which directory a command is in.

[kmurray@radon ~]$ type ping
ping is hashed (/bin/ping)

Ahh, so ping lives in /bin.

locate

Locate any arbitrary file on the filesystem.

[kmurray@radon ~]$ locate *.rpm
/home/kmurray/Download/VirtualBox-2.0.2_36488_fedora9-1.i386.rpm
/home/kmurray/Download/VirtualBox-2.0.4_38406_fedora9-1.i386.rpm
/home/kmurray/Download/skype-2.0.0.72-fc5.i586.rpm
/home/kmurray/Download/synaptics-0.14.6-8.fc9.i386.rpm

I have four rpm files on this computer, all of them in /home/kmurray/Download.

file

Determine what type a file is.

[kmurray@radon ~]$ file /bin/ping
/bin/ping: setuid ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped

ping is a binary file.

[kmurray@radon ~]$ file Mythbusters.S02E04.flv
Mythbusters.flv: Macromedia Flash Video

Mythbusters.S02E04.flv is a video.

[kmurray@radon ~]$ file /var/log/yum.log
/var/log/yum.log: ASCII text

/var/log/yum.log is a plain text file.

history

Shows what commands you have been running on the command line.

[kmurray@radon ~]$ history

991 du -hc Documents/N95/tutorials/
992 ls /var/log/messages
993 tail /var/log/messages
994 ls
995 ls /var/log/
996 ls -lh /var/log/
997 tail /var/log/yum.log
998 head /var/log/yum.log
999 grep firefox /var/log/yum.log
1000 sudo su –
1001 cd src/
1002 ls
1003 tar -xvzf TrueCrypt\ 6.1\ Source.tar.gz
1004 cd truecrypt-6.1-source/
1005 ls
1006 less Readme.txt
1007 make
1008 less Readme.txt
1009 make
1010 make
1011 cd
1012 cd .VirtualBox/VDI/
1013 ls
1014 cat RawUSBsdb.vdi
1015 mv RawUSBsdb.vdi RawUSBsdb1G.vdi
1016 cp RawUSBsdb1G.vdi RawUSBsdb2G.vdi
1017 vim RawUSBsdb2G.vdi
1018 rm RawUSBsdb2G.vdi
1019 pwd
1020 VBoxManage internalcommands createrawvmdk -filename /home/kmurray/.VirtualBox
1021 cat RawUSBsdb2G.vdi
1022 cd

In order to discover which commands I use most so I could write this list, I used a combination of five different commands all piped together:

history | awk {‘print $2’} | sort | uniq | less

awk {‘print $2’} causes only the second column of the output to be displayed, sort puts everything in alphabetical order, uniq cuts out all the duplicates, and less causes the output to be displayed one screen at a time.

If you have any favourite bash commands that you would like to share, please tell us about them in the comments below.


If this article helped you, I'd be grateful if you could share it on your preferred social network - it helps me a lot. If you're feeling particularly generous, you could buy me a coffee and I'd be super grateful :)

buy a coffee for simplehelp.net


Home » Linux » 20 Super-Helpful Bash Commands

0 thoughts on “20 Super-Helpful Bash Commands”

  1. Usefull for the beginners is:

    apropos [keyword]

    [kmurray@radon ~]$ apropos Package
    rpm (8) – Red Hat Package Manager
    rpm2cpio (8) – Converts Red Hat Package (RPM) to cpio archive

    and

    man [manpage]

    [kmurray@radon ~]$ man rpm
    Name
    rpm – RPM Package Manager

    Synopsis

    Querying and Verifying Packages:
    rpm {-q|–query} [select-options] [query-options]
    rpm {-V|–verify} [select-options] [verify-options]
    rpm –import PUBKEY …

Leave a Comment

Your email address will not be published. Required fields are marked *