Unix Commands cheat-sheet

This page lists down common unix commands I use in my day to day work.

Environment Variables Commands


back to top

How to find Unix version

-- os specific information
uname -a

uname -m 

-- show kernel version
uname -r

Although not all versions of ubuntu supports LSB(Linux Standard Base) specification, it contains tools which can help us find it’s specific version.

lsb_release -a

lsb_release -c

How to print all env variables

env	

How to set environment variables

export PATH=${PATH}:${HOME}/bin
	
set JAVA_HOME
export PATH=${PATH}:/usr/java/jdk1.5.0_07/bin
	
export ANT_HOME=/path/to/ant/dir
export PATH=${PATH}:${ANT_HOME}/bin:${JAVA_HOME}/bin

How to set system wide shell options

In unix world, user can login using 2 ways:

  • An interactive login shell is started using /bin/login after a successful login by reading the /etc/passwd file. Shell reads /etc/profile and its private equivalent ~/.bash_profile upon startup.
  • An interactive non-login shell is normally started at the command-line using a shell program (e.g., [prompt]$/bin/bash) or by the /bin/su command. This type of shell invocation normally copies the parent environment and then reads the user’s ~/.bashrc file for additional startup configuration instructions.

By default /etc/profile file act as a system-wide profile file for the Bash shell. You can force setting using this file for all user. However, it is recommended that you use /etc/profile.d file under CentOS / Fedora / Redhat Enterprise Linux. For all other distribution edit /etc/profile file.

How to set JAVA_HOME and PATH settings for all users

Create /etc/profile.d/java.sh file. Open the file and enter following code

 #!/bin/bash
export JAVA_HOME=/opt/jdk1.5.0_12
export PATH=$PATH:$JAVA_HOME/bin

Save and close the file. Setup executable permission:

chmod +x /etc/profile.d/java.sh

Users, Groups, File Permissions Commands


back to top

How to print info for logged in user

whoami

id

echo $LOGNAME

echo $USERNAME

How to change file permissions

-- view file permissions
ls -l file.txt

-- Set "Owner - read, write; group - read only; others - read only"
chmod 664 file.txt

-- Set "Owner - read, write; group - read, write; others - read, write only"
chmod 666 file.txt

user,group,others # Permissions
rw——- (600) Only the owner has read and write permissions.
-rw-r–r– (644) Only the owner has read and write permissions; the group and others have read only.
-rwx—— (700) Only the owner has read, write, and execute permissions.
-rwxr-xr-x (755) The owner has read, write, and execute permissions; the group and others have only read and execute.
-rwx–x–x (711) The owner has read, write, and execute permissions; the group and others have only execute.
-rw-rw-rw- (666) Everyone can read and write to the file.
-rwxrwxrwx (777) Everyone can read, write, and execute.

How to change owner

-- set Mukesh owner of the file
chown Mukesh file.txt

-- Recursively grant ownership of the directory and all files and subdirectories to Mukesh
chown -R Mukesh /home/hadoop

How to find all files in a directory with permissions and Change File Permissions recursively

find /home/user/demo -type f -perm 777 -print -exec chmod 755 {} \;

How to list down Unix groups for a unix user

groups ishu

How to enable debugging in shell scripts

sh -xv scrfile

If you want to run a script with debugging but you don’t want to edit the script file, you can also start the shell explicit from the command line and give the options there.

How to find whether any process is running or not in Unix

ps -ef | grep -i cron

How to tar untar all files under a directory in unix 

tar cvf docker.tar *
 
tar xvf docker.tar *

Modify profile file in unix to set up host name and present working directory on primary prompt

PS2="`hostname`:";    
PS1=$PS2'$PWD> '
export PS1

Using Mailx Command

echo "This is the body."| mailx -s "Email architecture Test" suniel@gmail.com

will display the pathnames of all files in the current directory and all subdirectories.  The commands

find . -print
find -print
find .

Find all files of type pptx under current directory

find . -type f  -name '*.pptx'

Find files which got changed in last 10 minutes

find / -mmin -10

Delete files from a directory

find $explodeDir -name "*.ear" -exec rm -fr '{}' \; 2>/dev/null

Delete files older than 30 days from given directory

find . -type f -mtime +30 -exec rm -f {} \;

Split the file

split -b 10k data.file

-- Ensure split file names starts with split_file
split -b 10k data.file -d -a 4 split_file

-- Split by No. of lines
split -l 10 data.file

Disk space Troubleshooting


back to top

How to see free and used space

df -h .

df -h /users/dir

Sort the underlying directories by space usage

du -k | sort -nr | more

du -sh *

du --si
ls -lh

Find 10 largest files in /user/hadoop

du -ak /user/hadoop | sort -nrk 1 | head
ls -lrt | grep ^l

How to find disk usage information for a directory in unix

df -h DIRECTORYNAME
df -h /users/android

df -H

Process Troubleshooting


back to top

top

Ps -ef 

Ps -ef | grep username

Find PID for a process and Kill The Process

ps -ef | grep sonidevName

kill -9 18244

How to see all Environment variables for a process

ps -ef | grep 'process_name'
cat /proc/$PID/environ

cat /proc/112222/environ | tr '\0' '\n'

Showing environment variables for a process

ps -eo cmd e

Find Out Current Working Directory Of a Process

pwdx 11728
ls -l /proc/11728/cwd

Find Out Owner Of a Process

ps aux | grep 11728
cat /proc/3813/environ
ps -eo pid,user,group,args,etime,lstart | grep '11728'

Find service for a given port

grep port /etc/services
grep 2600 /etc/services

Find executable from given process Id

ls -l /proc/3813/exe
galaxy@solara-dev[~]$ ls -l /proc/11728/exe
lrwxrwxrwx 1 galaxy galaxy 0 May 18 12:15 /proc/11728/exe -> /home/galaxy/environment/jre1.8.0_20/bin/java*

Network Troubleshooting


back to top

Display IP address

Ifconfig iface_name

List all open ports / Active internet Connections

lsof -i
lsof -i:8800
ps pid

lsof -i :portNumber
lsof -i tcp:portNumber
lsof -i udp:portNumber
lsof -i :80
lsof -i :80 | grep LISTEN

Find PID for given port

Find out the processes PID that opened tcp port 7000, enter:
fuser 7000/tcp
3813

Find service for a given port

grep port /etc/services

grep 2600 /etc/services

Find executable from given process Id

	ls -l /proc/3813/exe

Opened Ports from current machine

	lsof -i | grep ":[0-9]\+->" -o | grep "[0-9]\+" -o | sort | uniq

Admin Commands


back to top

Add new user

sudo adduser Mukesh

-- force user to change password
sudo passwd -e Mukesh

-- Allow login without password
sudo passwd -d Mukesh

-- Provide user sudo access
sudo adduser Mukesh sudo

Find CPU Info

-- displays information about all cores and processors
cat /proc/cpuinfo

Find shared library dependency

ldd ./p4

List all users

cat /etc/passwd

Create New Group

$ sudo groupadd webappadmin
	

$ sudo usermod -a -G webappadmin jobcpe
$ sudo usermod -a -G webappadmin nginx
$ sudo usermod -a -G webappadmin jenkins

Make changes for all the users

/etc/profile.d

http://bash.cyberciti.biz/guide//etc/profile.d

Run commands at regular intervals / frequency

watch command is used to run any designated command at regular intervals.

watch -d 'COMMANDS'

watch -n 5 free -m

Find files containing given pattern

grep -rnw . -e "java.protocol.handler.pkgs"

Version History


  Date Description
2015-08-15    Initial Version  
2016-03-04    Added “Create Branch commands”