Cracking IT Interview

​​

mv (Move / Rename)


This command is used move file from one directory to another. When we move file at the same location with different file names, it is considered as re-naming so it is also called as Rename command.


mv source_file target_file


  • source_file may be only a source file name / file name preceded by full path for its location 
  • target_file may be only a target file name / file name preceded by full path for its location 
  • when the source file path and target file path is same with different file names, it is used as rename.
  • syntax is same as copy command







$pwd
/home/pinku/my_dir/A_dir

$
$ls
A1              file1.txt       file2.txt       myfile1.txt     myfile2.txt

$
$mv myfile1.txt ./../B_dir/

$
$ls
A1              file1.txt       file2.txt       myfile2.txt

$
$cd ../B_dir/

$
$ls
myfile1.txt

$
$pwd
/home/pinku/my_dir/B_dir

$
$mv myfile1.txt myfile2.txt
$ls
myfile2.txt
$


Difference between Copy command and move command: 


  • Source file does not exist after performing move command, it exists in case of copy command.


  • New file created (target file) in case of copy command will have changed modification time and will be current date and time by default. In case of move, it remains same as that of source file modification time.


  • Destination file will have new i-node number in case of copy, i-node number remains same in case of move.








Optins to use with mv command:


  • mv -v - verbose mode on, prompt the filename in the screen


  • mv -i - interactively ask you to move file for confirmation, press "y" for yes


  • mv -f - forcefully, move file without prompting you anything


  • mv -n - Do not overwrite an existing target file


Example:


$pwd
/home/pinku/my_dir/C_dir
$ls
A1              file1.txt       file2.txt       myfile1.txt     myfile2.txt


verbose mode on, prompt the filename in the screen


$mv -v myfile2.txt ./../G_dir
myfile2.txt -> ./../G_dir/myfile2.txt

$
$cd ../G_dir

$
$pwd
/home/pinku/my_dir/G_dir

$
$ls
A_dir           file1.txt       file2.txt       myfile2.txt

$


interactively ask you to move file for confirmation, press "y" for yes


$cd -
/home/pinku/my_dir/C_dir

$
$ls
A1              file1.txt       file2.txt       myfile1.txt

$

$mv -i myfile1.txt file3.txt

$

$ls
A1              file1.txt       file2.txt       file3.txt

$
$mv -i file3.txt ./../file6.txt
$

$ls
A1              file1.txt       file2.txt

$
$cd ..

$
$ls -m
A_dir, B_dir, C_dir, G_dir, file1.txt, file2.txt, file3.txt, file4.txt, file6.txt


forcefully, move file without prompting you anything


$mv -f file6.txt /home/pinku/my_dir/C_dir/

$
$cd /home/pinku/my_dir/C_dir/

$
$ls
A1              file1.txt       file2.txt       file6.txt


Do not overwrite an existing destination file


$mv -n file1.txt file6.txt
$ls
A1              file1.txt       file2.txt       file6.txt
$mv -ni file1.txt file6.txt
overwrite file6.txt? (y/n [n]) n
not overwritten
$



Questions & Answers:


Qs: Difference between Copy and move ?


please refer this above.


Qs: How to create a back up file and remove the original file at the same time by single command ? 


mv -ir filename filename.backup



NEXT ->