Pages

Thursday, 12 December 2013

Linux Commands - mkdir | rmdir | touch | rm | cp | more | less | head | tail | cat




mkdir: Create Directory
rmdir: Remove Directory
touchCreate a file
rm:    Remove file
cp:    Copy file
more:  View file content <More than 1 page>
less:  View the file content
head:  Display first 10 lines of file
tail:  Display last 10 lines of file


mkdir:


User can create their directory using mkdir command.

Example:

hadoopguru@hadoop2:~$ mkdir hadoop_test
hadoop
guru@hadoop2:~$ ls
apache-flume-1.4.0-bin.tar.gz.1            hadoop-1.2.1-bin.tar.gz
aveo                                       hadoop_test
data                                       hive
datanode                                   hive-0.11.0-bin.tar.gz
derby.log                                  mahout-distribution-0.8.tar.gz
flume                                      metastore_db
hadoop-1.2.1                               namenode


touch: 

Creating a file:

Creating a file can be done using touch command

Example:

hadoopguru@hadoop2:~$ cd hadoop_test/

hadoopguru@hadoop2:~/hadoop_test$ touch pig.txt

hadoopguru@hadoop2:~/hadoop_test$ touch hive.txt

hadoopguru@hadoop2:~/hadoop_test$ touch mahout

hadoopguru@hadoop2:~/hadoop_test$ ls -l
total 0
-rw-rw-r-- 1 hadoop hadoop 0 Oct 28 00:13 hive.txt
-rw-rw-r-- 1 hadoop hadoop 0 Oct 28 00:13 mahout
-rw-rw-r-- 1 hadoop hadoop 0 Oct 28 00:13 pig.txt


1.  touch –t :
One can create a new file with user defined date and time, using this touch -t command.

Example:

hadoopguru@hadoop2:~/hadoop_test$ touch -t 201310272350 testemp1

hadoopguru@hadoop2:~/hadoop_test$ ls -l
total 0
-rw-rw-r-- 1 hadoop hadoop 0 Oct 28  00:13   hive.txt
-rw-rw-r-- 1 hadoop hadoop 0 Oct 28  00:13   mahout
-rw-rw-r-- 1 hadoop hadoop 0 Oct 28  00:13   pig.txt
-rw-rw-r-- 1 hadoop hadoop 0 Oct 27  23:50   testemp
-rw-rw-r-- 1 hadoop hadoop 0 Oct 27  23:50   testemp1


rm: 

      Removing a file:

File can be removed using rm command.

Note: A file can’t be recovered back after remove. A file once removed is gone. Hence one should be careful before removing a file.

Example:

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  mahout  pig.txt  testemp  testemp1  testemp2

hadoopguru@hadoop2:~/hadoop_test$ rm testemp2

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  mahout  pig.txt  testemp  testemp1
      

      rm -i :
This command asks for a confirmation from user to remove a file.

Example:

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  mahout  pig.txt  testemp  testemp1

hadoopguru@hadoop2:~/hadoop_test$ rm -i testemp1
rm: remove regular empty file `testemp1'? y

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  mahout  pig.txt  testemp


      rm -r :
This command can be used to remove any file or directory.

Example:
test_dir is a directory which can be removed using rm –r command

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  mahout  pig.txt  test_dir  testemp  test_empty

hadoopguru@hadoop2:~/hadoop_test$ rm –r test_dir

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  mahout  pig.txt  testemp  test_empty



cp:

Copy a file:
In order to copy a file cp command is used.
If in case the target is a directory, the source file will get copied to that directory.

      Example

    1Copy from file to file

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  pig.txt   testemp     Test_file1.txt
mahout    Test_dir  test_empty  Test_file2.txt

hadoopguru@hadoop2:~/hadoop_test$ cp Test_file1.txt Test_file2.txt


    2: Target is a directory

hadoopguru@hadoop2:~/hadoop_test$ cd Test_dir

hadoopguru@hadoop2:~/hadoop_test/Test_dir$ ls

hadoopguru@hadoop2:~/hadoop_test/Test_dir$ cd ..

hadoopguru@hadoop2:~/hadoop_test$ cp Test_file1.txt Test_dir

hadoopguru@hadoop2:~/hadoop_test$ cd Test_dir

hadoopguru@hadoop2:~/hadoop_test/Test_dir$ ls
Test_file1.txt


    3If user wants to copy whole directory, cp -r command is used.

Example:

Case I:  If target directory doesnot exist

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt    pig.txt      Test_dir2    Test_file1.txt
mahout      Test_dir     testemp      Test_file2.txt

hadoopguru@hadoop2:~/hadoop_test$ ls Test_dir
Test_file1.txt

hadoopguru@hadoop2:~/hadoop_test$ cp -r Test_dir Test_dir1

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt    pig.txt      Test_dir1     testemp     Test_file2.txt
mahout      Test_dir     Test_dir2     Test_file1.txt

hadoopguru@hadoop2:~/hadoop_test$ ls Test_dir1/
Test_file1.txt


Case II:  If target directory already exist

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  pig.txt   Test_dir1  testemp         Test_file2.txt
mahout    Test_dir  Test_dir2  Test_file1.txt

hadoopguru@hadoop2:~/hadoop_test$ ls Test_dir1
Test_file1.txt

hadoopguru@hadoop2:~/hadoop_test$ ls Test_dir2
Test_file2.txt

hadoopguru@hadoop2:~/hadoop_test$ cp -r Test_dir1 Test_dir2

hadoopguru@hadoop2:~/hadoop_test$ ls Test_dir2
Test_dir1  Test_file2.txt


    4:  File(s) from one directory to another:

File can be copied from one directory to another using same cp command where last name will always be a directory name

Example:

Case I:  Copy single file

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  pig.txt   Test_dir1  testemp         Test_file2.txt
mahout    Test_dir  Test_dir2  Test_file1.txt

hadoopguru@hadoop2:~/hadoop_test$ ls Test_dir1
Test_file1.txt

hadoopguru@hadoop2:~/hadoop_test$ ls Test_dir2
Test_file2.txt

hadoopguru@hadoop2:~/hadoop_test$ cp Test_dir1/Test_file1.txt Test_dir2

hadoopguru@hadoop2:~/hadoop_test$ ls Test_dir2
Test_file1.txt  Test_file2.txt


Case II:  Copy multiple files 

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  pig.txt   Test_dir1  testemp         Test_file2.txt
mahout    Test_dir  Test_dir2  Test_file1.txt

hadoopguru@hadoop2:~/hadoop_test$ cp hive.txt pig.txt Test_dir1/Test_file1.txt Test_dir2

hadoopguru@hadoop2:~/hadoop_test$ ls Test_dir2
hive.txt  pig.txt  Test_file1.txt  Test_file2.txt


    5:  Interactive commands:

Interactive command (cp –i) ask user confirmation to overwrite the existing file.

Example:

hadoopguru@hadoop2:~/hadoop_test$ ls
hive.txt  mahout  pig.txt  Test_dir  Test_dir1  Test_dir2

hadoopguru@hadoop2:~/hadoop_test$ cp -i hive.txt mahout
cp: overwrite `mahout'? no

hadoopguru@hadoop2:~/hadoop_test$



more:


         more command is used to view the contents of a file containing more than 1 page. 
      Can scroll to the next page using Space bar or PageUp Pagedown buttons.

Example:

hadoop@hadoop2:~$ ls
AboutHadoop.txt   aveo  data  derby.log  hadoop-1.2.1             hadoop_test       hive-0.11.0-bin.tar.gz metastore_db
apache-flume-1.4.0-bin.tar.gz.1          count.txt 
datanode          flume       hadoop-1.2.1-bin.tar.gz  
hive              mahout-distribution-0.8.tar.gz  namenode

hadoop@hadoop2:~$ more AboutHadoop.txt

Hadoop is framework written in Java.

1.  Scalable fault tolerant distributed system for large data storage & processing.

2.  Designed to solve problems that involve storing, processing & analyzing large data (Terabytes, petabytes, etc.)

3.  Programming Model is based on Google's Map Reduce.

4.  Infrastructure based on Google's Big Data & distributed file system.



less:



      less is also a command to view file content. After viewing file press q to quit.

Example:  

hadoop@hadoop2:~$ less count.txt

Press enter
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
count.txt (END)
Type "q:" & <Hit Enter> to come out of this.



hadoop@hadoop2:~$




      Head: 
      
      Displays first ten lines of a file.

Example:

hadoop@hadoop2:~$ head count.txt
one
two
three
four
five
six
seven
eight
nine
ten



      Tail: 

      Displays last 10 lines of file

Example:

hadoop@hadoop2:~$ tail count.txt

three
four
five
six
seven
eight
nine
ten
eleven
twelve


More Commands
   head  –n or tail  -n : Displays n number of lines
   head –cn or tail –cn : Displays n no of bytes of file.





MORE LINUX COMMANDS

Linux Commands - pwd | cd | ls | mkdir | rmdir | pushd | popd | clear










No comments: