Linux Shell Commands - Part 8


Hello Friends in This post I will show How to rename and move files.




'mv' command is used to rename and move files.

    root@bt:~/Desktop/Ckorner/post8# ls -l
    total 20
    -rw-r--r-- 1 root root    2 2012-07-17 02:14 post8
    drwxr-xr-x 2 root root 4096 2012-07-17 02:16 sampleDir1
    drwxr-xr-x 2 root root 4096 2012-07-17 02:16 sampleDir2
    -rw-r--r-- 1 root root   22 2012-07-17 02:18 sampleText2.txt
    -rw-r--r-- 1 root root   33 2012-07-17 02:17 sampleText.txt
   
root@bt:~/Desktop/Ckorner/post8#
   
root@bt:~/Desktop/Ckorner/post8# mv sampleText.txt renamed.txt
   
root@bt:~/Desktop/Ckorner/post8# ls -l
    total 20
    -rw-r--r-- 1 root root    2 2012-07-17 02:14 post8
    -rw-r--r-- 1 root root   33 2012-07-17 02:17 renamed.txt
    drwxr-xr-x 2 root root 4096 2012-07-17 02:16 sampleDir1
    drwxr-xr-x 2 root root 4096 2012-07-17 02:16 sampleDir2
    -rw-r--r-- 1 root root   22 2012-07-17 02:18 sampleText2.txt
   
root@bt:~/Desktop/Ckorner/post8#

    Note Here, the file has been renamed.

   
root@bt:~/Desktop/Ckorner/post8# mv sampleText.txt sampleText2.txt

    This will overwrite sampleText2.txt file.

    For this you need to use the '-i' key

   
root@bt:~/Desktop/Ckorner/post8# ls -l
    total 20
    -rw-r--r-- 1 root root    2 2012-07-17 02:14 post8
    drwxr-xr-x 2 root root 4096 2012-07-17 02:16 sampleDir1
    drwxr-xr-x 2 root root 4096 2012-07-17 02:16 sampleDir2
    -rw-r--r-- 1 root root   33 2012-07-17 02:17 sampleText2.txt
    -rw-r--r-- 1 root root   33 2012-07-17 02:21 sampleText3.txt
   
root@bt:~/Desktop/Ckorner/post8# mv -i sampleText2.txt sampleText3.txt
    mv: overwrite `sampleText3.txt'? no
   
root@bt:~/Desktop/Ckorner/post8#

    '-v' turns on the verbocity

   
root@bt:~/Desktop/Ckorner/post8# mv -v sampleText2.txt renamedNew.txt
    `sampleText2.txt' -> `renamedNew.txt'
   
root@bt:~/Desktop/Ckorner/post8#


b. To move files, the same command is used : 'mv'

    Note here how I would move files from post8 directory to sampleDir2 directory

   
root@bt:~/Desktop/Ckorner/post8# ls -l
    total 20
    -rw-r--r-- 1 root root    2 2012-07-17 02:14 post8
    -rw-r--r-- 1 root root   33 2012-07-17 02:17 renamedNew.txt
    drwxr-xr-x 2 root root 4096 2012-07-17 02:24 sampleDir1
    drwxr-xr-x 2 root root 4096 2012-07-17 02:16 sampleDir2
    -rw-r--r-- 1 root root   33 2012-07-17 02:21 sampleText3.txt
   
root@bt:~/Desktop/Ckorner/post8# mv sampleText3.txt sampleDir2/
   
root@bt:~/Desktop/Ckorner/post8# ls -l
    total 16
    -rw-r--r-- 1 root root    2 2012-07-17 02:14 post8
    -rw-r--r-- 1 root root   33 2012-07-17 02:17 renamedNew.txt
    drwxr-xr-x 2 root root 4096 2012-07-17 02:24 sampleDir1
    drwxr-xr-x 2 root root 4096 2012-07-17 02:26 sampleDir2
   
root@bt:~/Desktop/Ckorner/post8#
   
root@bt:~/Desktop/Ckorner/post8# cd sampleDir2
   
root@bt:~/Desktop/Ckorner/post8/sampleDir2# ls -l
    total 4
    -rw-r--r-- 1 root root 33 2012-07-17 02:21 sampleText3.txt
   
root@bt:~/Desktop/Ckorner/post8/sampleDir2#

    Note the sampleText3.txt is no longer in the 'post8' directory.

    Using '-i' would give warning, if the command is going to overwrite any file.

   
root@bt:~/Desktop/Ckorner/post8/sampleDir2# mv sampleText3.txt /root/Desktop/Ckorner/post8/
   
root@bt:~/Desktop/Ckorner/post8/sampleDir2# ls -l
    total 0
    root@bt:~/Desktop/Ckorner/post8/sampleDir2# cd ..
   
root@bt:~/Desktop/Ckorner/post8# ls -l
    total 20
    -rw-r--r-- 1 root root    2 2012-07-17 02:14 post8
    -rw-r--r-- 1 root root   33 2012-07-17 02:17 renamedNew.txt
    drwxr-xr-x 2 root root 4096 2012-07-17 02:24 sampleDir1
    drwxr-xr-x 2 root root 4096 2012-07-17 02:28 sampleDir2
    -rw-r--r-- 1 root root   33 2012-07-17 02:21 sampleText3.txt
   
root@bt:~/Desktop/Ckorner/post8# mv -i sampleText3.txt sampleDir1/
    mv: overwrite `sampleDir1/sampleText3.txt'? no
   
root@bt:~/Desktop/Ckorner/post8#

    Similarly '-v' would turn on the verbocity.

   
root@bt:~/Desktop/Ckorner/post8# mv -v sampleText3.txt sampleDir2/
    `sampleText3.txt' -> `sampleDir2/sampleText3.txt'
   
root@bt:~/Desktop/Ckorner/post8#


Thats All For this post, stay tuned for more.


Feel Free To Leave A Comment
If Our Article has Helped You, Support Us By Making A Small Contribution, Thank You!


0 comments: