Command Line Tips and Tricks

To find the largest file:

  • Search under /home for files greater than 100MB, where c stands for bytes.

# find /home -size +104857600c

  • This following command will output all the files under /usr/share which is more than 50MB in the format “file name : file size”

# find /usr/share -type f -size +50000k -exec ls –lh {} \; | awk ‘{ print $9 “: ” $5 }’

  • The below script will display every directory under the directory given as an argument to this script with its size sorted by the largest at the bottom.

Add the following lines to a file and give the file execute permission,

#!/bin/bash

du -sm $(find $1 –maxdepth 1 –xdevtype d)|sort -g

Then do a chmod +x <filename> to assign execute permission

  • The following two commands helps you in detecting the empty files.

# find . -size 0c or,

# find . -empty 

To Fix a Corrupt RPM database:

Execute the below commands as root user

# rm /var/lib/rpm/_db*

# rpm -v rebuilddb

Checking for SELinux :

# getenforce

This command will tell whether the selinux is in Enforcing, Permissive or Disabled mode

# ls -Z

This command helps in identifying the files and folders against selinux details.

# echo 0 > /selinux/enforcing

This will helps to change the selinux from enforcing to permissive temporarily (echo 1 > /selinux/enforcing to set it back to enforcing)

To disable the selinux permanently open the following file in your favourite editor and change the SELINUX context to disabled

# vi /etc/selinux/config

SELINUX=disabled

Increase the semaphore count in Linux:

# ipcs -a

—— Shared Memory Segments ——–
key shmid owner perms bytes nattch status
0x00000000 0 gdm 600 393216 2 dest
0x00000000 32769 gdm 600 393216 2 dest
0x00000000 65538 gdm 600 393216 2 dest
0x00000000 98307 gdm 600 393216 2 dest

—— Semaphore Arrays ——–
key semid owner perms nsems
0x00000000 0 root 600 1
0x00000000 65537 root 600 1

—— Message Queues ——–
key msqid owner perms used-bytes messages

# sysctl -a |grep sem
kernel.sem = 250 32000 32 128

# sysctl -w kernel.sem=300
kernel.sem = 300

# sysctl -a |grep sem
kernel.sem = 300 32000 32 128

We need to edit the /etc/sysctl.conf file to make this changes persistent across reboot.

The quick way to find the hardware model of your server:

# dmidecode -t 1

  • An example output,

# dmidecode 2.11
SMBIOS 2.4 present.

Handle 0x0001, DMI type 1, 27 bytes
System Information
Manufacturer: VMware, Inc.
Product Name: VMware Virtual Platform
Version: None
Serial Number: VMware-56 4d 42 b0 ee 0c 84 de-af 2d 47 d3 95 40 70 ef
UUID: 564D42B0-EE0C-84DE-AF2D-47D3954070EF
Wake-up Type: Power Switch
SKU Number: Not Specified
Family: Not Specified

  • Another example output,

# dmidecode 3.0
Scanning /dev/mem for entry point.
SMBIOS 2.4 present.

Handle 0x0100, DMI type 1, 27 bytes
System Information
Manufacturer: Xen
Product Name: HVM domU
Version: 4.2.amazon
Serial Number: ec2c4ad4-1afd-abba-aa66-74e921a4a3b7
UUID: EC2C4AD4-1AFD-ABBA-AA66-74E921A4A3B7
Wake-up Type: Power Switch
SKU Number: Not Specified
Family: Not Specified

The Product Name will tell you server details like its is VMWare, HP Proliant, Dell Poweregde etc.

To get the details of the IP Address and other N/W aspects:
# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:0C:29:40:70:EF
inet addr:192.168.213.155 Bcast:192.168.213.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe40:70ef/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:594 errors:0 dropped:0 overruns:0 frame:0
TX packets:530 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:54336 (53.0 KiB) TX bytes:127273 (124.2 KiB)

eth1 Link encap:Ethernet HWaddr 00:0C:29:40:70:F9
inet6 addr: fe80::20c:29ff:fe40:70f9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:2700 (2.6 KiB)

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:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:240 (240.0 b) TX bytes:240 (240.0 b)

the typical ifconfig command gives you info about all the interfaces configured in the system

the short and simple method you can use is the ip command as below,

# ip addr list
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:40:70:ef brd ff:ff:ff:ff:ff:ff
inet 192.168.213.155/24 brd 192.168.213.255 scope global eth0
inet6 fe80::20c:29ff:fe40:70ef/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:40:70:f9 brd ff:ff:ff:ff:ff:ff
inet6 fe80::20c:29ff:fe40:70f9/64 scope link
valid_lft forever preferred_lft forever

# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
192.168.213.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

This will provode the kernel routing table and so is the one below,

# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
link-local * 255.255.0.0 U 1002 0 0 eth0
192.168.213.0 * 255.255.255.0 U 0 0 0 eth0

# ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
MDI-X: Unknown
Supports Wake-on: d
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes

the ethtool provides the details of all your network interface.

# mii-tool eth1
eth1: negotiated 100baseTx-FD, link ok

this command also throw some brief info

Setting a Temporary IP:

#ifconfig eth0 192.168.213.415 netmask 255.255.255.0

#route add default gw 192.168.213.1

This settings will be lost the moment you reboot your system

Run a command repeatedly and display the output:

#watch -d ls -l

by default the program run every two seconds and lists the contents of the directory where the command executed

another way is a simple while loop

#while true; do ls -l; done

Information on a command:

# which ping
/bin/ping

the absolute path of a command

# rpm -qf /bin/ping
iputils-20071127-16.el6.x86_64

which pakage provides you this command

Add the words horizontally to a file:

To paste the contents of two files horizontally we can take the help of paste command,

# cat command
ping
ping
ping
ping

# cat ip
192.168.213.153
192.168.213.100
192.168.213.45
192.168.213.61

# paste -d ” ” command ip > output

-d ” ” will seperate the contents by a single space instead of a tab which is default.

# cat output
ping 192.168.213.153
ping 192.168.213.100
ping 192.168.213.45
ping 192.168.213.61

Redirecting the top command output to a file:

# top -b -n2 -d5 > /tmp/top.out

This command will run top 2 times and wait 5 seconds between each output.

Create a directory with different permission:

# mkdir /root/mydir -v -m 1777
mkdir: created directory `/root/mydir’

# ls -lhd /root/mydir/
drwxrwxrwt. 2 root root 4.0K Aug 9 20:35 /root/mydir/

This can be used instead of creating a directory and use chmod command to change the permissions

Create multiple files or directories:

#touch myfile{1..5}.txt

# ls my*

myfile1.txt  myfile2.txt  myfile3.txt  myfile4.txt  myfile5.txt

# mkdir dir{1..4}

Commands cat and tac:

# cat file
This is Linux
This is Ubuntu

# tac file
This is Ubuntu
This is Linux
[root@docker ~]#

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.