How to rename and move the directory and files in linux

To rename and move the directory and files in linux in linux we use single command called mv.
The mv command is used for both purpose means renaming and moving the directory or file,it is system command which ships bydefault when you install any linux. The command is also applicable to all Unix based operating system.

Syntax: mv [options] SOURCE DESTINATION

Move and rename directory or file in linux

Note: mv command is applicable to both directory and files.

Here for we have created a directory called test and the example is same with file also

linux@tuxworld:~/Desktop$ cd /tmp/
linux@tuxworld:/tmp$ mkdir test
linux@tuxworld:/tmp$ ls -ld test/
drwxrwxr-x 2 linux linux 4096 Aug 18 01:41 test/
linux@tuxworld:/tmp$ 

Now we will rename the directory.

Syntax: mv dir-name new-dir-name

mv test newtest

Reference:

linux@tuxworld:/tmp$ mv test/ newtest
linux@tuxworld:/tmp$ ls -ld *test*
drwxrwxr-x 2 linux linux 4096 Aug 18 01:41 newtest
linux@tuxworld:/tmp$

Now we will move the directory

Syntax: mv dir-name /destination/path

linux@tuxworld:/tmp$ mv newtest/ /home/linux/Desktop/
linux@tuxworld:/tmp$ cd ~/Desktop/
linux@tuxworld:~/Desktop$ ls -ld newtest/
drwxrwxr-x 2 linux linux 4096 Aug 18 01:41 newtest/
linux@tuxworld:~/Desktop$

Lets see,one more example of renaming the directory

Syntax: mv dir-name /path/you/want/to/move/new-dir-name

linux@tuxworld:~/Desktop$ mv newtest/ /home/linux/sharadtest
linux@tuxworld:~/Desktop$ ls -ld /home/linux/sharadtest/
drwxrwxr-x 2 linux linux 4096 Aug 18 01:41 /home/linux/sharadtest/
linux@tuxworld:~/Desktop$

How to move multiple directories or file

Note: The below given example is applicable to directory

Syntax example: mv directory1 directory2 directoryn -t /path/of/Destination-Directory

Use -t option

linux@tuxworld:/tmp$ mkdir test1
linux@tuxworld:/tmp$ mkdir test2
linux@tuxworld:/tmp$ mkdir test3
linux@tuxworld:/tmp$ 
linux@tuxworld:/tmp$ mkdir all-test
linux@tuxworld:/tmp$ 
linux@tuxworld:/tmp$ mv test1 test2 test3 -t all-test/
linux@tuxworld:/tmp$ ls -l all-test/
total 12
drwxrwxr-x 2 linux linux 4096 Aug 18 02:12 test1
drwxrwxr-x 2 linux linux 4096 Aug 18 02:12 test2
drwxrwxr-x 2 linux linux 4096 Aug 18 02:12 test3
linux@tuxworld:/tmp$ 

How to move multiple files into destination directory or path

Syntax example: mv filename1 filename2 filenamen -t /path/of/Destination-Directory

linux@tuxworld:/tmp$ mkdir all-files-dir
linux@tuxworld:/tmp$ touch testfile{1,2,3}
linux@tuxworld:/tmp$ ls -l testfile*
-rw-rw-r-- 1 linux linux 0 Aug 18 02:15 testfile1
-rw-rw-r-- 1 linux linux 0 Aug 18 02:15 testfile2
-rw-rw-r-- 1 linux linux 0 Aug 18 02:15 testfile3
linux@tuxworld:/tmp$ 
linux@tuxworld:/tmp$ mv testfile1 testfile2 testfile3 all-files-dir/
linux@tuxworld:/tmp$ 
linux@tuxworld:/tmp$ ls -l all-files-dir/
total 0
-rw-rw-r-- 1 linux linux 0 Aug 18 02:15 testfile1
-rw-rw-r-- 1 linux linux 0 Aug 18 02:15 testfile2
-rw-rw-r-- 1 linux linux 0 Aug 18 02:15 testfile3
linux@tuxworld:/tmp$ 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.