Tôi là Duyệt

Shell - Mọi thao tác với tệp và thư mục trên Bash

Shell bash trên Linux nếu như biết khai thác thì nó sẽ là một công cụ rất mạnh, trong bài này mình sẽ liệt kê các thao tác với tệp và thư mục (copy, move, rename, zip, …). Like a hacker :))

copy a file

Copy readme.txt vào thư mục documents

cp readme.txt documents/

https://github.com/you-dont-need/You-Dont-Need-GUI#duplicate-a-fileduplicate a file

cp readme.txt readme.bak.txt

https://github.com/you-dont-need/You-Dont-Need-GUI#copy-a-folderhttps://draft.blogger.com/nullcopy a folder

Copy thư mục myMusic vào thư mục myMedia

cp -a myMusic myMedia/
# or
cp -a myMusic/ myMedia/myMusic/

https://github.com/you-dont-need/You-Dont-Need-GUI#duplicate-a-folderhttps://draft.blogger.com/nullduplicate a folder

Chú ý dấu /

cp -a myMusic/ myMedia/
# or if `myMedia` folder doesn't exist
cp -a myMusic myMedia/

https://github.com/you-dont-need/You-Dont-Need-GUI#move-a-filemove a file

Di chuyển file readme.txt vào thư mục documents/

mv readme.txt documents/

Luôn sử dụng dấu / cuối thư mục, for this reason.

https://github.com/you-dont-need/You-Dont-Need-GUI#rename-a-filehttps://draft.blogger.com/nullrename a file

Đổi tên bằng cách move nó

mv readme.txt README.md

https://github.com/you-dont-need/You-Dont-Need-GUI#move-a-folderhttps://draft.blogger.com/nullmove a folder

Tương tự di chuyển file

mv myMedia myMusic/
# or
mv myMedia/ myMusic/myMedia

https://github.com/you-dont-need/You-Dont-Need-GUI#rename-a-folderhttps://draft.blogger.com/nullrename a folder

mv myMedia/ myMusic/

https://github.com/you-dont-need/You-Dont-Need-GUI#create-a-new-filehttps://draft.blogger.com/nullcreate a new file

Tạo file rỗng

touch 'new file'

or

> 'new file'

https://github.com/you-dont-need/You-Dont-Need-GUI#create-a-new-folderhttps://draft.blogger.com/nullcreate a new folder

mkdir 'untitled folder'

or

mkdir -p 'path/may/not/exist/untitled folder'

https://github.com/you-dont-need/You-Dont-Need-GUI#show-filefolder-sizehttps://draft.blogger.com/nullshow file/folder size

stat -x readme.md

or

du -sh readme.md

https://github.com/you-dont-need/You-Dont-Need-GUI#open-a-file-with-the-default-programhttps://draft.blogger.com/nullopen a file with the default program

Mở file trên GUI

open file       # on macOS
xdg-open file   # on Linux

https://github.com/you-dont-need/You-Dont-Need-GUI#zip-a-folderhttps://draft.blogger.com/nullzip a folder

Nén zip 1 thư mục

zip -r archive_name.zip folder_to_compress

https://github.com/you-dont-need/You-Dont-Need-GUI#unzip-a-folderhttps://draft.blogger.com/nullunzip a folder

Giải nén file zip

unzip archive_name.zip

https://github.com/you-dont-need/You-Dont-Need-GUI#remove-a-filehttps://draft.blogger.com/nullremove a file

Xóa hoàn toàn, không thể khôi phục file

rm my_useless_file

https://github.com/you-dont-need/You-Dont-Need-GUI#remove-a-folderhttps://draft.blogger.com/nullremove a folder

rm -r my_useless_folder

https://github.com/you-dont-need/You-Dont-Need-GUI#list-folder-contentshttps://draft.blogger.com/nulllist folder contents

Liệt kê nội dung thư mục

ls -la my_folder

https://github.com/you-dont-need/You-Dont-Need-GUI#tree-view-a-folder-and-its-subfoldershttps://draft.blogger.com/nulltree view a folder and its subfolders

hiển thị dưới dạng cây thư mục

tree                                                       # on Linux
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'     # on macOS

https://github.com/you-dont-need/You-Dont-Need-GUI#find-a-stale-filehttps://draft.blogger.com/nullfind a stale file

Tìm tất cả files modified hơn 5 ngày trước.

find my_folder -mtime +5