Friday 2 November 2018

Move / Rename files, Directory - MV Command in Unix / Linux

MV Command, LPI Study Materials, LPI Guides, LPI Tutorial and Materials, LPI Certification

Q. How to rename a file or directory in unix (or linux) and how to move a file or directory from the current directory to another directory?


Unix provides a simple mv (move) command which can be used to rename or move files and directories. The syntax of mv command is

mv [options] oldname newname

The options of mv command are

f : Do not prompt before overwriting a file. 
i : Prompts for the user input before overwriting a file.

If the newname already exists, then the mv command overwrites that file. Let see some examples on how to use mv command.

Unix mv command examples


1. Write a unix/linux command to rename a file?


Renaming a file is one of the basic features of the mv command. To rename a file from "log.dat" to "bad.dat", use the below mv command

> mv log.dat bad.dat

Note that if the "bad.dat" file already exists, then its contents will be overwritten by "log.dat". To avoid this use the -i option, which prompts you before overwriting the file.

mv -i log.dat bad.dat
mv: overwrite `bad.dat'?

2. Write a unix/linux command to rename a directory?


Just as renaming a file, you can use the mv command to rename a directory. To rename the directory from docs to documents, run the below command

mv docs/ documents/

If the documents directory already exists, then the docs directory will be moved in to the documents directory.

3. Write a unix/linux command to move a file into another directory?


The mv command can also be used to move the file from one directory to another directory. The below command moves the sum.pl file in the current directory to /var/tmp directory.

mv sum.pl /var/tmp/

If the sum.pl file already exists in the /var/tmp directory, then the contents of that file will be overwritten.

4. Write a unix/linux command to move a directory in to another directory?


Just as moving a file, you can move a directory into another directory. The below mv command moves the documents directory into the tmp directory

mv documents /tmp/

5. Write a unix/linux command to move all the files in the current directory to another directory?


You can use the regular expression pattern * to move all the files from one directory to another directory.

mv * /var/tmp/

The above command moves all the files and directories in the current directory to the /var/tmp/ directory.

6. mv *


What happens if you simply type mv * and then press enter?

It depends on the files you have in the directory. The * expands to all the files and directories. Three scenarios are possible.

◈ If the current directory has only files, then the contents of all the files (except one file) will be written in to the one file. The one file is the last file which depends on the pattern *.
◈ If the current directory contains only directories, then all the directories (except one directory) will be moved to another directory.
◈ If the current directory contains both files and directories, then it depends on the expansion of the *. If the pattern * gives the last one as directory then all the files will be moved to that directory. Otherwise the mv command will fail.

Some Tips:

◈ Try to avoid mv *
◈ Avoid moving large number of files.

Related Posts

0 comments:

Post a Comment