膨大な時間を節約する多数のLinuxトリック









この記事も英語です。



ある晩、 Jeffrey Friedleを読み直したとき、利用可能なすべてのドキュメントがあるにもかかわらず、自分に合った多くのトリックがあることに気付きました。 すべての人々はあまりにも異なっています。 そして、一部の人にとって明らかなトリックは、他の人にとっては明らかではなく、他の人にとってはある種の魔法のように見えるかもしれません。 ところで、私はすでにいくつかの同様のポイントをここで説明しました



管理者またはユーザーのコマンドラインは、すべてを実行できるツールであるだけでなく、愛する人のために永遠にカスタマイズできるツールでもあります。 最近、CLIの便利なトリックのトピックに関する翻訳を実行しました。 しかし、私は翻訳者自身がアドバイスをほとんど使用していないという印象を受けました。そのため、重要なニュアンスを逃す可能性があります。



個人的な経験から-カットの下で-コマンドラインでのダーストリック。



小さな余談-実際には、実際のサーバーまたはユーザーの名前が偶然に発生する可能性のある多くのトリックを使用していますが、これはNDAに該当する可能性があるため、記事のすべての例をコピーして貼り付けたり、特別に書き直したり、できるだけ簡略化することはできませんでしたが、このコンテキストでのいくつかの手法は完全に役に立たない-おそらくこれはまさにこの理由のためです。 しかし、いずれにせよ、コメントであなたのアイデアを共有してください!



1.変数展開を使用した改行



多くの場合、単純に単一の列の値を取得するためにcutまたはawkを使用します。

文字列の最初の文字を取得する必要がある場合は、$ {VARIABLE:0:5}を使用します。



ただし、#、##、%、および%%(bash変数展開)を使用して文字列を操作できる非常に強力なツールがあります。それらの助けを借りて、あらゆる側面から不要なパターンをカットできます。

以下の例は、文字列「username:homedir:shell」から3番目の列(シェル)のみを取得する方法を示しています。カットまたは変数展開を使用します(マスク*:および##コマンドを使用します。コロン):



$ STRING="username:homedir:shell"

$ echo "$STRING"|cut -d ":" -f 3
shell
$ echo "${STRING##*:}"
shell
      
      





(cut), , . bash- windows, , .



Ubuntu — 1000



$ cat test.sh
#!/usr/bin/env bash

STRING="Name:Date:Shell"

echo "using cut"
time for A in {1..1000}
do
  cut -d ":" -f 3 > /dev/null <<<"$STRING"
done

echo "using ##"
time for A in {1..1000}
do
 echo "${STRING##*:}" > /dev/null
done

      
      





$ ./test.sh
using cut

real    0m0.950s
user    0m0.012s
sys     0m0.232s
using ##

real    0m0.011s
user    0m0.008s
sys     0m0.004s

      
      



— !



, — . . . cut /etc/passwd. ##, read. , ?



$ cat test.sh
#!/usr/bin/env bash

echo "using cut"
time for count in {1..1000}
do
  cut -d ":" -f 7 </etc/passwd > /dev/null
done

echo "using ##"
time for count in {1..1000}
do
  while read
  do
    echo "${REPLY##*:}" > /dev/null
  done </etc/passwd
done


      
      



$ ./test.sh
$ ./test.sh
using cut

real    0m0.827s
user    0m0.004s
sys     0m0.208s
using ##

real    0m0.613s
user    0m0.436s
sys     0m0.172s

      
      



=)




:

$ VAR="myClassName = helloClass"
$ echo ${VAR##*= }
helloClass

      
      







$ VAR="Hello my friend (enemy)"
$ TEMP="${VAR##*\(}"
$ echo "${TEMP%\)}"
enemy

      
      







2. bash TAB



bash-completion , /etc/bash.bashrc /etc/profile.d/bash_completion.sh, . TAB — , .

, , , , , , , , . .

PATH, — .profile .bashrc.

*nix lowercase , uppercase — :



$ alias TAsteriskLog="tail -f /var/log/asteriks.log"
$ alias TMailLog="tail -f /var/log/mail.log"
$ TA[tab]steriksLog
$ TM[tab]ailLog
      
      







3. bash TAB — 2



, $HOME/bin.

.

PATH, .

LastLogin .profile ( .profile):



function LastLogin {
  STRING=$(last | head -n 1 | tr -s " " " ")
  USER=$(echo "$STRING"|cut -d " " -f 1)
  IP=$(echo "$STRING"|cut -d " " -f 3)
  SHELL=$( grep "$USER" /etc/passwd | cut -d ":" -f 7)
  echo "User: $USER, IP: $IP, SHELL=$SHELL"
}
      
      





( , , , alias)

:



$ L[tab]astLogin
User: saboteur, IP: 10.0.2.2, SHELL=/bin/bash
      
      





4. Sensitive data



, bash history, , — , echo «hello 2» :



$ echo "hello"
hello
$ history 2
 2011  echo "hello"
 2012  history 2
$  echo "my password secretmegakey"
my password secretmegakey
$ history 2
 2011  echo "hello"
 2012  history 2
      
      





:

export HISTCONTROL=ignoreboth




, git ansible, external bash file , 600, .gitignore , source:



secret.sh
PASSWORD=LOVESEXGOD

myapp.sh
. ~/secret.sh
sqlplus -l user/"$PASSWORD"@database:port/sid @mysqfile.sql
      
      







Tanriol, , , , .

, ssh , . wget — --password, , wget, .



— ( , , SQL) openssl .

openssl , . :



secret.key :



$ echo "secretpassword" > secret.key; chmod 600 secret.key
      
      





aes-256-cbc, c :



$ echo "string_to_encrypt" | openssl enc -pass file:secret.key -e -aes-256-cbc -a
U2FsdGVkX194R0GmFKCL/krYCugS655yLhf8aQyKNcUnBs30AE5lHN5MXPjjSFML
      
      





-, .git — secret.key .

, -e -d:



$ echo 'U2FsdGVkX194R0GmFKCL/krYCugS655yLhf8aQyKNcUnBs30AE5lHN5MXPjjSFML' | openssl enc -pass file:secret.key -d -aes-256-cbc -a
string_to_encrypt
      
      





, , , secret.key .



5. grep



-



tail -f application.log | grep -i error
      
      









tail -f application.log | grep -i -P "(error|warning|failure)"
      
      





-v, — , — , exception , ( , info, ):



tail -f application.log | grep -v -i "info"
      
      





:



-P, grep basic regular expression, PCRE, , .



"--line-buffered", "-i".



, --only-matching, . . grep, // .



6.



, , , *nix, , iNode.

, , , ( ). , , .

( , , )



( , ):



> application.log
      
      





( ):



truncate --size=1M application.log
      
      





, , , .



, 1000 :



echo "$(tail -n 1000 application.log)" > application.log
      
      





Himura .

P.S. — - , , log4j, rotatelogs.



7. watch !



, - . , ( who ), - ftp ( ls ).







watch <>
      
      





, 2 , Ctrl+C. .

,



8. bash



, :



for srv in 1 2 3; do echo "server${srv}";done
server1
server2
server3
      
      





:



for srv in server{1..3}; do echo "$srv";done
server1
server2
server3
      
      





seq, . :



for srv in $(seq -w 1 10); do echo "server${srv}";done
server01
server02
server03
server04
server05
server06
server07
server08
server09
server10
      
      





, . basename:



for file in *.txt; do name=$(basename "$file" .txt);mv $name{.txt,.lst}; done
      
      





, %:



for file in *.txt; do mv ${file%.txt}{.txt,.lst}; done
      
      





«rename»



java :

mkdir -p project/src/{main,test}/{java,resources}
      
      





project/
   !--- src/
        |--- main/
        |    |-- java/
        |    !-- resources/
        !--- test/
             |-- java/
             !-- resources/
      
      







9. tail,



multitail, . - , .



tail:



tail -f /var/logs/*.log
      
      





, «tail -f».

, , , , tail -f. - .

, «tail -f», . , , .



, PID PID , tail :



alias TFapplog='tail -f --pid=$(cat /opt/app/tmp/app.pid) /opt/app/logs/app.log'
      
      





. , tail, .



10.



dd



dd if=/dev/zero of=out.txt bs=1M count=10
      
      





fallocate:



fallocate -l 10M file.txt
      
      





, (xfs, ext4, Btrfs...), , dd.



11. xargs



, , .

— .



-n:



$ #    5 
for string in string{1..5}; do echo $string >> file.lst; done
$ cat file.lst
string1
string2
string3
string4
string5
saboteur@ubuntu:~$ cat file.lst | xargs -n 2
string1 string2
string3 string4
string5
      
      





— , . , xargs , 2 :



cat file | xargs -n 2 -P 3
      
      





, nproc, :



cat file | xargs -n 2 -P $(nproc)
      
      





12. sleep? while? read!



sleep while, read, , :



read -p "Press any key to continue " -n 1
      
      





, :



read -p "Press any key to continue (autocontinue in 30 seconds) " -t 30 -n 1
      
      





:



REPLY=""
until [ "$REPLY" = "y" ]; do
  # executing some command
  read "Press 'y' to continue or 'n' to break, any other key to repeat this step" -n 1
  if [ "$REPLY" = 'n' ]; then exit 1; fi
done

      
      





, .

— !



P.S. Update:

1. — !

2. `` $() — !

3. , .



, — , =), + : khim, Tanriol, Himura, bolk, firegurafiku, dangerous3, RPG, ALexhha, IRyston, McAaron



All Articles