Sunday 25 March 2018

Linux: Command Line Useful Commands:


Linux:Command Line Useful Commands:

## to access folder name having space
MBIDSAR-M-X1PX:Python mbidsar$ cd Python Space
-bash: cd: Python: No such file or directory
MBIDSAR-M-X1PX:Python mbidsar$ cd Python\ Space/
MBIDSAR-M-X1PX:Python Space mbidsar$ 
MBIDSAR-M-X1PX:Python Space mbidsar$


## to list all folders under directory
MBIDSAR-M-X1PX:Python mbidsar$ ls -R Python\ Space/
cbse-Python    examples

Python Space//cbse-Python:
cbse.zip  unit-1.pdf unit-2.pdf

Python Space//examples:
check_arg_type.py    hello.c              mymath.c       mysql-2.py     mysql-5.py     reflect.py      threads-with-lock.py
client.py      logger.py      myque.py       mysql-3.py     mysql-at.py          server.py      threads.py
decorator.py         multiprocess.py      mysql-1.py     mysql-4.py     mystack.py     threads-2.py
MBIDSAR-M-X1PX:Python mbidsar$ 



## to represent parent directory of current directory
MBIDSAR-M-X1PX:Python mbidsar$ cd ..
MBIDSAR-M-X1PX:Desktop mbidsar$ 


## to move back between folder
MBIDSAR-M-X1PX:Desktop mbidsar$ cd -
/Users/mbidsar/Desktop/Python
MBIDSAR-M-X1PX:Python mbidsar$ 


## this will take you from wherever you are to home folder
MBIDSAR-M-X1PX:Python mbidsar$ cd
MBIDSAR-M-X1PX:~ mbidsar$ 


## to create and remove directory
MBIDSAR-M-X1PX:Python mbidsar$ mkdir new_folder
MBIDSAR-M-X1PX:Python mbidsar$ rmdir new_folder/


## in order to remove, folder it should be empty
MBIDSAR-M-X1PX:Python mbidsar$ mkdir new_folder
MBIDSAR-M-X1PX:Python mbidsar$ mkdir new_folder/hello
MBIDSAR-M-X1PX:Python mbidsar$ rmdir new_folder
rmdir: new_folder: Directory not empty
MBIDSAR-M-X1PX:Python mbidsar$ 
For this we can do rm -r ## delete recursively


## move file to other dir
MBIDSAR-M-X1PX:Python mbidsar$ vi forfun.txt
MBIDSAR-M-X1PX:Python mbidsar$ mv forfun.txt new_folder/
MBIDSAR-M-X1PX:Python mbidsar$ ls for*
ls: for*: No such file or directory


## to move all .txt files to new folder
MBIDSAR-M-X1PX:Python mbidsar$ mv *.txt new_folder/


## to move back all files to current dir
MBIDSAR-M-X1PX:Python mbidsar$ mv new_folder/* .


## use of ? to delete files. this will delete file name with funny followed by any single character
MBIDSAR-M-X1PX:Python mbidsar$ cp funny.txt funny1.txt
MBIDSAR-M-X1PX:Python mbidsar$ cp funny.txt funny2.txt
MBIDSAR-M-X1PX:Python mbidsar$ rm funny?.txt
MBIDSAR-M-X1PX:Python mbidsar$ ls fun*
'fun.txt       funny.txt


## find all files in current directory search based on name
MBIDSAR-M-X1PX:Python mbidsar$ find . -name "*.txt"
./abc.txt
./forfun.txt
./fun.txt
./funny.txt
./misc.txt
./python regexp for ipv4 matach.txt
./Questions.txt
MBIDSAR-M-X1PX:Python mbidsar$



## to search in different folder
MBIDSAR-M-X1PX:Python mbidsar$ find ~/ -name "*folio*"
find: /Users/mbidsar//Library/Caches/com.cisco.Jabber/com.apple.opencl: Permission denied
find: /Users/mbidsar//Library/Saved Application State/com.yourcompany.StellarPhoenixMacDataRecovery.savedState: Permission denied
MBIDSAR-M-X1PX:Python mbidsar$ 


## to give up privilege
MBIDSAR-M-X1PX:Python mbidsar$ sudo -k


## if you want to work as root
MBIDSAR-M-X1PX:Python mbidsar$ 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


## let’s provide read, write and execute permission to user
MBIDSAR-M-X1PX:Python mbidsar$ chmod 744 LEGB.py
MBIDSAR-M-X1PX:Python mbidsar$ 
MBIDSAR-M-X1PX:Python mbidsar$ ls -lh LEGB.py
-rwxr--r--  1 mbidsar  staff     0B Mar 22 10:04 LEGB.py
MBIDSAR-M-X1PX:Python mbidsar$ ./LEGB.py
MBIDSAR-M-X1PX:Python mbidsar$ 


## let’s remove read permission for user
MBIDSAR-M-X1PX:Python mbidsar$ chmod 244 LEGB.py 
MBIDSAR-M-X1PX:Python mbidsar$ cat LEGB.py
cat: LEGB.py: Permission denied
MBIDSAR-M-X1PX:Python mbidsar$ chmod 755 LEGB.py
MBIDSAR-M-X1PX:Python mbidsar$ cat LEGB.py
MBIDSAR-M-X1PX:Python mbidsar$ 


## when a user create a file in home directory, it starts with 664 permission
MBIDSAR-M-X1PX:Python mbidsar$ touch newfile
MBIDSAR-M-X1PX:Python mbidsar$ ls -lh newfile
-rw-r--r--  1 mbidsar  staff     0B Mar 22 10:08 newfile
MBIDSAR-M-X1PX:Python mbidsar$ 



## we can change the ownership of the file
MBIDSAR-M-X1PX:Python mbidsar$ 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
MBIDSAR-M-X1PX:Python mbidsar$ sudo chown mbidsar newfile
MBIDSAR-M-X1PX:Python mbidsar$ vi newfile 


## for changing group ownership we will use chgrp

## command to print whatever you give to it
MBIDSAR-M-X1PX:Python mbidsar$ echo "hello"
hello
MBIDSAR-M-X1PX:Python mbidsar$ 


## use of pipeline
MBIDSAR-M-X1PX:Python mbidsar$ echo "hello" | wc
       1       1       6
MBIDSAR-M-X1PX:Python mbidsar$ 

==> one line of text, one word and six character

MBIDSAR-M-X1PX:Python mbidsar$ echo "hello world from the command line" | wc
       1       6      34
MBIDSAR-M-X1PX:Python mbidsar$ 
==> one line of text, six word and 34 character


## Cat 
concatenate and print files
MBIDSAR-M-X1PX:Python mbidsar$ cat comment.py
Comment.py:
----------
print('Hi i am Manish')

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


if True:
    print('Hello Manish')

"""
#or

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

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




## to see first 10 and last 10 lines of a file
First-10
MBIDSAR-M-X1PX:Python mbidsar$ head comment.py
print('Hi i am Manish')

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


if True:
    print('Hello Manish')

"""
MBIDSAR-M-X1PX:Python mbidsar$ 


Last-10
MBIDSAR-M-X1PX:Python mbidsar$ tail comment.py
"""
#or

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

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

MBIDSAR-M-X1PX:Python mbidsar$ 



## we can also customize it.  this will print 1st 5 lines of a file
MBIDSAR-M-X1PX:Python mbidsar$ head -n 5 comment.py
print('Hi i am Manish')

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

MBIDSAR-M-X1PX:Python mbidsar$ 


## last 3 lines of file
MBIDSAR-M-X1PX:Python mbidsar$ tail -n 3 comment.py
print(sum(1,2))
"""

MBIDSAR-M-X1PX:Python mbidsar$ 

MBIDSAR-M-X1PX:Python mbidsar$ cat comment.py | cat -n | tail -n 5
    15        return(x+y)
    16   
    17    print(sum(1,2))
    18    """
    19   
MBIDSAR-M-X1PX:Python mbidsar$ 


MBIDSAR-M-X1PX:Python mbidsar$ cat comment.py | tail -n 5 | cat -n
     1        return(x+y)
     2   
     3    print(sum(1,2))
     4    """
     5   
MBIDSAR-M-X1PX:Python mbidsar$ 



## to view large files
MBIDSAR-M-X1PX:Python mbidsar$ 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

MBIDSAR-M-X1PX:Python mbidsar$ grep "sum" comment.py
def sum(x,y):
print(sum(1,2))
MBIDSAR-M-X1PX:Python mbidsar$ 

MBIDSAR-M-X1PX:Python mbidsar$ grep -n "sum" comment.py
14:def sum(x,y):
17:print(sum(1,2))
MBIDSAR-M-X1PX:Python mbidsar


## grep is case sensitive
MBIDSAR-M-X1PX:Python mbidsar$ grep -n "Sum" comment.py
MBIDSAR-M-X1PX:Python mbidsar$ 


## for case in-sensitive we can use -i
MBIDSAR-M-X1PX:Python mbidsar$ grep -i "manish" comment.py
print('Hi i am Manish')
    print('Hello Manish')
MBIDSAR-M-X1PX:Python mbidsar$ 

## to omit lines which we don’t want to see
MBIDSAR-M-X1PX:Python mbidsar$ grep -vi "sum" comment.py
print('Hi i am Manish')

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


if True:
    print('Hello Manish')

"""
#or

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

"""

MBIDSAR-M-X1PX:Python mbidsar$ 


## for regex, we can use -E. To fetch he occurrence of letter "hijk"
MBIDSAR-M-X1PX:Python mbidsar$ grep -E "[hijk]" comment.py
print('Hi i am Manish')
if False:
    print('This code will not execute')
if True:
    print('Hello Manish')
if False: """
print(sum(1,2))
MBIDSAR-M-X1PX:Python mbidsar$ 



## 6 or more any word character
MBIDSAR-M-X1PX:Python mbidsar$ grep -E "\w{6,}" comment.py
print('Hi i am Manish')
    print('This code will not execute')
    print('Hello Manish')
    return(x+y)
MBIDSAR-M-X1PX:Python mbidsar$ 

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



## 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


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 authorization 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


mac oS is not linux it is unix variant

## uname -a works in linux too
MBIDSAR-M-X1PX:Python mbidsar$ uname -a 
Darwin MBIDSAR-M-X1PX 16.7.0 Darwin Kernel Version 16.7.0: Thu Jan 11 22:59:40 PST 2018; root:xnu-3789.73.8~1/RELEASE_X86_64 x86_64


## 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

Misc-Questions:
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 is true.

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. Which command help you select lines that have specific text you are looking for?
grep

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

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

No comments:

Post a Comment

Note: only a member of this blog may post a comment.