Pages

Sunday, October 8, 2017

Linux Command Line

Terminal usage:

[email protected]:/users/nawlekha/Desktop/pyATS$ ls -lh
total 67992
[email protected]  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/
[email protected] 45 nawlekha  staff   1.5K Jun 19 10:25 Python 3 for begineers

[email protected]:/users/nawlekha/Desktop/pyATS$ ls -l /var/log
total 73464
drwxr-xr-x   3 root             wheel             102B Apr 13 21:14 Bluetooth/
[email protected]  1 root             wheel              12B Apr 13 21:13 CDIS.custom
drwxr-xr-x   2 root             wheel              68B Aug  6  2016 CoreDuet/

[email protected]:/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/
[email protected]  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

[email protected]:~/Desktop/pyATS$ man ls

~ means home folder

## to access folder name having space
[email protected]:~/Desktop/pyATS$ cd Python training_doc
-bash: cd: Python: No such file or directory
[email protected]:~/Desktop/pyATS$
[email protected]:~/Desktop/pyATS$
[email protected]:~/Desktop/pyATS$ cd Python\ training_doc
[email protected]:~/Desktop/pyATS/Python training_doc$

## to list all folders under directory
[email protected]:~/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
[email protected]:~/Desktop/pyATS$ cd ..
[email protected]:~/Desktop$

## to move back between folder
[email protected]:~/Desktop/pyATS$ cd -
/Users/nawlekha/Desktop/pyATs
[email protected]:~/Desktop/pyATs$

## this will take you from wherever you are to home folder
[email protected]:~/Desktop/pyATs$ cd
[email protected]:~$

## to create and remove directory
[email protected]:~/DEsktop/pyATS$ mkdir new_folder
[email protected]:~/DEsktop/pyATS$ rmdir new_folder

## in order to remove, folder it should be empty
[email protected]:~/DEsktop/pyATS$ mkdir new_folder
[email protected]:~/DEsktop/pyATS$ mkdir new_folder/hello
[email protected]:~/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
[email protected]:~/DEsktop/pyATS$ vi forfun.txt
[email protected]:~/DEsktop/pyATS$

[email protected]:~/DEsktop/pyATS$ mv forfun.txt new_folder/
[email protected]:~/DEsktop/pyATS$
[email protected]-Q1GZ:~/DEsktop/pyATS$ ls for*
ls: for*: No such file or directory

[email protected]:~/DEsktop/pyATS/new_folder$ ls
forfun.txt  hello/

[email protected]:~/DEsktop/pyATS$ mv new_folder/forfun.txt new_folder/funny.txt
[email protected]:~/DEsktop/pyATS$
[email protected]:~/DEsktop/pyATS$ cd new_folder/
[email protected]:~/DEsktop/pyATS/new_folder$ ls
funny.txt  hello/

## to move folder to current directory
[email protected]:~/DEsktop/pyATS$ mv new_folder/funny.txt .

[email protected]:~/DEsktop/pyATS$ ls fu*
funny.txt

## to move all .txt files to new folder
[email protected]:~/DEsktop/pyATS$ mv *.txt new_folder/

## to move back all files to current dir
[email protected]:~/DEsktop/pyATS$ mv new_folder/* .

## use of ? to delete files. this will delete file name with funny followed by any single character
[email protected]:~/DEsktop/pyATS$ cp funny.txt funny1.txt
[email protected]:~/DEsktop/pyATS$ cp funny.txt funny2.txt
[email protected]:~/DEsktop/pyATS$ rm funny?.txt

[email protected]:~/DEsktop/pyATS$ ls fun*
funny.txt

## find all files in current directory search based on name
[email protected]:~/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

[email protected]:~/DEsktop/pyATS$ find . -name "PyATS*"
./PyATS online sessions

[email protected]:~/DEsktop/pyATS$ find . -name "*folio*"
./portfolio.csv
./portfolio1.csv

## to search in different folder
[email protected]:~/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
[email protected]:~/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

[email protected]:~/DEsktop/pyATS$ sudo ls /root
Password: ## user password
ls: /root: No such file or directory
[email protected]:~/DEsktop/pyATS$ sudo ls
.DS_Store missing.csv
CodeSchool-TryPython.pdf new_folder

## to give up privilege
[email protected]:~/DEsktop/pyATS$ sudo -k

## if you want to work as root
[email protected]:~/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

[email protected]:~/DEsktop/pYATS$ ./LEGB.py
-bash: ./LEGB.py: Permission denied
[email protected]:~/DEsktop/pYATS$
[email protected]:~/DEsktop/pYATS$
[email protected]:~/DEsktop/pYATS$ ls -lh LEGB.py
-rw-r--r--  1 nawlekha  staff   210B Sep 21 18:01 LEGB.py
[email protected]:~/DEsktop/pYATS$
[email protected]:~/DEsktop/pYATS$
[email protected]:~/DEsktop/pYATS$ python3 LEGB.py
outer x
outer x
[email protected]:~/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
[email protected]:~/DEsktop/pYATS$ chmod 744 LEGB.py
[email protected]:~/DEsktop/pYATS$
[email protected]:~/DEsktop/pYATS$ ls -lh LEGB.py
-rwxr--r--  1 nawlekha  staff   210B Sep 21 18:01 LEGB.py*
[email protected]:~/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
[email protected]:~/DEsktop/pYATS$ chmod 244 LEGB.py or chmod u-r LEGB.py
[email protected]:~/DEsktop/pYATS$ cat LEGB.py
cat: LEGB.py: Permission denied
## let’s provide read permission again
[email protected]:~/DEsktop/pYATS$ chmod 755 LEGB.py
[email protected]:~/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
[email protected]:~/DEsktop/pYATS$ touch newfile
[email protected]:~/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
[email protected]:~/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
[email protected]:~/DEsktop/pYATS$ sudo chown nawlekha newfile
[email protected]:~/DEsktop/pYATS$ vi newfile

## for changing group ownership we will use chgrp

## command to print whatever you give to it
[email protected]:~/DEsktop/pYATS$ echo "hello"
hello

## use of pipeline
[email protected]:~/DEsktop/pYATS$ echo "hello" | wc
       1       1       6
==> one line of text, one word and six character

[email protected]Q1GZ:~/DEsktop/pYATS$ echo "hello world from the command line" | wc
       1       6      34

## Cat 
concatenate and print files

[email protected]:/users/nawlekha/Desktop/pyATS$ cat comment.py

## to see first 10 and last 10 lines of a file
[email protected]:/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')

"""
[email protected]:/users/nawlekha/Desktop/pyATS$ tail comment.py

#or

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

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

[email protected]:/users/nawlekha/Desktop/pyATS$

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

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

[email protected]:/users/nawlekha/Desktop/pyATS$

## last 3 lines of file
[email protected]:/users/nawlekha/Desktop/pyATS$ tail -n 3 comment.py
print(sum(1,2))
"""
[email protected]:/users/nawlekha/Desktop/pyATS$

[email protected]:/users/nawlekha/Desktop/pyATS$ cat comment.py | cat -n | tail -n 5
    21     return(x+y)
    22
    23 print(sum(1,2))
    24 """
    25
[email protected]:/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
[email protected]:/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

[email protected]:/users/nawlekha/Desktop/pyATS$ grep "sum" comment.py
def sum(x,y):
print(sum(1,2))
def sum(x,y):
print(sum(1,2))
[email protected]:/users/nawlekha/Desktop/pyATS$

[email protected]:/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))
[email protected]:/users/nawlekha/Desktop/pyATS$

## grep is case sensitive
[email protected]:/users/nawlekha/Desktop/pyATS$ grep -n "Sum" comment.py
[email protected]:/users/nawlekha/Desktop/pyATS$

## for case in-sensitive we can use -i
[email protected]:/users/nawlekha/Desktop/pyATS$ grep -i "nawraj" comment.py
print('Hi i am Nawraj')
    print('Hello nawraj')
[email protected]:/users/nawlekha/Desktop/pyATS$

## to omit lines which we don’t want to see
[email protected]:/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"
[email protected]:/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
[email protected]:/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)
[email protected]:/users/nawlekha/Desktop/pyATS$

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

## print values from 2nd column
[email protected]:/users/nawlekha/Desktop/pyATS$ awk '{print $2}' apr-24.txt

## to sort output based on numbers
[email protected]:/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
[email protected]:/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.
[email protected]:/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
[email protected]:/users/nawlekha/Desktop/pyATS$ sort -k2n simple.txt

### to remove duplicate entry
[email protected]:/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.
[email protected]:/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
[email protected]:/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
[email protected]:/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.
[email protected]:/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

[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ ls
hello/      sample.txt
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ ls 1>filelist.txt
## here 1 is stdout
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ cat filelist.txt
filelist.txt
hello/
sample.txt
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$

## we can ommit 1 as well
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ ls > filelist2.txt
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ cat filelist2.txt
filelist.txt
filelist2.txt
hello/
sample.txt

## usage of stdr
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ ls notreal
ls: notreal: No such file or directory

[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ ls notreal 2> fileslist4.txt
[email protected]:/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

[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ cat fileslist4.txt
ls: notreal: No such file or directory
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ >fileslist4.txt
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ cat fileslist4.txt
## fileslist4 is empty. > has overhidden the file

[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ ls > filelist5.txt
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ echo "and some appended text" >> filelist5.txt
[email protected]:/users/nawlekha/Desktop/pyATS/new_folder$ cat filelist5.txt
filelist.txt
filelist2.txt
filelist5.txt
fileslist4.txt
hello/
sample.txt
and some appended text
[email protected]:/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
[email protected]:/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.
[[email protected] ~]# 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.
[[email protected] ~]$ 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