Pages

Sunday, October 8, 2017

Linux Command Line

Terminal usage:

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ ls -lh
total 67992
-rw-r--r--@  1 nawlekha  staff    19M Mar  2  2017 CodeSchool-TryPython.pdf
-rw-r--r--   1 nawlekha  staff   210B Sep 21 18:01 LEGB.py
drwxr-xr-x  19 nawlekha  staff   646B Feb 28  2017 PyATS online sessions/
drwx------@ 45 nawlekha  staff   1.5K Jun 19 10:25 Python 3 for begineers

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ ls -l /var/log
total 73464
drwxr-xr-x   3 root             wheel             102B Apr 13 21:14 Bluetooth/
-rw-r--r--@  1 root             wheel              12B Apr 13 21:13 CDIS.custom
drwxr-xr-x   2 root             wheel              68B Aug  6  2016 CoreDuet/

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ ls -l -a -h /var/log or ls -lah
total 73464
drwxr-xr-x  62 root             wheel             2.1K Oct  5 17:37 ./
drwxr-xr-x  28 root             wheel             952B Jul 21 17:29 ../
drwxr-xr-x   3 root             wheel             102B Apr 13 21:14 Bluetooth/
-rw-r--r--@  1 root             wheel              12B Apr 13 21:13 CDIS.custom
drwxr-xr-x   2 root             wheel              68B Aug  6  2016 CoreDuet/

Ctrl + A = move to beginning
Ctrl + E = move to end
Clear => clear terminal

Ctrl + U => deletes from cursor to beginning of line

Ctrl + K => deletes from cursor to end of line

Ctrl+Shift+C => Copy to clipboard

Ctrl+ Shift+V => Paste to clipboard

nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATS$ man ls

~ means home folder

## to access folder name having space
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATS$ cd Python training_doc
-bash: cd: Python: No such file or directory
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATS$
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATS$
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATS$ cd Python\ training_doc
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATS/Python training_doc$

## to list all folders under directory
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATS$ ls -R Python\ training_doc
Foundations of Python Network  Programming, 3rd Edition.pdf  byte_of_python.pdf
PY_diff.txt                                                  dive-into-python3.pdf
Python  Scripting_new.pptx                                   thinkpython.pdf
apr-24.txt

## to represent parent directory of current directory
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATS$ cd ..
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop$

## to move back between folder
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATS$ cd -
/Users/nawlekha/Desktop/pyATs
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATs$

## this will take you from wherever you are to home folder
nawlekha@NAWLEKHA-M-Q1GZ:~/Desktop/pyATs$ cd
nawlekha@NAWLEKHA-M-Q1GZ:~$

## to create and remove directory
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ mkdir new_folder
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ rmdir new_folder

## in order to remove, folder it should be empty
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ mkdir new_folder
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ mkdir new_folder/hello
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ rmdir new_folder
rmdir: new_folder: Directory not empty

for this we can do rm -r ## delete recursively

## move file to other dir and also to rename files
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ vi forfun.txt
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$

nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ mv forfun.txt new_folder/
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ ls for*
ls: for*: No such file or directory

nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS/new_folder$ ls
forfun.txt  hello/

nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ mv new_folder/forfun.txt new_folder/funny.txt
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ cd new_folder/
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS/new_folder$ ls
funny.txt  hello/

## to move folder to current directory
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ mv new_folder/funny.txt .

nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ ls fu*
funny.txt

## to move all .txt files to new folder
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ mv *.txt new_folder/

## to move back all files to current dir
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ mv new_folder/* .

## use of ? to delete files. this will delete file name with funny followed by any single character
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ cp funny.txt funny1.txt
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ cp funny.txt funny2.txt
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ rm funny?.txt

nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ ls fun*
funny.txt

## find all files in current directory search based on name
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ find . -name "*.txt"
./apr-24.txt
./funny.txt
./Python 3 for begineers/12.3/employees.txt
./python training.txt
./Python training_doc/apr-24.txt
./Python training_doc/PY_diff.txt
./python_training_examples/one.txt
./python_training_examples/output.txt

nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ find . -name "PyATS*"
./PyATS online sessions

nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ find . -name "*folio*"
./portfolio.csv
./portfolio1.csv

## to search in different folder
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ find ~/ -name "*folio*"
/Users/nawlekha//Desktop/pyATS/portfolio.csv
/Users/nawlekha//Desktop/pyATS/portfolio1.csv
find: /Users/nawlekha//Library/Caches/com.cisco.Jabber/com.apple.opencl: Permission denied
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$

## to gain root permission we can use sudo. For first time it will ask for password but there is some grace time within which it won’t prompt for password

nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ sudo ls /root
Password: ## user password
ls: /root: No such file or directory
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ sudo ls
.DS_Store missing.csv
CodeSchool-TryPython.pdf new_folder

## to give up privilege
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ sudo -k

## if you want to work as root
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pyATS$ su root
Password:

Now prompt will change to #

To switch back to $ prompt type exit

## Octal file permissions
file rwxrwxrwx
first rwx represent user or owner of the file
2nd rwx represent group the owner is the member of
3rd rwx represent all other group the owner is not the member of

user rwx 421 = 7
group rx  41 = 5
Other r    4  = 4

## we can change permission of file using chmod command
two ways to do so
-> octal (e.g. 755 644 777)
-> symbolic (e.g. a=r, g+w, and o-x)

user = u ,
to provide user read, write and execute permission: chmod u+rwx
group = g,
to provide group read permission: chmod g=r
others = o,
to remove read, write and execute from others : chmod o-rwx
All = a

+ adds permission; - removes permission
= adds permission but removes others

Comparision of octal and symbolic values

777 or a+rwx : rwxrwxrwx
755 or u+rwx,g=rx,o=rx : rwx-xr-xr
644 or u=rw,g=r,o=r : rw-r- -r- -
700 or u=rwx,g-rwx,o-rwx : rwx- - - - - -

## to run executable file on command line
./file.sh

==> executable means runs on its own without having to be loaded by other program first

nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ ./LEGB.py
-bash: ./LEGB.py: Permission denied
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ ls -lh LEGB.py
-rw-r--r--  1 nawlekha  staff   210B Sep 21 18:01 LEGB.py
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ python3 LEGB.py
outer x
outer x
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$

## still i was able to run file using python3 because it is not run on itself. it is run by other program

## let’s provide read, write and execute permission to user
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ chmod 744 LEGB.py
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ ls -lh LEGB.py
-rwxr--r--  1 nawlekha  staff   210B Sep 21 18:01 LEGB.py*
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ ./LEGB.py
./LEGB.py: line 4:
LEGB
Local, Enclosing, Global, Built-in
: command not found
./LEGB.py: line 12: syntax error near unexpected token `('
./LEGB.py: line 12: `def outer():'

## let’s remove read permission for user
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ chmod 244 LEGB.py or chmod u-r LEGB.py
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ cat LEGB.py
cat: LEGB.py: Permission denied
## let’s provide read permission again
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ chmod 755 LEGB.py
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ cat LEGB.py
'''
LEGB
Local, Enclosing, Global, Built-in
'''

#import builtins
#print(dir(builtins))


#x = 'global x'

def outer():
x = 'outer x'

def inner():
#x = 'inner x'
print(x)
inner()
print(x)

outer()

## when a user create a file in home directory, it starts with 664 permission
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ touch newfile
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ ls -lh newfile
-rw-rw-r—  1 nawlekha  staff     0B Oct  5 19:46 newfile

## we can change the ownership of the file
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ sudo chmod root newfile
## after changing ownership we won’t be able to write to it
"newfile" [readonly] 1L, 8C

## lets move back the ownership. Now i am able to write to file
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ sudo chown nawlekha newfile
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ vi newfile

## for changing group ownership we will use chgrp

## command to print whatever you give to it
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ echo "hello"
hello

## use of pipeline
nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ echo "hello" | wc
       1       1       6
==> one line of text, one word and six character

nawlekha@NAWLEKHA-M-Q1GZ:~/DEsktop/pYATS$ echo "hello world from the command line" | wc
       1       6      34

## Cat 
concatenate and print files

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ cat comment.py

## to see first 10 and last 10 lines of a file
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ head comment.py
print('Hi i am Nawraj')

if False:
    print('This code will not execute')


if True:
    print('Hello nawraj')

"""
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ tail comment.py

#or

if False: """
def sum(x,y):
    return(x+y)

print(sum(1,2))
"""

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$

## we can also customise it.  this will print 1st 5 lines of a file
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ head -n 5 comment.py
print('Hi i am Nawraj')

if False:
    print('This code will not execute')

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$

## last 3 lines of file
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ tail -n 3 comment.py
print(sum(1,2))
"""
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ cat comment.py | cat -n | tail -n 5
    21     return(x+y)
    22
    23 print(sum(1,2))
    24 """
    25
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ cat comment.py | tail -n 5 | cat -n
     1     return(x+y)
     2
     3 print(sum(1,2))
     4 """
     5

## to view large files
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ less comment.py
press q to quit

## to filter piece of information
grep is used to search files for text that matches a given pattern

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ grep "sum" comment.py
def sum(x,y):
print(sum(1,2))
def sum(x,y):
print(sum(1,2))
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ grep -n "sum" comment.py
11:def sum(x,y):
14:print(sum(1,2))
20:def sum(x,y):
23:print(sum(1,2))
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$

## grep is case sensitive
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ grep -n "Sum" comment.py
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$

## for case in-sensitive we can use -i
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ grep -i "nawraj" comment.py
print('Hi i am Nawraj')
    print('Hello nawraj')
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$

## to omit lines which we don’t want to see
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ grep -vi "sum" comment.py
print('Hi i am Nawraj')

if False:
    print('This code will not execute')


if True:
    print('Hello nawraj')

"""
    return(x+y)

"""

#or

if False: """
    return(x+y)

"""

## for regex we can use -E. To fetch he occurrence of letter "hijk"
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ grep -E "[hijk]" comment.py
print('Hi i am Nawraj')
if False:
    print('This code will not execute')
if True:
    print('Hello nawraj')
print(sum(1,2))
if False: """
print(sum(1,2))

## 6 or more any word character
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ grep -E "\w{6,}" comment.py
print('Hi i am Nawraj')
    print('This code will not execute')
    print('Hello nawraj')
    return(x+y)
    return(x+y)
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$

## awk and sed
tools to extract or modify text from a file or stream

## print values from 2nd column
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ awk '{print $2}' apr-24.txt

## to sort output based on numbers
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ awk '{print $2 "\t" $1}' apr-24.txt | sort -n

## while awk is used for extracting data, sed is used for modify data
#every occurrence of you is replaced by him
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ sed s/you/him/ funny.txt
hello how r him.
i am looking for him.

## sort
#by default sort is based on the 1st character.
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ sort funny.txt
ahh !! i forgot to call you.
hello how r you.
i am looking for you.

# we can also do sort based on columns and numbers
## sort based on 2nd column numbers
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ sort -k2n simple.txt

### to remove duplicate entry
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ cat funny.txt
hello how r you.
i am looking for you.
ahh !! i forgot to call you.
i am looking for you.
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ sort -u funny.txt
ahh !! i forgot to call you.
hello how r you.
i am looking for you.

## to print file in reverse order
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ rev funny.txt
 .uoy r woh olleh
.uoy rof gnikool ma i
.uoy llac ot togrof i !! hha
.uoy rof gnikool ma i

## to change all lower case to upper
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ cat funny.txt | tr [:lower:] [:upper:]
HELLO HOW R YOU.
I AM LOOKING FOR YOU.
AHH !! I FORGOT TO CALL YOU.
I AM LOOKING FOR YOU.
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$

## we have two editors. One is vi and another is nano
nano is by default present in MacOS.
== to write or save file in nano ctl + o
== to exit out of file ctl + x
== ctl + v to move down the screen
== ctl + y to move up the screen
== ctl + a to beginning of line
== ctl + e to end of line

## tar
create and manipulate tape archive files
.tar
.tar.gz
.tgz
.tar.bz2

tar -caf myfiles.tar.gz Exercise\ Files/

## Exercise and Files are are folders we want to compress

## to extract tar file
tar -xf mayflies.tar.gz

## Redirection
(standard input)stdin , keyboard input, descriptor 0
srdout, text on screen, 1
stderr, Error text on screen , 2

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ ls
hello/      sample.txt
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ ls 1>filelist.txt
## here 1 is stdout
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ cat filelist.txt
filelist.txt
hello/
sample.txt
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$

## we can ommit 1 as well
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ ls > filelist2.txt
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ cat filelist2.txt
filelist.txt
filelist2.txt
hello/
sample.txt

## usage of stdr
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ ls notreal
ls: notreal: No such file or directory

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ ls notreal 2> fileslist4.txt
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ cat fileslist4.txt
ls: notreal: No such file or directory

single “>” will redirect the file
double “>>” will append the file

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ cat fileslist4.txt
ls: notreal: No such file or directory
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ >fileslist4.txt
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ cat fileslist4.txt
## fileslist4 is empty. > has overhidden the file

nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ ls > filelist5.txt
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ echo "and some appended text" >> filelist5.txt
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$ cat filelist5.txt
filelist.txt
filelist2.txt
filelist5.txt
fileslist4.txt
hello/
sample.txt
and some appended text
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS/new_folder$

Problem: You have received a log file from your admin. In logs there are some breaking attempts by hacker .Your task is
1. Extract the log file
2. look for invalid user authorisation requests
3. Create a file containing the usernames

Solution:
## tar -xvf log.tar.gz
## cat auth.log
## cat auth.log | grep “input_userauth_request” | awk ‘{print $9}’ | sort -u >> users.txt
## cat users.txt

## to find which linux distribution you are using
cat /etc/* -release

In mac we should use
nawlekha@NAWLEKHA-M-Q1GZ:/users/nawlekha/Desktop/pyATS$ uname -a
Darwin NAWLEKHA-M-Q1GZ 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64

mac oS is not linux it is unix variant

## uname -a works in linux too

## in linux to see free memory 
free -h
cat /proc/cpuinfo

## to see space across volumes
df -h

## to see space taken by files and folders
du -h

## “dnf” is used to install software in fedora . It is replacement of yum
eg. sudo def install nano

## in ubuntu we use “apt” to install software
e.g. sudo apt-get update

Quiz.
1. Select the command that gives you the absolute path of the folder where you are currently working.
pwd

2. ls -l shows the permissions and ownership of files, among other things.
Yes, this istrue.

3. What command do you use to create a folder?
mkdir

4. Which of the following commands copies test1.txt to test2.txt?
cp test1.txt test2.txt

5. What character do you use with the find command to match any number of characters?
*

6. What are the two basic user roles in Linux?
normal and superuser

7. "rwxrw-r--" indicates that the owner of the file can read and write, but not execute the file.
Yes, this is false.

8. The command line is a text-based interface to type commands and direct text-based input and output to screen, files, and other programs.
You’re correct!

9. What is the general pattern of a command line statement?
command, options, arguments

10. What does ~ represent in a command line statement?
the user's home folder

11.What is the keyboard shortcut to jump to the end of the command line?
Ctrl + E

12. Which typed statement will show you more information about a particular command?
man [commandName]

13.What is the core Unix philosophy?
Tools should do one thing, and do it well.

14. What is the result of this command: echo "command" | wc
"1 1 8"

15. Select the command that lists the last few lines of a log file.
tail log.txt

16. The grep command prints the lines of text that match a pattern .
You’re correct!

17. Which 'awk' command shows the first column of data from a log file?
wk '{print $1}' logs.txt

18. What are the two modes in Vim?
insertion mode and command mode

19. You can use ^O to save a file in nano
Yes, this istrue.

20. Tar files do not offer compression themselves.
Yes, this istrue.

21. Select the name that has the descriptor of 1.
stdout

22. Which command help you select lines that have specific text you are looking for?
grep

23. Write the command that lists the contents of all the files in the systems /etc folder that end with -release.
cat /etc/*-release

24. This command outputs the RAM value for the local machine
free -h

25. Which command installs nano on Fedora?
sudo dnf install nano


Miscellaneous

Your assistant needs to print directory stack, what would you suggest?
NAWLEKHA-M-N1AJ:bin nawlekha$ dirs
/usr/local/bin

You have a large text file, and you need to see one page at a time. What will you do?
You can achieve the above result by pipeling the output of 'cat file_name.txt' with 'more' command.
[root@localhost ~]# cat file_name.txt | more

What are daemons?
Daemons are services that provide several functions that may not be available under the base operating system. Its
main task is to listen for service request and at the same time to act on these requests. After the service is done, it
is then disconnected and waits for further requests.

You have lots of running jobs, how would you remove all the running processes, without
restarting the machine?
The Linux command 'disown -r' will remove all the running Processes.

A user is new to Linux and he wants to know full list of available commands, what would
you suggest him?
A command 'compgen -c' will show a
full list of available commands.
[root@localhost ~]$ compgen -c

You need to track events on your system. What will you do?
For tracking the events on the system, we need a daemon called syslogd. The syslogd daemon is useful in tracking
the information of system and then saving it to specified log files.
Running „syslogd„ application in terminal generates log file at the location „/var/log/syslog„. The syslogd application is
very useful in troubleshooting Linux sytems. A sample log file looks similar to below.

Tell us the difference between Telnet and SSH?
Telnet and SSH both are communication protocol which are used to manage remote system. SSH is Secured, which
requires exchanging of key opposite of telnet which transmit data in plain text, which means telnet is less secure
than SSH.

What is the difference between command „ping‟ and „ping6′?
Both the commands are same and used for the same purpose except that the fact that ping6 is used with ipv6 IP
address

You want to search for all the *.tar files in your Home directory and wants to delete all at
once. How will you do it?
We need to use find command with rm command to delete all “.tar” files.
# find /home/ -name '*.tar' | xargs rm -rf

Which ports should you open in host firewall for an email server?
Open port 110 which is used by the POP3 e-mail protocol.
Open port 25 which is used by the SMTP e-mail protocol.
Open port 143 which is used by the IMAP e-mail protocol.

List out few of the differences between Softlink and Hardlink ?
a) Hardlink cannot be created for directories. Hard link can only be created for a file.
b) Symbolic links or symlinks can link to a directory.
c) Removing the original file that your hard link points to does not remove the hardlink itself; the hardlink still
provides the content of the underlying file.
d) If you remove the hard link or the symlink itself, the original file will stay intact.
e) Removing the original file does not remove the attached symbolic link or symlink, but without the original file, the
symlink is useless

Differentiate relative path from absolute path
Relative path refers to the path relative to the current path. Absolute path, on the other hand, refers to the exact path
as referenced from the root directory.

What is pid?
Pid is short for Process ID. It is used primarily to identify every process that runs on the UNIX system, whether it
runs on the foreground or runs at the background. Every pid is considered unique.

I want to see how many interfaces (Ethernet cards) are working using single command?
Ifconfig


No comments:

Post a Comment