Lệnh copy trong linux. Cũng giống như khi sử dụng windows, bạn luôn có nhu cầu copy các file từ thư mục này sang thư mục khác để sử dụng. Đối với linux sử dụng môi trường dòng lệnh thì bạn không thể dùng chuột để copy/paste được. Dưới đây là cách sử dụng lệnh copy trong linux, với các tham số từ cơ bản tới nâng cao giúp bạn nắm vững được công việc này. Hãy bắt đầu nào!
Lệnh Copy trong Linux.
Lệnh copy trong linux là CP, chúng ta sẽ tìm hiểu cú pháp của lệnh CP này để thực hiện copy các file và thư mục trong linux.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
[[email protected] tmp]# cp --help Usage: cp [OPTION]... [-T] SOURCE DEST or: cp [OPTION]... SOURCE... DIRECTORY or: cp [OPTION]... -t DIRECTORY SOURCE... Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. Mandatory arguments to long options are mandatory for short options too. -a, --archive same as -dR --preserve=all --backup[=CONTROL] make a backup of each existing destination file -b like --backup but does not accept an argument --copy-contents copy contents of special files when recursive -d same as --no-dereference --preserve=links -f, --force if an existing destination file cannot be opened, remove it and try again (redundant if the -n option is used) -i, --interactive prompt before overwrite (overrides a previous -n option) -H follow command-line symbolic links in SOURCE -l, --link link files instead of copying -L, --dereference always follow symbolic links in SOURCE -n, --no-clobber do not overwrite an existing file (overrides a previous -i option) -P, --no-dereference never follow symbolic links in SOURCE -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all -c same as --preserve=context --no-preserve=ATTR_LIST don't preserve the specified attributes --parents use full source file name under DIRECTORY -R, -r, --recursive copy directories recursively --reflink[=WHEN] control clone/CoW copies. See below. --remove-destination remove each existing destination file before attempting to open it (contrast with --force) --sparse=WHEN control creation of sparse files. See below. --strip-trailing-slashes remove any trailing slashes from each SOURCE argument -s, --symbolic-link make symbolic links instead of copying -S, --suffix=SUFFIX override the usual backup suffix -t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY -T, --no-target-directory treat DEST as a normal file -u, --update copy 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 -x, --one-file-system stay on this file system -Z, --context=CONTEXT set security context of copy to CONTEXT --help display this help and exit --version output version information and exit |
Có rất nhiều tham số, chúng ta sẽ tìm hiểu những tham số cơ bản cần thiết nhất.
Cú pháp cơ bản như trên:
1 2 3 4 5 |
cp SOURCE DEST cp SOURCE DIRECTORY cp SOURCE1 SOURCE2 SOURCE3 SOURCEn DIRECTORY cp [OPTION] SOURCE DEST cp [OPTION] SOURCE DIRECTORY |
Giải thích: SOURCE: file hay thư mục nguồn. DEST: file hay thư mục đích. OPTION là tham số kèm theo.
Như lệnh trên, lệnh 1 là copy file nguồn thành 1 file khác (cùng thư mục), lệnh 2 là copy file nguồn vào 1 thư mục khác.
Lệnh 3 là copy nhiều file vào 1 thư mục khác.
lệnh 4,5 tương tự nhưng có kèm thêm tham số tùy chọn.
Ví dụ về sử dụng lệnh copy trong linux.
Để dễ hiểu, chúng ta sẽ tìm hiểu qua một vài ví dụ thực tế.
Trong thư mục hiện hành đang có những file như sau:
ta sẽ copy file baihat.docx thành file mới có tên danhsachbaihat.docx
1 2 |
cp baihat.docx danhsachbaihat.docx ls * -ln |
kết quả ta có file mới với tên: danhsachbaihat.docx
Copy file baihat.docx và huongdan.pdf vào thư mục backup:
1 2 |
cp baihat.docx huongdan.pdf backup/ ls backup/ |
Nếu thư mục backup nằm ở một vị trí khác ví dụ /home/backup thì:
1 2 |
cp baihat.docx huongdan.pdf /home/backup ls /home/backup |
Tùy chọn khi copy:
Verbose (-v) tùy chọn này sẽ hiện thị trạng thái khi copy.
1 2 |
cp -v huongdan.pdf /tmp ls /tmp |
Preserve file attributes (-p)
Tùy chọn này sẽ bỏ qua thuộc tính của file gốc.
1 2 |
cp -p baihat.pdf /tmp/dsbaihat.pdf cp -p filename /path/to/new/location/myfile |
Các thuộc tính của file bao gồm:
- Modification time/date
- Access time
- File flags
- File mode
- User ID (UID)
- Group ID (GID)
- Access Control Lists (ACLs)
- Extended Attributes (EAs)
Copy tất cả các file:
Sử dụng ký tự đại diện * để copy tất cả các file trong thư mục vào thư mục đích.
1 2 3 4 |
cp * backup/ cp * /home/backup cp /tmp/* /home/backup cp -vp * /home/user/filelist |
Trong thư mục vừa copy, có tồn tại 1 thư mục là backup. Muốn copy cả thư mục này sang thư mục khác thì sử dụng tham số -R (Recursive copy)
1 |
cp -R * /home/backup |
Như vậy qua bài viết bạn đã có thể nắm được cách copy file trong linux rồi.
Chúc các bạn thành công!