Linux mv Command

Blogs ❯❯ Linux

Image could not load

Image by pixabay

What is mv Command in Linux ?

Linux में use होने वाली mv command बहुत ही common but important command है। अगर आप Linux के बारे में ज्यादा नहीं जानते हैं तब भी आपने कही न कहीं इसका use जरूर किया होगा। mv command का main purpose files और directories को किसी दूसरी location पर move करना है।

Linux mv Syntax

mv command का basic syntax बहुत simple है , आपको बस उसी file को point out करना है जिसे location पर move करना है।

$ mv file/dir ~location

Linux mv Example

Example के लिए, मेरी current directory में file.txt file है जिसे dir1 directory में move करना है।

$ tree
.
├── dir1
└── file.txt

1 directory, 1 file
$ mv file.txt ./dir1
$ tree
.
└── dir1
    └── file.txt

1 directory, 1 file

Example में आप देख सकते हैं कि file.txt file dir1 directory में move हो गयी है।

What is tree ?

Linux में tree command , file structure को traverse करके tree like format में files और directories को list करता है।
यह काफी useful command है अगर आप quickly किसी directory structures और किसी certain file की location को investigate करना चाहें।

learn more about tree command ...

अगर file किसी दूसरी directory में है तो आपको वो path pass कर देना है। Example के लिए अभी हमने जो file dir1 में move की थी उसे फिर से current directory में लाने के लिए कुछ इस तरह से command run करेंगे।

$ mv ./dir1/file.txt ./

Move multiple files

multiple files को move करने के लिए आपको space के साथ files के name pass करना हैं।

$ mv file_1 file_2 ./location

Linux move directory

जिस तरह से आपने file को move की ठीक उसी तरह से आप directories को भी move कर सकते हैं। Example के लिए एक directory dir2 बनाते हैं , फिर उसे dir1 में move करेंगे।

$ mkdir dir2
$ tree 
.
├── dir1
├── dir2
└── file.txt

2 directories, 1 file
$ mv dir2 ./dir1
$ tree
.
├── dir1
│   └── dir2
└── file.txt

2 directories, 1 filee

तो कुछ इस तरह से आप Linux में files और directories को need के according दूसरी locations पर move कर सकते हैं।

Rename File and Directory

mv command की help से आप किसी file और directory को rename भी कर सकते हैं।

Example -

$ ls 
dir1  file.txt
$ mv file.txt newfile_name.txt
$ ls 
dir1  newfile_name.txt

और ठीक इसी तरह से आप किसी directory का name change कर सकते हैं।

$ mv dir1 new_dir
$ ls 
new_dir  newfile_name.txt

Linux mv Command Options

अभी जो आपने अभी examples देखे वो normal example थे , हालाँकि mv command में कई सारे options available हैं जिन्हे mv --help command run करके देख सकते हैं।

$ mv --help

उनमे से कुछ Important options को examples के साथ देखेंगे।

Linux mv -i

file move करते समय अगर targeted location पर पहले से कोई file मौजूद है तो file overwrite हो जायगी , ऐसे में -i option को pass करके आप file move करने से पहले user से ये confirm करा सकते हैं , कि file को overwrite करना है या नहीं।

अच्छे से समझने के लिए new_dir में same name की एक file बना लेते हैं। फिर file को move करते हैं।

$ touch new_dir/newfile_name.txt
$ tree 
.
├── new_dir
│   └── newfile_name.txt
└── newfile_name.txt

1 directory, 2 files
$ mv -i newfile_name.txt ./new_dir
mv: overwrite './new_dir/newfile_name.txt'? yes
$ tree
.
└── new_dir
    └── newfile_name.txt

1 directory, 1 file

confirmation के लिए आप yes / y pass कर सकते हैं , इसके अलावा कुछ pass करेंगे तो वो as a no ही treat होगा।

Linux mv -f

-f option को pass करने पर कोई confirmation message show नहीं होगा और file overwrite हो जायगी। अगर same name की file मजूद है तो।

$ mv -f filepath ./location

Linux mv- n

हालाँकि अगर आप existing file को overwrite नहीं करना चाहते हैं तो , -n option का use कर सकते हैं।

$ mv -n file1 directory

Linux mv -u

-u option pass करने से file तभी overwrite होगी जब file source / file content नया होगा।

$ mv -u file1 directory
Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook