Useful Linux Commands:
1. How to find and kill a process in Unix:
find a process: ps -ef |grep tomcat
kill process: kill -9 (pid)
2.How will you figure out free available space in Uni
Ans: df command – Shows the amount of disk space used and available on Linux file systems.du command – Display the amount of disk space used by the specified files and for each subdirectory.
df -h
3. Help for any command
man command name like man df
4.How will you find files recursively that contains specific words in their contents
5.Search for a given string in a file (case in-sensitive search)
7.How to sort a file using unix command
sort -filename
for numeric sort
sort -n filename
reverse sort
sort -r filename
reverse sort without duplicate
sort -u -i filename
8. How to count number of lines in a file in unix terminal
filter for specfic port:
11.How will you copy a file from one Unix host to another Unix host
In the following example, transfer all files (/var/www/html) from remote server called server1 to another server called server2:
Linux Commands Part2
Java Interview Questions
1. How to find and kill a process in Unix:
find a process: ps -ef |grep tomcat
kill process: kill -9 (pid)
2.How will you figure out free available space in Uni
Ans: df command – Shows the amount of disk space used and available on Linux file systems.du command – Display the amount of disk space used by the specified files and for each subdirectory.
df -h
3. Help for any command
man command name like man df
4.How will you find files recursively that contains specific words in their contents
Search for a given string in all files recursively
$ grep -r "ramesh" *
5.Search for a given string in a file (case in-sensitive search)
$ grep -i "the" demo_file
6. How will you find files recursively that contains specific words in their filename
find . -maxdepth 1 -name "*string*" -print
It will find all files in the current directory (delete maxdepth 1
if you want
it recursive) containing "string" and will print it on the screen.
7.How to sort a file using unix command
sort -filename
for numeric sort
sort -n filename
reverse sort
sort -r filename
reverse sort without duplicate
sort -u -i filename
8. How to count number of lines in a file in unix terminal
wc sort.txt
output : 5 5 41 sort.txt
The three numbers produced in output correspond to number of lines,
number of words and number of bytes
9.How will you list nth column of a flat file in Unix ?
awk '{ print $2 $4 }' filename.txt
10.
How will you find a process using specific port in Unix and Windows ?
netstat -tulpn
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1138/mysqld
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 850/portmap
filter for specfic port:
netstat -tulpn | grep :80
11.How will you copy a file from one Unix host to another Unix host
In the following example, transfer all files (/var/www/html) from remote server called server1 to another server called server2:
scp -r user@server1:/var/www/html/ user@server2:/var/www/html/
I recommend using rsync command
which will only push or download updated files. It can copy locally,
to/from another host over any remote shell, or to/from a remote rsync
daemon. In this example, copy files from remote server called server1
into /backup directory:
rsync -avz -e ssh user@server1:/var/www/html /backup
Basic Linux/Unix Commands Part1
Command
|
Explanation
|
Commands
are entered at terminal
Unix is
case sensitive
~ Home
directory
_ and
hyphen allowed in file names, spaces
are not
allowed
hidden
files start with .
There
is no recycle bin in Unix. Files deleted cannot be restored.
every
user has a home directory denoted by ~
|
|
passwd
|
Change
password
|
ls
ls –l <directory>ls -l <directory>ls -t
ls -a
ls -la
|
List
files and subdirectories
All
information of files or subdirectories
/<fullpath>
Sort by
creation time
all
directories including hidden directory
. –
current
.. –
parent
|
cd
<directory>
cd ..
cd /
cd ~
pwd
|
Change
directory
parent
directory
root
directory
home directory
of user
present
working directory
|
man
<command>
|
documentation
for all unix commands
if
there is : at bottom next page of documentation press <spacebar> to get
out of documentation press q
|
cat
<filename>
more
<filename>
|
displays
contents of file, scrolls if file is large
display
page wise contents of a file
|
mkdir
<directoryname>
mv
<filename> <destination_directory>
cp
<source_file> <new_file>
cp -R
<source_dir> <destination_dir>
|
create
new directory
move a
file to another location
wildcards
can be used to move multiple files
copy
files
copy
all files and subdirectories recursively
|
rm
<filename>
rmdir
<directory>
rm -R
<directory>
|
There
is no recycle bin in unix and file/directory once deleted cannot be recovered
remove
a file
wildcards
can also be used
remove
the directory
Empty
directory cannot be removed
remove
everything in directory including the directory
|
ln
<source_file> <destination_file>
ln -s
<sourc_dir> <destination_dir>
ls
<linkeddirectory>
|
Link
file
Deleting
a linked file does not delete original file
Link a
directory. create a symbolic link
|
find
<wheretofind> -name <whattofind> -print
find
<wheretofind> -type d -print
find
<wheretofind> -type f –print
find
<wheretofind> -size +10M -print
|
Find a
file. Print results on screen
Get all
directories
Get all
files
Get all
files >10MB
|
Linux Commands Part2
Command
|
Explanation
|
history
!<line
no>
|
Get all
previous commands
Run a
command from history list
|
chmod
<permission> <filename>
|
Change
file/directory permissions
|
wc
<file_name>
wc -w
<file_name>
wc -l
<file_name>
ls | wc
-l
|
count
of lines, count of words, count of characters
count
of words
count
of lines
count
of files in a directory
|
head
<file_name>
tail
<file_name>
tail -f
<file_name>
|
first
few lines
last
few lines
watch
last lines of a file as it grows
|
clear
|
Clear screen
|
grep
<word> <filename>
grep -c
<word> <filename>
grep -i
<word> <filename>
grep -v
<word> <filename>
grep
^<word> <filename>
grep
<word>$ <filename>
grep a.
<filename>
grep a*
<filename>
grep
a[bc] <filename>
|
Searh a
word in file
count
of times a word occurs
case
insensitive search
lines
which do not have the word
line
starting with word
line
ending with word
. means
any character. word a followed with any character
occurrence
of a followed with any number of characters
searches
for words ab or ac in file
|
diff
<filename1> <filename2>
|
Compare
contents of two files
|
tar
–cvf <tar_file_name> <contents>
tar
–tvf <tar_file_name>
tar
–xvf <tar_file_name>
|
Create
a tar file
Get
list of tar file contents
Extract
contents of tar file
|
env |
more
|
get
list of all environment variables
|
echo
$SHELL
echo
$PATH
|
get
name of shell
get
PATH variable contents
|
which
<command>
|
Get the
location of file for command
|
ps
|
Get
list of all processes
|
ps aux
| grep <username>
|
Get
list of all process running for all users and then sort the processes for a
user
|
kill
<processId>
kill -9
<processId>
|
Kills a
process
Kills a
process and all its child processes
|
vi
editor commands
|
|
<ctrl>+o
<ctrl>+x
vi
<filename>
i
<ESC>
x
:wq
:q
|
Save
Exit
Open an
existing or new file in vi editor
Go to
insert mode
Get out
of insert mode
Delete
a character
Write
and quit
Quit
|
Java Qestions
- How do you create an Object instance without using new
-
Using newInstance method of Class class
Eg: Class clas = Class.forName("NewClass");
NewClass obj = (NewClass) clas.newInstance();
- Class.forName() loads the class and to create an object, we need to use newInstance() method of Class class.
or using class loader: getClass().getClassLoader().loadClass("NewClass").newInstance();
- Using Object Deserialization
Eg : ObjectInputStream objStream = new ObjectInputStream(inputStream );
NewClass obj = (NewClass ) inStream.readObject();
-
Using clone() of java.lang.Object
Eg: NewClass obj = new NewClass();
NewClass obj2 = obj.clone(); - Creates copy of an existing object
-
Using newInstance method of Class class
Eg: Class clas = Class.forName("NewClass");
NewClass obj = (NewClass) clas.newInstance();
- Class.forName() loads the class and to create an object, we need to use newInstance() method of Class class.
or using class loader: getClass().getClassLoader().loadClass("NewClass").newInstance();