Linux Commands (In Progress)


Find the no of CPUs in the system


	
	cat /etc/cpuinfo  == will show you all the CPU information.

Find out the memory in the system


	
	cat /etc/meminfo  == will show you all the memory information.									

Find out current free memory and total memory


	free -m				# prints the free memory in the system
										

List(ls)


	ls				# list all files in the current directory
	
	ls -ltr			# list all files and details in the current directory.
	
	ls -ltra		# list all files and details (including hidden files, files starting with '.') in current directory
	

grep


Used to search lines containing the text


	grep 'foo' file.txt					# lists all lines in file.txt which contain the word 'foo'
	
	grep 'foo' file.txt | grep 'bar'	# lists all lines in file.txt which contain both 'foo', and 'bar'.
	
	grep 'foo\|bar' file.txt			# lists all lines in file.txt which contain the word 'foo' or 'bar'.
										# in the above, '\' is the escape char, and '|' is the OR char.
										



tail


Shows the tail(last lines) of a file, mostly used in the viewing the latest logs of a running server log file.


	tail -100f file.txt					# prints the last 100 lines of a file.
	
	tail -100f file.txt | grep 'foo' 		# scans the last 100 lines and prints whichever has 'foo' in it.
	
	tail -100f file.txt | grep 'foo\|bar' 	# scans the last 100 lines and prints whichever has 'foo' or 'bar' in it.
		
	tail -100f file.txt | grep 'foo' | grep 'bar' 	# scans the last 100 lines and prints whichever has 'foo' and 'bar' in it.
	

df


disk filesystem command gives you the information about file system on disk.


	df -h					# prints the space occupied by various partitions in human readable format.

	df -k					# prints the space occupied by various partitions in kilobytes.

	df -m					# prints the space occupied by various partitions in megabytes.

	df -T					# prints the disk filesystem along with the type of the file system in the partition.
	

du


disk usage command gives you the disk usage, It is very useful for finding out which are the folders/files occupying the most space on disk.


	du					# prints the space occupied by each of the files in the current directory.

	du  /home/ng				# prints the space occupied by each of the files in the /home/ng directory.

	du -sk * | sort -n			# prints the directories along with space occupied by them in the decreasing order(very useful command)
	


creating tar


create a compressed file in tar.gz format


	# compresses directoryToTar and creates targetFile.tar.gz
	tar -cvzf targetFile.tar.gz directoryToTar	

extracting tar


tar.gz file is extracted by the following


	# converts targetFile.tar.gz to .tar file
	gunzip targetFile.tar.gz 	

	# expands the tar
	tar -xvf targetFile.tar

Finding a file


A file with a particular extension or name can be located using below.


	# finds a file with name ending with apk in the /home/user location
	find /home/user -type f -name "*.apk"

Detecting network problems with traceroute


traceroute command gives us the route which is taken when you connect to a remote machine.


How it works


	# give the route from my local machine to abc.com
	traceroute abc.com

How to find out the hardware related information


It is particularly useful for finding out if a machine is physical or virtual.


	# dmidecode spits out all the hardware related information
	dmidecode

	# for virtual machines, it will mostly include VMware virtual platform.
	dmidecode -s system-product-name

How to kill all the processes containing a name


Normally the processes are killed with kill -9 processId1 processId2... but sometimes it is not easy to collect the process ids. So, we can use the ps -ef to collect the processes, use awk to get the process ids and pass it to kill -9 using the xargs commands.


	# killing all the processes having word python.
	ps -ef | grep python | awk '{ print $2 }' | xargs kill -9

How to find out the memory shared by a VM with other machines


Normally, many virtual machines share the resources from a physical machine. So, although your VM may have 8 GB of memory in theory, it may be allocated to it, but only less will be available if other VMs on the same machines occupy more memory. For VMs using VMware Virtual Platform, we can use the below command to find out the memory shared by a VM with other VMs. This memory will not be availble to the machine at that time.


	# memory shared with others for VMware Virtual Platform
	vmware-toolbox-cmd stat balloon

How to find out the network load on a linux machine


'nload' command can be used to find out the network load on a machine. It gives the 'Current', 'Avg', 'Min', 'Max' network bandwidth usage for both incoming and outgoing.


	# network bandwidth usage/network load
	nload