cp -rมีวัตถุประสงค์เพื่อคัดลอกไฟล์ซ้ำและcp -Rคัดลอกไดเรกทอรีซ้ำ แต่ฉันได้ตรวจสอบแล้วและทั้งคู่ดูเหมือนจะคัดลอกทั้งไฟล์และไดเรกทอรีสิ่งเดียวกัน ดังนั้นอะไรคือความแตกต่างจริง ๆ ?
cp -rมีวัตถุประสงค์เพื่อคัดลอกไฟล์ซ้ำและcp -Rคัดลอกไดเรกทอรีซ้ำ แต่ฉันได้ตรวจสอบแล้วและทั้งคู่ดูเหมือนจะคัดลอกทั้งไฟล์และไดเรกทอรีสิ่งเดียวกัน ดังนั้นอะไรคือความแตกต่างจริง ๆ ?
คำตอบ:
ในขณะที่-Rposix มีการกำหนดชัดเจน-rไม่ใช่พกพา!
บน Linux ในแอฟริกาและ BusyBox การใช้งานcp, -rและ-Rเทียบเท่า
ในด้านอื่น ๆ ที่คุณสามารถอ่านในPOSIXหน้าคู่มือของcp, -rพฤติกรรมการดำเนินงานที่กำหนดไว้
* If neither the -R nor -r options were specified, cp shall take actions based on the type and contents of the file referenced by the symbolic link, and not by the symbolic link itself. * If the -R option was specified: * If none of the options -H, -L, nor -P were specified, it is unspecified which of -H, -L, or -P will be used as a default. * If the -H option was specified, cp shall take actions based on the type and contents of the file referenced by any symbolic link specified as a source_file operand. * If the -L option was specified, cp shall take actions based on the type and contents of the file referenced by any symbolic link specified as a source_file operand or any symbolic links encoun- tered during traversal of a file hierarchy. * If the -P option was specified, cp shall copy any symbolic link specified as a source_file operand and any symbolic links encoun- tered during traversal of a file hierarchy, and shall not follow any symbolic links. * If the -r option was specified, the behavior is implementation- defined.
-rจะคัดลอกไฟล์จริงที่ชี้โดยลิงก์สัญลักษณ์ในขณะที่-Rจะคัดลอกลิงก์สัญลักษณ์ซึ่งเป็นสิ่งที่มันตั้งใจไว้เกือบตลอดเวลา
ความแตกต่างคือหนึ่งใช้ตัวพิมพ์เล็ก "R" และอื่น ๆ ใช้ตัวพิมพ์ใหญ่ "R" ยิ่งไปกว่านั้นไม่มีความแตกต่าง สิ่งเดียวกันถ้าคุณใช้--recursiveตัวเลือกยาว
-Rเพียงอย่างเดียวเสมอและในความเป็นจริงแล้วบางหน้าคู่มือของระบบที่กล่าวถึงยังแนะนำอีกด้วย
ตัวพิมพ์เล็ก-rเป็นตัวเลือกที่เก่ากว่านำมาใช้ใน 4.1BSD ซึ่งจะคัดลอกไฟล์ที่ไม่ใช่ไดเรกทอรีทั้งหมดเป็นไฟล์ กล่าวคือหากพบอุปกรณ์หรือ FIFO อุปกรณ์จะเปิดอ่านเนื้อหาและสร้างไฟล์ที่ปลายทางด้วยเนื้อหา
ตัวพิมพ์ใหญ่-Rเป็นตัวเลือกมาตรฐาน (แนะนำให้รู้จักกับ BSD ใน 4.4BSD แม้ว่ารุ่นก่อนหน้านี้จะเป็นคำพ้องความหมาย-r) ซึ่งในการเผชิญหน้ากับอุปกรณ์ FIFO หรือไฟล์พิเศษอื่น ๆ ทำให้ไฟล์พิเศษที่เทียบเท่าที่ปลายทาง
การใช้งานหลายอย่างยังคงรักษาความแตกต่างนี้ แต่บางส่วน (รวมถึงรุ่น GNU ทั่วไปกับ Linux) เพียงให้-Rความหมาย-rที่มีคำพ้องความหมาย
ใน OS X และ FreeBSD เวอร์ชันเก่า-rนั้นเหมือน-R -L --copy-contentsใน coreutils หรือตาม symlink และอ่านเนื้อหาของไฟล์พิเศษและ FIFO
mkdir a;touch b;ln -s $PWD/b a;cp -r a cแทนที่ symlink ด้วยไฟล์เป้าหมายใน OS X mkdir a;mkfifo a/b;cp -r a cรับการบล็อกการอ่าน FIFO และmkdir a;ln -s /dev/zero a;cp -r a bเริ่มเติมb/zeroด้วยศูนย์
จากcpหน้า man ใน OS X และ FreeBSD เวอร์ชันเก่า:
Historic versions of the cp utility had a -r option. This implementation
supports that option; however, its use is strongly discouraged, as it
does not correctly copy special files, symbolic links, or fifo's.
ใน FreeBSD เวอร์ชันใหม่-rเทียบเท่ากับ-RL:
Historic versions of the cp utility had a -r option. This implementation
supports that option, however, its behavior is different from historical
FreeBSD behavior. Use of this option is strongly discouraged as the
behavior is implementation-dependent. In FreeBSD, -r is a synonym for
-RL and works the same unless modified by other flags. Historical imple-
mentations of -r differ as they copy special files as normal files while
recreating a hierarchy.
http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html :
--copy-contentsหากการคัดลอกซ้ำให้คัดลอกเนื้อหาของไฟล์พิเศษใด ๆ (เช่นไฟล์ FIFO และไฟล์อุปกรณ์) ราวกับว่าเป็นไฟล์ปกติ หมายความว่าพยายามอ่านข้อมูลในแต่ละไฟล์ต้นฉบับและเขียนไปยังปลายทาง โดยปกติจะเป็นความผิดพลาดในการใช้ตัวเลือกนี้เนื่องจากโดยปกติแล้วจะมีผลกระทบที่ไม่พึงประสงค์ในไฟล์พิเศษเช่น FIFO และไฟล์ที่มักพบใน
/devไดเรกทอรี ในกรณีส่วนใหญ่cp -R --copy-contentsจะแขวนไปเรื่อย ๆ พยายามที่จะอ่านจาก FIFOs และไฟล์พิเศษเช่นและมันจะเติมดิสก์ปลายทางของคุณถ้าคุณใช้มันในการคัดลอก/dev/console/dev/zeroตัวเลือกนี้จะไม่มีผลยกเว้นการคัดลอกซ้ำและไม่มีผลต่อการคัดลอกลิงก์สัญลักษณ์
หนึ่งในความแตกต่างฉันพบว่า -r ไม่ได้คัดลอกไดเรกทอรีที่ซ่อนอยู่ในขณะที่ -R คัดลอกไดเรกทอรีที่ซ่อนอยู่
ฉันทดสอบมัน. git directory ในไดเรกทอรีเป้าหมายและมาถึงข้อสรุปข้างต้น ฉันใช้ centOS อยู่ในขณะนี้
ฉันอาจจะผิด แต่มันก็เปิดการสนทนา
-rและ-Rทั้งคัดลอกไดเรกทอรีที่ซ่อนอยู่