べすとえふぉーと

プログラミング等のノート 

bash

urlのテキストをxargsでwgetに渡す

nc以降はオプション cat url.txt | xargs -I % wget % -nc --no-check-certificate --timeout=10

URLを含むテキストが入ったディレクトリに対してwget実行

#!/bin/bash dirs=$(ls -F | grep /) rootdir=$(pwd) for dir in $dirs do cd $rootdir/$dir filenames=$(find . -name "*.txt" | cut -c 3-) for file in $filenames do wget -i $(pwd)/$file -nc --no-check-certificate -U "" rm $(pwd)/$file done done

curlでDropboxにアップロード(v2)

v1が使えなくなったのでv2modeをoverwriteで上書き curl -X POST https://content.dropboxapi.com/2/files/upload \ --header "Authorization: Bearer $KEY" \ --header "Dropbox-API-Arg: {\"path\": \"/file.txt\",\"mode\": \"overwrite\",\"autorename\"…

2つのディレクトリにあるファイルを比較して差分のあるファイルを表示

dir1とdir2がパス diff -qr dir1/ dir2/ | grep "Files" | cut -d ' ' -f2

パイプで渡された文字の前に特定の文字列をつける

16進の値が渡ってきたとして、前に0xをつける echo "A2" | awk '{print "0x" $1}'

ディレクトリ内にあるファイル名だけを抽出

find dir/ -type f | awk -F "/" '{ print $NF }'dirが抽出対象のディレクトリ

sedで文字列置換

普通に置き換え echo hogepiyohoge | sed -e "s/piyo/hoge/g"空白に置き換え echo hogepiyohoge | sed -e "s/piyo//g"

sedで#が入ってる行を削除、空白行も削除する

例はhttpd.conf 動作は未確認 cat httpd.conf | sed '/\#/d' | sed '/^$/d' > out_httpd.conf

lsの結果をxargsでcpに渡す

cd で移動して lsをxargsでcpへ #cd /path/src #ls | xargs -I % cp % /path/to

rm: Argument list too longが出た場合

特にファイル名等に指定がない場合 find . -print | grep "./" | xargs rm

ディレクトリ下のファイル数・容量を調べるコマンド

ファイル数のカウント ls -l $dir | wc -lディレクトリの容量 du -h $dir

curlでDropboxにファイルをアップロード

トークンを取得した状態で実行 curl -X PUT -H "Authorization: Bearer $TOKEN" -T file.png https://content.dropboxapi.com/1/files_put/auto/file.png