0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[bash] ディレクトリ文字列のリネーム処理

Posted at

shのrename処理を行うコードをcopilotに聞いてみた結果

実現したいこと

a/b/c/name.csv c/name.csv name.csv

name_a_b_c.csv name_c.csv name.csv

rename.sh
#!/bin/bash
# ファイルパスのリスト
 file_paths=("a/b/c/name.csv" "c/name.csv" "name.csv") 
# 各ファイルパスを処理
 for path in "${file_paths[@]}"; do 
# ファイル名を抽出 
 filename=$(basename "$path") 
# ディレクトリパスを抽出し、'_'で結合 
 dir_path=$(dirname "$path" | tr '/' '_') 
# ディレクトリパスが空でない場合、ファイル名に追加 
if [ "$dir_path" != "." ]; then 
 new_filename="${filename%.csv}_${dir_path}.csv" 
else 
 new_filename="$filename" 
fi
 echo "$new_filename" 
done

以上。

edgeのcopilot優秀で驚きます。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?