Sunday, May 10, 2015

Linux Commands

Archive and Compression

Look inside an Archive:
tar -tzf jdk-7u79-linux-x64.tar.gz

Expand the Archive:
tar -xzvf jdk-7u79-linux-x64.tar.gz
tar -xjvf jdk-7u79-linux-x64.tar.bz2

Create an Archive of directory:
tar -cvf mahesh.tar /mahesh-dir :
-c create, v-verbose, f-file

Extract a .zip file.
unzip file.zip -d destination_folder

List file details in human readable form:
ls -lh

Disk usage of  the current directory:
du -sh
-s : summery

Create an Compressed Archive:
tar -czvf file.tgz /dir
tar -cjvf file.tar.bz2 /dir

Get type of file:
file mahesh.txt

Edit the bashrc file
sudo vim $HOME/.bashrc

Re-load the bashrc file
exec bash

Copy Recursively:
sudo cp -R jdk1.7.0_79/ /usr/811

Delete a non-empty directory
sudo rm -r folderName

Find out where the command is:
which uptime

Logout
Ctrl + D

Long Directory listing with last modified time:
ls -lt /
Reverse listing
ls -lrt /

Write to File:
ls > out.txt

Append to File:
ls >> out.txt
Output standard output
ls /811 1>> out.txt

1 - standard output (default)
2 - error output

Send both error and standard output to the same file:
ls /811 > out.txt 2>&1

Send the output to nowhere:
ls /811 > /dev/null 2>&1

Word count of a file:
wc -l out.txt

Print shell options:
set -o

Prevent overriding an existing file (appending is possible).
set -o noclobber
Override the safety:
ls /811 >| out.txt

Free Disk space
df -h 

Pipes - Send output of one command to input of another

No of lines returned by "ls" command (Un named pipes):
ls -l | wc -l

Named Pipes (Inter process communication - IPC)
Create the named pipe
mkfifo mypipe
ls -l > mypipe
wc -l < mypipe

Delete non empty directory:
rm -rf abc

Search text in a file:
grep error unit-test.log
grep error *.txt

Search with case insensitive way:
grep -i error *.txt

List only directories:
ls -l | grep "^d"
Inverts the search
ls -l | grep -v "^d"

Grep options:
-i - case insensitive
-c - count the occurrences
-l - show line number of occurrences
-v - inverts search

See only lines starting with "ABC"
grep '^server\b' /811
grep '\bserver\b' /811

Find all files in the current directory and its sub directory:
find /811
find .
find /811 -name abc
find /811 -name '*txt'

Find the content of files:
find /811 -name '*txt' -exec grep -l xyz {} \;
find -name FsShell.java -exec vim {} \;

!$ - represents the last argument

Ctrl + r - Reverse searches for your input



No comments:

Post a Comment