Linux 中的 tee 指令

在設定 config 檔案時,第一次可能會用編輯器進行修改,但第二、三次就會用指令的方式進行修改 常常遇到因為權限問題而沒辦法寫入檔案中,沒辦法指令化,範例如下 sudo echo 'foo' > /foo -bash: /foo: Permission denied 在 這篇 剛好有人問,因此紀錄一下 tee tee 指令是從 standard input (stdin) 讀取並輸出 standard output (stdout) 和文件,同時「螢幕輸出」及「輸出檔案」 echo 'foo' | sudo tee /foo 上面的方式會直接覆蓋掉檔案 /foo,如果想要追加寫入檔案的話,可以使用 -a 或 --append 參數 (類似於 >> 的方式) echo 'foo' | sudo tee -a /foo 如果不想要看到 tee 輸出在螢幕的內容,可以將 tee 的 stdout 丟到黑洞中 /dev/null echo 'foo' | sudo tee -a /foo > /dev/null

2020-08-09 · Jett

Linux 中的 man 指令

在 Linux 中常常需要執行指令,在不知道指令或是參數之前都會看手冊 使用 man 指令時,容易因為有相同名稱檔案或指令而導致看錯手冊 舉個例子像是 passwd passwd 這同時有指令 /usr/bin/passwd 的手冊,也是檔案 /etc/passwd 的手冊 這時需要查一下有哪些 manual 查名稱有 passwd 的手冊 man -f passwd passwd (1) - change user password passwd (1ssl) - compute password hashes passwd (5) - the password file -f, --whatis: same as whatis(1). Search for all words in expression in manual page names only. 查名稱包含 passwd 的手冊 man -k passwd chgpasswd (8) - update group passwords in batch mode chpasswd (8) - update passwords in batch mode gpasswd (1) - administer /etc/group and /etc/gshadow grub-mkpasswd-pbkdf2 (1) - generate hashed password for GRUB mkpasswd (1) - Overfeatured front end to crypt(3) openssl-passwd (1ssl) - compute password hashes pam_localuser (8) - require users to be listed in /etc/passwd passwd (1) - change user password passwd (1ssl) - compute password hashes passwd (5) - the password file update-passwd (8) - safely update /etc/passwd, /etc/shadow and /etc/group -k, --apropos: same as apropos(1)....

2020-07-27 · Jett

Linux 中的 cut 指令

在 Linux 中會需要字串處理,學會 cut 指令可以幫助不少 cut 在 /etc/passwd 中儲存了所有 Linux 帳號的登入資訊,每一行就是一筆資料,欄位有 login name, user id, group id … 等等,並且用 : 分隔 如果想要列出所有使用者的話,可以使用 cut 來辦到 cut 處理的字串是以「行」為單位 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin cut -d ':' -f 1 /etc/passwd root bin daemon -d, --delimiter=DELIM use DELIM instead of TAB for field delimiter -f, --fields=LIST select only these fields 如果想要其他欄位的話,可以在 -f 參數後面加上 cut -d ':' -f 1,3-4 /etc/passwd root:0:0 daemon:1:1 bin:2:2 後記 使用 cut 在處理多空格相連的資料時,會比較難判斷是第幾個 fields,應該有其他處理方式

2020-06-12 · Jett

Linux 中的 scp 指令

scp 指令是在網路上主機之間複製文件的指令,使用 ssh 進行身份驗證、安全地數據傳輸檔案 remote -> local scp user@remote_host:remote_file local_file local -> remote scp local_file user@remote_host:remote_file

2019-02-08 · Jett