0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

忘れがちなターミナルコマンドをまとめました

Last updated at Posted at 2023-02-11

今回は、私が忘れがちなターミナルコマンドについてまとめてました。

一応自分のこれからのための備忘録として執筆させていただきます

cpコマンド

1.ワイルドカードでまとめてコピーする

$ cp DeskTop/*.png Pictures/

2.「-r」オプションでディレクトリを丸ごとコピーする

$cp -r samples backup

ディレクトリ,ファイルの作成・削除コマンド

mkdir

####「-p」オプションで深いディレクトリを一気に作成する

例えば

$ mkdir ~/newDir/testdir/sample

を実行してsampleディレクトリを作成する場合、~/newDir/testdir/までは既に作成されている状態でないとエラーが起きてしまう。
そこで、「-p」オプションを指定して実行すれば、途中ディレクトリも一気に作成することができる

$ mkdir -p ~/newDir/testdir/sample

例え「/testdir」が存在しなくてもそれすらも作成してsampleディレクトリを作成する

catコマンドでテキストファイルを作成する

通常catはファイルを表示する役割があるが引数をなしで実行すると入力した文字がそのまま表示される

$ cat
test
test
test3
test3

また、「>」や「<」を利用して、読み込む順序を変更することができる

これらを利用すると,下記のようなことが実現できる

$ cat test.txt
cat: test.txt: No such file or directory
$ cat > test.txt
tanaka
sato
takahashi
//control + Dで終了する
$ cat test.txt
tanaka
sato
takahashi

また、既に作成されているファイルに対してこれをするとファイルが上書きされてしまうので注意する

rm(ファイル削除)

-i」オプションでファイルを確認しながら削除する

$ rm -i *.png
remove test1.png? y //「y」で削除
remove test2.png? n //「y」以外で削除しない

「-r」オプションでディレクトリを丸ごと削除する

$ rm -r tmp

「-fr」オプションで「書き込み」が許可されてなくても削除

$ rm -fr samples/

ファイル参照

先頭からn行目までの部分を表示する

$ head -n 5 test.txt

test.txtを先頭から5行目までを表示させる

末行からn行目までの部分を表示する

$ tail -n 5 test.txt

test.txtを末尾から5行目までを表示させる

ログファイルをリアルタイムで監視する

tail -f test.log

行数、単語数、バイト数を表示させる

$ wc test.txt
   121//行数  883//単語数 11338//バイト数 test.txt

wc -c はバイト数だけ表示
wc -w は単語数だけを表示
wc -l は行数だけを表示

パイプを使って複数のコマンドを組み合わせる

test.txt内の「unix」という文字列が含まれる行数を表示する

grep "unix" test.txt | wc -l

test.txt内の重複行を取り除いて表示

cat test.txt | uniq

ファイルを操作する

sortコマンドで行をソート

スクリーンショット 2021-04-03 13.52.27.png

に対してsortコマンドを実行するとアルファベット順になる

$ sort color1.txt
black
blue
green
red
white
yellow

「>」を利用して別ファイルにその結果を保存することもできる

$ sort color1.txt > sorted.txt

フィールド番号を指定して並べ替える

スクリーンショット 2021-04-03 13.58.00.png

「-k 最初のフィールド番号,最後のフィールド番号」オプションを指定
フィールドの区切りはデフォルトで空白orタブとして認識されるが
「-t 区切り文字」オプションで指定することもできる

$ sort -k 3,3 -t "," custormer.txt

を実行すると3フィールド目(性別)を元に並び替えられて表示される

-nオプションで数値順に並び替えることもできる

$ sort -n -k 4,4 -t "," custormer.txt

を実行すると4フィールド目(年齢)を元に並び替えられて表示される(数値が小さい順)

重複行非表示にして並び替える

$ sort -u test.txt

重複回数を表示する

test.txt
テスト
hoge
honge
honhonge
テスト
$ sort test.txt | uniq -c
1 hoge
1 honge
1 honhonge
2 テスト

grepコマンドの主なオプション

マッチした行数を表示する

grep -c キーワード ファイルパス

英大文字と小文字を区別をしない

grep -i キーワード ファイルパス

行番号を表示する

grep -n キーワード ファイルパス

マッチしなかった行を表示する

grep -v キーワード ファイルパス

行全体とマッチする行のみを表示する

grep -x キーワード ファイルパス

findコマンドでファイルを検索する

find ディレクトリパス -name ファイル名

コマンドに別名をつけるエイリアス

コマンドにオプションや引数を含めて別名をつけることを「エイリアス」という。

$ alias rm='rm -i'

とするとrmコマンド実行時にrm -i (削除確認)が実行される

エイリアスの確認

$ alias

エイリアスの削除

$ unalias rm

アクセス権限について

パーミッション

image.png

chmod

スクリーンショット 2021-04-04 17.30.01.png

シェルスクリプト

コマンド名だけでそのシェルスクリプトを実行する

showTime.sh
#! /bin/bash
echo "こんにちは$(whoami)さん"
date "+ただいまの時刻は%H時%M分です"

全てのユーザーに実行を許可する

$ chmod a+x showTime.sh

あとはパスで実行

$ ./showTime.sh

コマンド名だけで実行する

コマンド検索パスに登録されているディクトりに移動する。

$ mv showTime.sh ~/bin/

コマンド名だけで実行できる

$ showTime.sh

コマンドの引数を扱う

paramTest.sh
#! /bin/bash

echo '$0:' $0
echo '$1:' $1
echo '$2:' $2
echo '$3:' $3
echo '$@:' $@
echo '$#:' $#

コマンド実行

$ ./paramTest.sh apple orange green
$0: ./paramTest.sh
$1: apple
$2: orange
$3: green
$@: apple orange green //リスト
$#: 3 //数

ループ

paramTest.sh
for p in "$@"
do
    echo $p
done

コマンド実行

$ ./paramTest.sh apple orange green
apple
orange
green

なんで「$@」をダブル9ウォーテーションで囲むのか

スペースを含む引数が個別の引数として扱われてしまうため

$ ./paramTest.sh "hello world" good
hello //helloとworldが別の引数として扱われてる
world
good
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?