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