Useful Unix Commands and Tips - Part 4

Let us learn about tar and ps in this part of the Unix series:
tar: tar is derived from Tape Archive when magnetic tapes were used to archive files. tar utility is useful in creating single archive file from multiple files maintaining owners, file types, permissions etc. It is mainly used to create backups and copy files from one computer to other. Let us see few examples:
1) To create tar file from a dir:
tar -cvf <tar file name> <Source dir name> #Here, c stands for compress, v for verbose, f - file name of tar file.
tar -cvf scripts.tar /opt/oracle/product/admin/scripts
2) To extract files from tar file:
tar xvf scripts.tar
3) tar doesn’t compress files, it only creates archive file. It can be compressed using gzip utility.
gzip scripts.tar # It creates a compressed file scripts.tar.gz
gzip -d scripts.tar.gz  # to unzip
4) We can archive and compress with single command using ‘z’ option:
tar -zcvf scripts.tar.gz scripts # Compress
tar -zxvf scripts.tar.gz # Uncompress
5) To exclude files from tar archive:
tar -zcvf scripts.tar.gz --exclude='*.html' --exclude='*.txt' scripts # Excluding html and txt files.
6) To retain permissions and ownership are preserved using the p option: 
tar cpf - .|(cd /tmp/mc; tar xvpf -)
7) To list contents of a tar gz file:
tar -ztvf my-data.tar.gz
tar -ztvf my-data.tar.gz ‘search-pattern’
8) Number of files and directories in tar.gz file. After taking backup, we can verify whether all the files are backed or not using above command.
tar -ztvf test1.tar.gz | wc -l
9) To unzip or extract gz file: 
gunzip file.gz
ps: ps (short for process status) Report snapshot of current processes. 
1) List all processes in the machine:
ps -ef
2) To find orphaned and zombie processes:
ps -ef | grep defu
3) To find number of threads in a process:
ps huH <pid> | wc -l
4) To print individual thread of a process:
ps -mp {processid} -o THREAD
5) To find total number of processes and sub-processes running in unix/linux:
ps h -Led -o user | sort | uniq -c | sort -n | awk '{s=s+$1}END{print s}'
6)To find cpu utlization by thread:
ps -Le -o pid,user,s,lwp,pcpu | grep <PID>
7) To sort process by memory used and average cpu used since start of the process:
ps -eo pid,pcpu,user,rss,args --sort rss,pcpu
8) To find cpu utilization of individual threads:
ps -mp 113202 -o THREAD
9) To display process hierarchically: 
ps -efaux
10) To show running processes in tree format:
pstree -p      

Comments

Popular posts from this blog

HOW WE REDUCED SOA OSB PROVISIONING FROM 4 DAYS TO 4 HOURS

NOT ABLE TO START RABBITMQ CLUSTER: CANNOT DECLARE A QUEUE ‘~S’ ON NODE ‘~S’: ~255P

SOA SUITE 12.2.1.4 INSTALLATION: GOT EXCEPTION WHEN AUTO CONFIGURING THE SCHEMA COMPONENT(S) WITH DATA OBTAINED FROM SHADOW TABLE