Saturday, 19 December 2015

5 Practical Uses Of Move(mv) Command in Unix/Linux

In This article we will see the practice uses of move command which we use on regular basis in our day to day activity work. Here we have a source directory /home/baba which has two text files and a directory. We will perform all the operation in /home/baba source path only.

$ ls -ltr
-rw-r--r-- 1 baba None 0 Dec 19 22:59 abc.txt
-rw-r--r-- 1 baba None 0 Dec 19 22:59 pqr.txt
drwxr-xr-x+ 1 baba None 0 Dec 19 22:45 Tgt

1. Move file abc.txt from /home/baba to /home/baba/Tgt folder
$ mv /home/baba/abc.txt /home/baba/Tgt
  
Output: Will move the file abc.txt at directory /home/baba/Tgt
$ ls -ltr /home/baba/Tgt
-rw-r--r-- 1 baba None 0 Dec 19 22:59 abc.txt

Note: Unlike to copy command, timestamp of a file will not change by move. It Will remain same as in source directory.
  
If yoy are in directory /home/baba, then below will work.
$ mv abc.txt /home/baba/Tgt

2. Move multiple files from source to destination.
$ mv abc.txt pqr.txt /home/baba/Tgt

Output:
$ ls -ltr /home/baba/Tgt
-rw-r--r-- 1 baba None 0 Dec 19 22:59 abc.txt
-rw-r--r-- 1 baba None 0 Dec 19 22:59 pqr.txt

If you try to move a file which is not available in source then cp will raise an error:
mv: cannot stat ‘file’: No such file or directory

Move all the .txt files from source to destination
$ mv *.txt /home/baba/Tgt

Output: Will copy all the .txt files in destination directory /home/baba/Tgt
$ ls -ltr /home/baba/Tgt
-rw-r--r-- 1 baba None 0 Dec 19 22:59 abc.txt
-rw-r--r-- 1 baba None 0 Dec 19 22:59 pqr.txt

3. Rename the file
$ mv abc.txt xyz.txt

Output: Will rename the file abc.txt to xyz.txt. Actually move has not renamed the file, if you see move properties, it moves the file from one place to other.In this case it has moved the file in same directory.
-rw-r--r--  1 baba None 0 Dec 19 22:59 xyz.txt

4. Move in Verbose Mode
It is always good to see what is going on, what mv is doing. We can see it using -v switch.

$ mv -v *.txt /home/baba/Tgt

Output:
‘abc.txt’ -> ‘/home/baba/Imp/abc.txt’
‘pqr.txt’ -> ‘/home/baba/Imp/pqr.txt’

5. How to move complete directory to the destination.
Let suppose we want to copy all the files from /home/baba to /home/Source
ls -ltr  /home/baba. Unlike copy command move does not use any switch (like cp use R) to copy recursively.

-rw-r--r-- 1 baba None 0 Dec 19 22:59 abc.txt
-rw-r--r-- 1 baba None 0 Dec 19 22:59 pqr.txt
drwxr-xr-x+ 1 baba None 0 Dec 19 22:45 Tgt

$ mv * /home/Source/

Output: Complete directory has been moved to the destination path.
ls -ltr /home/Source
-rw-r--r-- 1 baba None 0 Dec 19 22:59 abc.txt
-rw-r--r-- 1 baba None 0 Dec 19 22:59 pqr.txt
drwxr-xr-x+ 1 baba None 0 Dec 19 22:45 Tgt

Conclusion: If you observer closely:
1. Move does not change the time stamp of the file.
2. Unlike copy where files are available in both source as well destination, move removes the file from source path hence file is available only in destination path.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...