EVERYTHING IN COMMAND LINE/ TERMINAL – MANIPULATION

Here, we learn about Manipulation of Files and Directories. For manipulation we use options.

  • ls -a = lists all files and directories including the hidden ones. Here, -a is an option.
pwd
/home/all/wild
  • ls -a = lists all files and directories including the hidden ones. Here, -a is an option. The hidden directories or files begin with a dot (.).
ls
animals  birds  flowers.txt
ls -a
. .. .local animals birds flowers.txt
  • ls -l = The -l option lists files and directories as a table. The -l option lists files and directories as a table.Here we see three rows, with seven columns separated by spaces. Here’s what each column means:
    1. Access rights. These are actions that are permitted on a file or directory.
    2. Number of hard links. This number counts the number of child directories and files. This number includes the parent directory link (..) and current directory link (.).
    3. The username of the file’s owner. Here the username is company.
    4. The name of the group that owns the file. Here the group name is staff.
    5. The size of the file in bytes.
    6. The date & time that the file was last modified.
    7. The name of the file or directory.
ls -l                                                                                       
total 8                                                                                     
drwxr-xr-x 4 company staff 4096 Jul  8 17:33 animals                                          
drwxr-xr-x 4 company staff 4096 Jul  8 17:36 birds                                                                                      
-rw-r--r-- 1 company staff    0 Jul  8 17:32 flowers.txt     
  • ls – t = lists all files and directories in the order of last modified. :
ls -t
birds animals flowers.txt
  • ls – alt = lists all files and directories including hidden ones, in long format, ordered by the date and time they were last modified.
ls -alt                                                                         
total 20                                                                          
drwxr-xr-x 5 company staff 4096 Aug 16 01:54 ..                                   
drwxr-xr-x 4 company staff 4096 Jul  8 17:57 .                                    
-rw-r--r-- 1 company staff    0 Jul  8 17:42 .local                           
drwxr-xr-x 2 company staff 4096 Jul  8 17:36 birds                               
drwxr-xr-x 2 company staff 4096 Jul  8 17:33 animals                            
-rw-r--r-- 1 company staff    0 Jul  8 17:32 flowers.txt 
  • cp <files or directories to copy> <destination> = used to copy files or directories to specified destination

cp biopic/ray.txt biopic/notorious.txt historical/

Advertisements

Creating a Dictionary in Python

Dictionary is similar to a list. It contains key:value pairs. Here, we will learn about creating and modifying dictionaries.
Creating Dictionary
dictionary_name = {key:value, key:value,…..}

We create a dictionary with colors of jams as keys and fruit names as values.

jam = {'red':'strawberry', 'yellow': 'mango', 'orange':'orange'}
print jam['orange']
print jam['red']

orange
strawberry

Insertion – Here, we add a new key/value pair to the existing dictionary.

jam['blue'] = 'blueberry'

{‘blue’: ‘blueberry’, ‘red’: ‘strawberry’, ‘yellow’: ‘mango’, ‘orange’: ‘orange’}

Deletion – Here, we delete a key/value pair

del jam['orange']
jam

{‘blue’: ‘blueberry’, ‘red’: ‘strawberry’, ‘yellow’: ‘mango’}

Replacing values – We replace value for key ‘red’ from strawberry to cherry.

jam ['red'] = 'cherry'
jam

{‘blue’: ‘blueberry’, ‘red’: ‘cherry’, ‘yellow’: ‘mango’}

Nesting : We nest a new dictionary within an existing dictionary.

 jam['red'] = {'light' : 'cherry', 'dark' : 'strawberry' }

{‘blue’: ‘blueberry’, ‘red’: {‘dark’: ‘strawberry’, ‘light’: ‘cherry’}, ‘yellow’: ‘mango’}
We set a list as a value for key ‘blue’.

jam['blue'] = ['blueberry','plum']
jam

{‘blue’: [‘blueberry’, ‘plum’], ‘red’: {‘dark’: ‘strawberry’, ‘light’: ‘cherry’}, ‘yellow’: ‘mango’}

Append to a list

jam['blue'].append('jamun')  
print jam['blue']

[‘blueberry’, ‘plum’, ‘jamun’]

Indexing the nested dictionary

print jam['red']['dark']

strawberry

jam

{‘blue’: [‘blueberry’, ‘plum’, ‘jamun’], ‘red’: {‘dark’: ‘strawberry’, ‘light’: ‘cherry’}, ‘yellow’: ‘mango’}