Sunday, 5 February 2017

File Management

All data in Unix is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the filesystem.

When you work with Unix, one way or another, you spend most of your time working with files. This tutorial will help you understand how to create and remove files, copy and rename them, create links to them, etc.

In Unix, there are three basic types of files –

·        Ordinary FilesAn ordinary file is a file on the system that contains data, text, or program instructions. In this tutorial, you look at working with ordinary files.

·   DirectoriesDirectories store both special and ordinary files. For users familiar with Windows or Mac OS, Unix directories are equivalent to folders.

·         Special Files Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to  aliases or shortcuts and enable you to access a single file using different name


In file management we are going to see…

Directory Command

What are Commands


       A command is an instruction given to our computer by us to do whatever we want. In Mac OS, and Linux it is called terminal, whereas, in windows it is called command prompt. Commands are always case sensitive.

Commands are executed by typing in at the command line followed by pressing enter key.

This command further passes to the shell which reads the command and execute it. Shell is a method for the user to interact with the system. Default shell in Linux is called bash (Bourne-Again Shell).

There are two types of shell commands:

Built-in shell commands: They are part of a shell. Each shell has some built in commands.

External/Linux commands: Each external command is a separate executable program written in C or other programming languages.


Linux Directory Commands


Directory Command
Description
pwd
The pwd command stands for (print working directory). It displays the current working location or directory of the user. It displays the whole working path starting with /. It is a built-in command.
ls
The ls command is used to show the list of a folder. It will list out all the files in the directed folder.
cd
The cd command stands for (change directory). It is used to change to the directory you want to work from the present directory.
mkdir
With mkdir command you can create your own directory.
rmdir
The rmdir command is used to remove a directory from your system.

ls command

List (ls):

                To list the files present under the directory


ls option
Description
ls -a
In Linux, hidden files start with . (dot) symbol and they are not visible in the regular directory. The (ls -a) command will enlist the whole list of the current directory including the hidden files.
ls -l
It will show the list in a long list format.
ls -lh
This command will show you the file sizes in human readable format. Size of the file is very difficult to read when displayed in terms of byte. The (ls -lh)command will give you the data in terms of Mb, Gb, Tb, etc.
ls -lhS
If you want to display your files in descending order (highest at the top) according to their size, then you can use (ls -lhS) command.
ls -l - -block-size=[SIZE]
It is used to display the files in a specific size format. Here, in [SIZE] you can assign size according to your requirement.
ls -d */
It is used to display only sub directories.
ls -g or ls -lG
With this you can exclude column of group information and owner.
ls -n
It is used to print group ID and owner ID instead of their names.
ls --color=[VALUE]
This command is used to print list as colored or discolored.
ls -li
This command prints the index number if file in the first column.
ls -p
It is used to identify the directory easily by marking the directories with a slash (/) line sign.
ls -r
It is used to print the list in reverse order.
ls -R
It will display the content of the sub-directories also.
ls -lX
It will group the files with same extensions together in the list.
ls -lt
It will sort the list by displaying recently modified filed at top.
ls ~
It gives the contents of home directory.
ls ../
It give the contents of parent directory.
ls --version
It checks the version of ls command.

Wildcard Command

The * wildcard

     The character * is called a wildcard, and will match against none or more character(s) in a file (or directory) name. For example,

 ls list*



This will list all files in the current directory starting with list....

 ls *list


This will list all files in the current directory ending with ....list

The ? wildcard

     The character ? Will match exactly one character.
So ?ouse will match files like house and mouse, but not grouse.

 ls ?list




Cat Command

Cat Command:

                cat is one of the most frequently used commands on Unix-like operating systems. It has three related functions with regard to text files:
            
            1. Displaying files
2. Combining copies of files
3. Creating new files.

Displaying Files

                To display a file

Syntax:

cat file_name
               
  If the file is too large for all of the text to fit on the monitor screen simultaneously, as is frequently the case, the text will scroll down the screen at high speed and be very difficult to read. This problem is easily solved by piping the output to the filter less, i.e.,

       cat file1 | less
              
  As well we can try with more command for this like below
    
           Cat file1 | more

Combining copies of files

                    The second role of cat is concatenation (i.e., stringing together) of copies of the contents of files. (This is the source of cat's curious name.) Because the concatenation occurs only to the copies, there is no effect on the original files.

For example, the following command will concatenate copies of the contents of the three files file1, file2 and file3:

cat file1 file2 file3

The contents of each file will be displayed on the monitor screen (which, again, is standard output, and thus the destination of the output in the absence of redirection) starting on a new line and in the order that the file names appear in the command. This output could just as easily be redirected using the output redirection operator to another file, such as file4, using the following:

cat file1 file2 file3 > file4


In the next example, the output of cat is piped to the sort filter in order to alphabetize the lines of text after concatenation and prior to writing to file4:

           cat file1 file2 file3 | sort > file4

File Creation

            The third use for cat is file creation. For small files this is often easier than using vi, gedit or other text editors. It is accomplished by typing cat followed by the output redirection operator and the name of the file to be created, then pressing ENTER and finally simultaneously pressing the CONTROL and d keys. For example, a new file named file1 can be created by typing

cat > file1

Then pressing the ENTER key and finally simultaneously pressing the CONTROL and d keys.

If a file named file1 already exists, it will be overwritten (i.e., all of its contents will be erased) by the new, empty file with the same name. Thus the cautious user might prefer to instead use the append operator (represented by two successive rightward pointing angular brackets) in order to prevent unintended erasure. That is,

cat >> file1

To create a new file file2 that consists of the contents of file2 followed by text typed in from the keyboard, first enter the following:


cat file1- > file2

Tuesday, 20 December 2016

Basic Unix Commands


                                                          Basic Unix Commands

       Hi all,
                
               This post will be useful to the beginners..

List (ls):
                To list the files present under the directory
% ls
To view the hidden files
% ls –a
To view the file with its size
% ls –l


Check what happens with  % ls –al  ????

      To know more about ls command please click here

Directories:
To change the directory
% cd directory_name

To go to the parent directory

% cd ..

Path Name: (pwd)
To know the current path on which you are working
% pwd

Concatenate (cat):
                The command cat can be used to display the contents of a file on the screen.
% cat filename

Less:
The command less writes (view) the contents of a file onto the screen a page at a time.
% less filename

gzless
    Gzless command is used to view the .gz files
% gzless filename.gz

Head:
The head command writes the first ten lines of a file to the screen.
% head filename
              
       To view the first 5 lines of the file
% head -5 filename


Tail:
The tail command writes the last ten lines of a file to the screen.

% tail filename

To view the last 5 lines of the file
% tail -5 filename

grep:
                grep stands for "Global regular expression print" it is used search the keyword in a file

% grep keyword filename
               
 If you not sure about your keyword (case sensitive)
% grep -i keyword filename                             (-i is for case sensitive it will consider both a and A)
                

To search a sentence or more than two keywords
% grep -i ‘keyword  keyword’ filename                  (we can use either single quote or double)


Some of the other options of grep are:
-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines

gzgrep
        To reduce the file size files are compressed to gz file(hello.txt.gz). gzgrep command is used to check for search keywords in gz files

% gzgrep keyword filename.gz

Word count:
                
           To know the number of words present in the file
% wc –w file name
                
          To know the number of lines present in the file
% wc –l file name

Sort:
                To sort the content in the file
% sort file name               ascending order
% sort –r filename           descending order
% sort –u filename          To remove repeated content

Pipes:
                With the help of pipe symbol we can combined perform commands
% ls | tail
% ls | sort

Example:
grep apple fruit.txt | grep grape| grep banana

History

To view the commands entered by you (user)

% history

Recall commands

% !! (recall last command)
% !-3 (recall third most recent command)
% !5 (recall 5th command in list)
% !grep (recall last command starting with grep)

Help
         To know the complete description of any command ant its syntax use man command. For example, to find out more about the wc (word count) command, type

% man wc

Alternatively,

% whatis wc         gives a one-line description of the command

Apropos
When you are not sure of the exact name of a command,
% apropos keyword