ホーム » Tips集 » Shell » emrm.sh

Tips集

emrm.sh

  • ファイルサイズがゼロのファイルを選択的、もしくはいっぺんに消すためのスクリプト (empty file removerの略)。
  • [オプション]
  • 引数なし ファイルサイズがゼロのファイルすべてに対して削除するかをy or n (yes or no)で選択させる。
  • 引数=all ファイルサイズがゼロのファイルを全て一括して消去する
  • 引数= ファイル名(複数可) 選択したファイルのうち、ファイルサイズがゼロのものを消去する。
  • [注意事項] ファイルを消去するコマンドなので、よく注意して使用して下さい。
  • [作成意図] プログラム改変中に作ってしまう出力値を含まない空ファイルを消去し、ディレクトリ内のファイルを減らすために用いる。
#!/bin/bash
# $Id: emrm.sh,v 1.4 2012-10-13 09:13:03+09 shirai Exp $
# EMPTY REMOVE

list=(`ls`)

# MAIN

main(){
if [ $# = 0 ];then
	select_emrm
elif [ "$1" = "all" ];then
	all_emrm
else
	ask_emrm
fi
}

# MODE SELECT

select_emrm(){
# REMOVE SELECTED EMPTY FILES
echo "---> select files to remove"
for file in ${list[@]}
do
	if [ ! -s $file ];then
		check=0
		while ((check==0))
		do
			echo -n "remove $file (y/n)? "
			read yORn
			if [ "$yORn" = "y" ];then
				diae rm $file
				check=1
			elif [ "$yORn" = "n" ];then check=1
			else
				echo " PLEASE INPUT y(yes) or n(no)"
			fi
		done
		check=0
	fi
done
}

all_emrm(){
# REMOVE ALL EMPTY FILES
echo "---> remove all empty files"
for file in ${list[@]}
do
	if [ ! -s $file ];then
		diae rm $file
	fi
done
}

ask_emrm(){
# REMOVE BY HANDS
echo "---> remove by hand"
for file in $@
do
	if [ ! -s $file ];then
		diae rm $file
	else
		echo " $file is not empty."
	fi
done
}

# FUNCTION

diae(){
echo $@
$@
}

# SCRIPT START !
main $@

おすすめソフト