LoginSignup
2
1

More than 5 years have passed since last update.

bash > bundle_similar_file_180202_exec > 似たようなファイル名の一覧を省略表示するスクリプト v0.1, v0.2 > スクリプトでなく「ls | uniq -w 3」でいい

Last updated at Posted at 2018-02-02
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
NCAR Command Language Version 6.3.0
for WRF3.7.1, WPS3.7.1
  openmpi-1.8.x86_64 とその-devel
  mpich.x86_64 3.1-5.el6とその-devel
  gcc version 4.4.7 (とgfortran)
for WRF3.9, WPS3.9
  Open MPI v2.1.1
  gcc version 4.9.2 (とgfortran; devtoolset-3使用)
 NetCDF v4.4.1.1, NetCDF (Fortran API) v4.4.4
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
date (GNU coreutils) 8.4 
tmux 1.6-3.el6

背景

$ ls
AAA01  AAA02  AAA03  BBB01  BBB02  BBB03  BBB04  bundle_similar_file_180202_exec  CCC01

AAA*やBBB*やCCC*など共通のprefixを持つファイルが一つのディレクトリに数百個あると、そのファイルが目立ち、他にどういうファイルがあるか見つけにくい。
他のファイルも含めてみるためのスクリプトを検討した。

  • 指定の文字長さでuniq処理する

参考

code v0.1

(追記 2018/02/02)
同じ処理は@tukiyo3 さんのコメントでもっとシンプルに実装されています。

bundle_similar_file_180202_exec
#!/usr/bin/env bash
set -eu

if [ $# -eq 0 ]; then
    echo "[cmd] [number of common characters]"
    exit 0
fi

CHECK_LEN=$1

RESULT=$(
for elem in $(ls)
do
    echo ${elem:0:$CHECK_LEN}
done
)

echo $RESULT | xargs --max-args=1 echo | uniq

実行例

文字長さ3での実行例。

run
$ bash bundle_similar_file_180202_exec 3
AAA
BBB
bun
CCC

文字長さ5での実行例。

run
$ bash bundle_similar_file_180202_exec 5
AAA01
AAA02
AAA03
BBB01
BBB02
BBB03
BBB04
bundl
CCC01

使い勝手が良いかは不明。
bunでなくbundle_similar_file_180202_exec と表示されると良いだろうか。

code v0.2

少し変えてみた。

参考: https://unix.stackexchange.com/questions/201761/search-and-delete-duplicate-files-with-different-names

answered Nov 30 '15 at 1:25
Tom Zych

bundle_similar_file_180202_exec
#!/usr/bin/env bash
set -eu

if [ $# -eq 0 ]; then
    echo "[cmd] [number of common characters]"
    exit 0
fi

ls | uniq -w $1

スクリプト不要

つまり、以下だけで良いということ。

$ ls | uniq -w 3
AAA01
BBB01
bundle_similar_file_180202_exec
CCC01

リストの改行を取り除きたい場合は

$ ls | uniq -w 3 | xargs
AAA01 BBB01 bundle_similar_file_180202_exec CCC01
2
1
12

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
2
1