Moving or rename files and directory using Linux mv command - Basic Linux Command.

 

Moving or rename files and directory using Linux mv command - Basic Linux Command.

 

Renaming Linux file and folder using Linux mv command, the mv command can be use to moving Linux files and directory to another location on the Linux system.

 

Command name: mv

 

The Linux command 'mv' can be use to rename files or to rename directory in Linux system. The 'mv' command also can be use to moving the files or to moving directory to different directory. The 'mv' command can be interpret as copying and then deleting the files or directory but this is done in much faster paste than manually copying and then deleting the files or folder.

 

Command Description:

Moving or renaming files or directory.

Command type:

In Fedora Core 5, mv command is aliased to 'mv -i', that means every mv command is execute in interactive mode.

 

[root@fedora ~]# type mv

mv is aliased to `mv -i'

 

Options that can be use with mv command:

-i, --interactive, interactive mode and prompt before overwriting.

-f, --force, force to overwrite even if the files or folder already exists.

-u, --update, move when only the file is newer or not exists.

 

Rename Linux files using mv command.

 

The example below show how to use the mv command to rename a file install.log to install-fedora.log

 

[root@fedora ~]# pwd

/root

 

[root@fedora ~]# ls -l

total 100

-rw------- 1 root root 1209 Apr 9 21:38 anaconda-ks.cfg

drwx------ 2 root root 4096 Apr 8 19:06 Desktop

-rw-r--r-- 1 root root 28948 Apr 9 21:26 install.log

-rw-r--r-- 1 root root 4152 Apr 9 19:56 install.log.syslog

-rw-r--r-- 1 root root 28948 Apr 9 21:26 X.txt

[root@fedora ~]# mv install.log install-fedora.log

 

Verify using the ls command.

 

[root@fedora ~]# ls -l

total 100

-rw------- 1 root root 1209 Apr 9 21:38 anaconda-ks.cfg

drwx------ 2 root root 4096 Apr 8 19:06 Desktop

-rw-r--r-- 1 root root 28948 Apr 9 21:26 install-fedora.log

-rw-r--r-- 1 root root 4152 Apr 9 19:56 install.log.syslog

-rw-r--r-- 1 root root 28948 Apr 9 21:26 X.txt

[root@fedora ~]#

 

Command explanations:

 

• The 'pwd' command is to print the current directory that you are in:

 

pwd

 

• The use of 'ls -l' command is to list the contents inside the directory:

 

ls -l

 

• This is the part that where we use the 'mv' commad to rename file:

 

mv install.log install-fedora.log

 

• The 'ls -l' command is use again to verify the changes:

 

ls -l

 

Rename Linux files, base on file inode number.

 

To rename file or directory in Linux using the inode number, we need to know their inode number. Use the ' ls -i ' to find out the inode number for the files or directory.

 

[root@fedora ~]# ls -i

648389 anaconda-ks.cfg 97315 example-directory 1847716 install.log.syslog

97582 Desktop 1847715 install-fedora.log 648395 X.txt

 

Now let use the example-directory with inode number 97315 as an experiment for the example.

Now, execute the find command to find the example-directory inode number than rename that directory to new-directory-name.

 

[root@fedora ~]# find . -inum 97315 -exec mv {} new-directory-name \;

find: ./example-directory: No such file or directory

 

To verify the change on the directory name, use the ls command with the ' -i ' option to list file and folder including their inode number.

 

[root@fedora ~]# ls -i

648389 anaconda-ks.cfg 1847715 install-fedora.log 97315 new-directory-name

97582 Desktop 1847716 install.log.syslog 648395 X.txt

 

Command explanations:

 

• The 'ls -i' command is usually to get the inode number for file and directory in Linux or Unix system:

 

ls -i

 

• This line of command use 'find' command to find the inode number given than execute the 'mv' command to rename the file or directory:

 

find . -inum 97315 -exec mv {} new-directory-name \;

 

• To verify the changes to the filename 'ls -i' is use again:

 

ls -i

 

Moving files or directory to another location on the system.

 

The mv command is use to move or to rename a file or directory. This command is useful when you need to rename file or directory and to move file or directory to another location, for example:

 

[root@fedora ~]# mv install.log test/

 

The command above will move a file ( install.log ) from current directory and put the install.log file to the test directory.

 

[root@fedora ~]# mv -i install.log test/

mv: overwrite `test/install.log'? y

 

The command above will move a file ( install.log ) from current directory and put the install.log file to the test directory in interactive mode ( -i ), if the destination directory contain the file that used the same filename the command then prompt for user input to overwrite the file

 

[root@fedora5 ~]# mv install.log install-fedoracore.log

 

The command above will rename a file install.log to new file name install-fedoracore.log

 

[root@fedora5 ~]# mv * /etc/tmp/

 

The command above will move everything from current directory to destination directory that we specified in this example to /etc/tmp/ directory.

 

NAME:

mv - move (rename) files

 

Usage: mv [OPTION]... SOURCE DEST

or: mv [OPTION]... SOURCE... DIRECTORY

or: mv [OPTION]... --target-directory=DIRECTORY SOURCE...

Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

 

for more information on using mv command:

# info mv

# man mv

# mv --help

 

Keywords: rename, move, moving, rename file, moving file, rename linux files, moving linux files, mv command, rename directory, rename folder, rename linux folder, mv linux files, moving linux directory, rename command.

 

1 comment

18
Dec

Lifesaver commands

Thanks for this tutorial. My system just crashed (in a Virtual machine, ofcourse :P) and I had to recover contents of a file using the recovery console. Being a newbie, I did not know many options. My plan was to open the file and screen capture the contents of the file. For this I need to copy it to .txt (becoz it seemed only text files can be displayed in recovery mode). But I am unable to copy it, I think, becoz the file extension was .abw or the file name contained a space. Then I remembered that files have inode numbers and with the help of this tutorial, I am able to copy it to .txt. Now I will atleast get to see the contents.