Showing posts with label Unix Commands. Show all posts
Showing posts with label Unix Commands. Show all posts

Wednesday 23 August 2017

Unix Shell Scripting Tutorial - page 1

Unix Shell Scripting Tutorial - Step 1

Question: How to create file shell script file?
echo "Hello World? How are you all?";



Question: How to make a file executable?
chmod a+rx script1.sh



Question: How to run execute file?
Just with name, you can execute.
script1.sh



Question: How to add comment in shell script files?
Use hash(#) before the line for comment.
chmod a+rx script1.sh



Question: How to use variable in shell script file?
name="Manoj"
echo "My Name is : $name"



Question: What is variable convention?
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).


Question: How to define read only variable?
readonly version="10.2"
echo "Version is : $version"



Question: How to unset variable?
name="Manoj"
unset name;



Question: What are different type of variable?
  1. Local Variables: A local variable is a variable that is present within the current instance of the shell.
  2. Environment Variables: An environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly.
  3. Shell Variables: A shell variable is a special variable that is set by the shell.



Question: How to use Escape Characters?
Use slash \ for Escape the character;

echo "Hello \"User\""; // Hello "User"
echo "Slash \\" ;// Slash \



Question: How to use expression in shell script file?
Use expr enclosed with caret (~), For Example
myvar=10;
result=`expr 10 * 5`
echo "Multiple: $result";

myvar=10;
result=`expr 10 + 5`
echo "Additon: $result";

myvar=10;
result=`expr 10 -5`
echo "Subtract: $result";

Question: How to use loop?
for i in 1 2 3 4 5
do
  echo "Number $i"
done



Question: How to use if else condition?
if [ 2 = 3 ]
then
  echo "Both are equal"
else
  echo "Both are not equal"
fi



Question: How to use if else elseif condition?
if [ 2 = 3 ]
then
  echo "Both are equal"
elseif [ 2 > 3 ]
  echo "2 is greater than 3"
  else
  echo "2 is less than 3"
fi



Question: How to read from user?
echo "What is your name?"
read name
echo "Your name is $name"


Question: How to search a folder with name?
find ~ -type d -name "*foldername*" -print




Thursday 28 January 2016

Unix questions and answers for Root user

Unix questions and answers for Root user

Question: How to login as super admin?
Once you login in console, the root account is not active by default.
To be active as super admin, you need to execute following commands.
sudo -su root



Question: How to change the permission of folder?
chmod 755 folder 



Question: How to change the permission of multiple folder?
chmod 755 foldername1 foldername2 foldername2


Question: How to change the permission of folder in Recursive?
chmod -R 755 folder 



Question: How to change the ownership of folder?
sudo chown user:group foldername



Question: How to change the ownership of multiple folder?
sudo chown user:group foldername1 foldername2 foldername2


Question: How to change the ownership of folder in Recursive?
sudo chown -R user:group foldername



Question: How to change the folder permission only not files?
find . -type d -exec chmod 751 {} \;



Question: How to change the folder permission only in Recursive not files?
find . -type d -exec chmod -R 751 {} \;



Question: Who is login is ssh?
whoami



Question: How to change the password of user?
passwd username
(Now it will prompt for new password and re-enter password)


Question: Give example of understanding from 0-7?
Number Read (R) Write (W) Execute (X)
0 No No No
1 No No Yes
2 No Yes No
3 No Yes Yes
4 Yes No No
5 Yes No Yes
6 Yes Yes No
7 Yes Yes Yes



Tuesday 27 October 2015

SVN interview questions and answers

SVN interview questions and answers


Question: What is SVN?
Subversion is an open source control system which is used to trace all the changes made to your source code.


Question: List out what all things should be stored in SVN repository?
  1. Source Code
  2. MySQL Queries
  3. Database Updates
  4. Project regarding important files
  5. Product Documents
  6. Minutes of Metting and Imp Email


Question: How to add SVN file?
svn add filename.php


Question: How to add SVN folder?
svn add foldername


Question: How to get Updates from SVN Repo
svn update


Question: How to delete file from SVN Repo?
svn delete filename


Question: How to get SVN Info?
svn info


Question: How to get SVN Log for a file?
svn log testFile.php


Question: How to check for modifications?
svn status -v PATH


Question: Difference between SVN commit and SVN update?
SVN commit:: Push (upload) the local changes to Repository.
SVN Update:: Get (download) the Repository changes files to local system.


Question: What is use of Revert in SVN?
Revert your local changes.
It have two types
1) Local Revert: It will delete all changes from files which you made after updates and before commit.
2) Repo Revert: Upload the changes to previous Repo.


Question: List out what is the best practices for SVN?
  1. Work from your own local work-space
  2. Commit small autonomous changes
  3. Use comment
  4. Validate the files you are committing, you actually changed
  5. Take Update before commit to the Repo.



Question: How to apply a patch in SVN ?
First we need to "Create Patch" by making changes and generating the .diff file. Then this .diff file can be applied to the new Code base using "Apply Patch".


Monday 15 June 2015

Linux and Unix rm command help and examples

rm (short-name for remove) is a command used to remove objects such as files, directories, device nodes, symbolic links etc from the file-system. rm also remove the reference of objects. It works on Linux as well on Unix.


Linux and Unix rm command help and examples

In Actual, rm does not remove any object (file, directories and nodes etc), It just unlink the object from filesystem. That's why file system space may still contain leftover data from the removed file.

Following are rm commands with example.
Delete a single file.
rm filename



Delete all files [USE WITH EXTENSIVE CARE].
rm *



Delete all files which have .txt extension.
rm *.txt 



Delete all files which start with "data" text.
rm data*



Delete a folder (folder must be empty).
rm -d foldername



Delete all recursively. (d-directory, r-recursive).
rm -dr foldername



Delete a file forcefully and will not prompt.
rm -f  filename



Delete the files with prompt (d-directory, r-recursive, i-Prompt before every removal).
rm -dri foldername



Delete the files  without prompt (d-directory, r-recursive, f-without prompt).
rm -drf foldername



Delete the files with without prompt (d-directory, r-recursive, f-without prompt)
rm -drf foldername



Delete the files with single prompt (d-directory, r-recursive, I-single prompt)
rm -drI foldername



Delete the files with details (d-directory, r-recursive, v-explaination)
rm -drv foldername



Delete a file which exist in another folder.
rm /mnt/data/home/project1/public_html/filename.txt 



When we are using multiple options in command, see following:
1) We can exchange the position of option  (Make no difference)
2) We can also give "-" again each option parameter.
3) To Delete a directory recusively, Following commands are valid and are same.
rm -dr foldername
rm -rd foldername
rm -d -r foldername
rm -r -d foldername




Tuesday 21 April 2015

Unix Commands Interview Questions and Answers for beginner

Unix Commands Interview Questions and Answers for beginner


Question: How to create shell script file?
Follow the simple steps to create first shell script file.
1. Login to putty console with your username/password.
2. Now go inside directory where you want to create the shell script files.
3. type following command to create text file.
vi test1
Now, you are in vi editor mode.
4. Type following script inside editor.
echo "Hello World"

5. Now get out of this vi editor using :wq
6. Now execute your file.
sh test1
7.It should print "Hello World" in output


Question: How can I concatenate two string variables in shell script file?
a="Hello World,"
b=" How r u?"
c=$a$b;
echo $c;#Hello World, How r u?



Question: Give me few examples of Grep command?
Example 1: Search text in file.
grep  "Hello" test.txt

"Hello" -is text, which you want to search.
test.text -It is file where u r searching.

Example 2:Search text in multiple files.
grep "Hello" test_*

"Hello" -is text, which you want to search.
test_* -It means search in all the files which start with test_

Example 3:Search text in file with insensitive.
grep -i "Hello" test.txt
"Hello" -is text, which you want to search.
test_* -It means search in all the files which start with test_

Example 4: Search text recursively in all files.
grep -r "Hello" *
-r: It is option used to search recursively in all sub folder.
"Hello" -is text, which you want to search.
* -Search in all current directory



Question: How to get all cron job for all users?
create empty text file and add following code in that file.
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done
Now, just execute the file
sh filename
It will list all the cron job for all user.


Question: What is difference between sh and bash?
Sh: Shell Command is a programming language described by the POSIX standard.
blash: In Starting, bash was also sh compatible but Now it is NOT due to in-valid POSIX shell.


Question: How can I "Reverse the order of lines" in a file?
tail -r text.txt



Question: How to download a file from SSH?
You can copy the file from server to another public directory, from there you can download with FTP/SFTP.
scp username@domain.edu:file.txt /path/to/dir

This command, put the file.txt to "path/to/dir", From this directory you can download.


Question: What is difference between Soft link and Hard link?
Soft Link: soft or symbolic is just a short cut to the original file. If we delete one or more soft link, nothing happens.
Hard link: It is multiple paths to the same file.


Question: How to set screen names with GNU screen?
screen -S foo



Question: How to get full path of a file?
readlink -f test.txt



Question: How to permanently set $PATH on Linux?
You need to add it to your ~/.profile file.
export PATH=$PATH:/path/to/directory



Question: How to list all the files/directory with permission?
 ls -l



Question: How to get the current folder path?
pwd



Question: How can I copy the output of a command directly into my clipboard?
cat file | xclip