mv : move files and directories - mrolarik/Linux-Command GitHub Wiki
คำสั่ง mv หรือ moveเป็นคำสั่งที่ใช้สำหรับย้าย (move) ไฟล์หรือไดเรกทอรี โดยย้ายจากไดเรกทอรีหนึ่งไปยังอีกที่หนึ่ง
การเรียกใช้คำสั่ง mv
- หากพิมพ์คำสั่ง
mvลงไปยัง Terminal จะเกิดข้อผิดพลาด เนื่องจากคำสั่งmvจะต้องส่งอาร์กิวเม้น (argument) ไปด้วย ดังนั้นจึงแสดงข้อผิดพลาดดังต่อไปนี้
$ mv
mv: missing file operand
Try 'mv --help' for more information.
- หากต้องการดูวิธีการใช้คำสั่ง
mvสามารถทำได้โดยพิมพ์คำสั่งmv --helpซึ่งแสดงดังต่อไปนี้
$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force do not prompt before overwriting
-i, --interactive prompt before overwrite
-n, --no-clobber do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
-u, --update move only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
-Z, --context set SELinux security context of destination
file to default type
--help display this help and exit
--version output version information and exit
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable. Here are the values:
none, off never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'
- โดยคำสั่ง
mvจะต้องระบุไฟล์หรือไดเรกทอรีต้นทาง (SOURCE) ที่ต้องการย้าย และไดเรกทอรี่ที่จัดเก็บปลายทาง (DESTINATION: DEST)
$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
การย้ายไฟล์จำนวน 1 ไฟล์
- เริ่มต้นด้วยการดูโครงสร้างของไดเรกทอรี่และไฟล์โดยพิมพ์คำสั่ง
treeผลลัพธ์ที่ได้แสดงดังต่อไปนี้
s$ tree
.
├── backup
│ ├── index.htm
│ └── test.html
├── html
│ └── index.html
├── index.htm
├── index.html
├── test.html
├── test.py
├── test.txt
├── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
└── weather.py
3 directories, 12 files
ให้สังเกตุที่จำนวนของไฟล์ซึ่งมีทั้งหมด 12 ไฟล์
- จากตัวอย่างข้างต้นหากต้องการย้ายไฟล์
test.txtไปไว้ในไดเรกทอรี่backupสามารถทำได้โดยพิมพ์คำสั่งดังต่อไปนี้
$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ └── test.txt
├── html
│ └── index.html
├── index.htm
├── index.html
├── test.html
├── test.py
├── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
└── weather.py
3 directories, 12 files
จากตัวอย่างข้างต้นเมื่อย้ายไฟล์ test.txt ไปไว้ที่ไดเรกทอรี่ backup สังเกตุได้ว่าจำนวนไฟล์ยังมีจำนวน 12 ไฟล์ เท่าเดิม
การย้ายไดเรกทอรี
- สำหรับการย้ายไดเรกทอรี หากไดเรกทอรีมีไฟล์อยู่ก็จะทำการย้ายทั้งไดเรกทอรีและไฟล์ไปพร้อมกัน
- เริ่มต้นด้วยการพิมพ์คำสั่ง
treeเพื่อตรวจสอบข้อมูลไฟล์และไดเรกทอรี
$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ └── test.txt
├── html
│ └── index.html
├── index.htm
├── index.html
├── test.html
├── test.py
├── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
└── weather.py
3 directories, 12 files
- ตัวอย่างต่อไปนี้จะทดสอบการย้ายไดเรอทอรี
tmpไปไว้ในไดเรกทอรีbackupดังนั้นสามารถทำได้โดยพิมพ์คำสั่งดังต่อไปนี้
$ mv tmp/ backup/
จากนั้นให้พิมพ์คำสั่ง tree เพื่อดูการเปลี่ยนแปลง
$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ ├── test.txt
│ └── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
├── html
│ └── index.html
├── index.htm
├── index.html
├── test.html
├── test.py
└── weather.py
3 directories, 12 files
- จากตัวอย่างข้างต้น สังเกตุได้ว่า ไฟล์ที่อยู่ในไดเรกทอรี
tmpประกอบด้วยindex.html, test.html และ weather.pyจะถูกย้ายตามไปอยู่ในไดเรกทอรีbackupด้วย
การย้ายไฟล์จำนวนมากกว่า 1 ไฟล์
- การย้ายไฟล์ด้วยคำสั่ง
mvสามารถย้ายไฟล์ได้มากกว่า 1 ไฟล์ในคำสั่งเดียว - เริ่มต้นด้วยการสร้างไดเรกทอรี `tmp`` ด้วยคำสั่ง
$ mkdir tmp1
จากนั้นพิมพ์คำสั่ง tree เพื่อตรวจสอบว่ามีไดเรกทอรี tmp1 ปรากฎอยู่หรือไม่ ผลลัพธ์ที่ได้แสดงดังต่อไปนี้
$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ ├── test.txt
│ └── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
├── html
│ └── index.html
├── index.htm
├── index.html
├── test.html
├── test.py
├── tmp1
└── weather.py
4 directories, 12 files
- จากนั้นทดสอบการย้าย (mv) ไฟล์จำนวนมากกว่าหนึ่งไฟล์ โดยพิมพ์คำสั่งดังต่อไปนี้
$ mv index.htm index.html tmp1/
- โดยคำสั่ง
mvจะต้องส่งอาร์กิวเม้น (argument) โดยอาร์กิวเม้นตัวแรก (n) ถึงตัวที่ n-1 จะเป็นไฟล์หรือไดเรกทอรีที่ต้องการย้าย และอาร์กิวเม้นตัวสุดท้าย (n) จะระบุเป็นไดเรกทอรีปลายทาง ดังนั้น คำสั่งmv index.htm index.html tmp1/จึงหมายถึงการย้ายไฟล์index.htm และ index.htmlไปไว้ยังไดเรกทอรีปลายทางคือtmp1/ - ขั้นตอนต่อไป ตรวจสอบความถูกต้องด้วยการพิมพ์คำสั่ง
treeเพื่อดูผลลัพธ์
$ tree
.
├── backup
│ ├── index.htm
│ ├── test.html
│ ├── test.txt
│ └── tmp
│ ├── index.html
│ ├── test.html
│ └── weather.py
├── html
│ └── index.html
├── test.html
├── test.py
├── tmp1
│ ├── index.htm
│ └── index.html
└── weather.py
4 directories, 12 files
- สำหรับคำสั่ง
mvสามารถใช้งานร่วมกับเครื่องหมายที่เรียกว่าwildcardเช่นเครื่องหมาย asterisk (*) หรือquestion mark (?)ได้เช่นเดียวกันกับคำสั่งcpสามารถอ่านรายละเอียดเพิ่มเติมได้ที่บทความ cp : copy file and directory