0

「テスト」フォルダがあります。A、Bなどの名前が異なる他のフォルダが20個あります。(実際には、A、Bではない人の名前です...)シェルスクリプトを作成します。 test / Aのような各フォルダーで、すべての.cファイルの名前をA [1,2 ..]に変更し、それらを「test」フォルダーにコピーします。私はこのように始めましたが、それを完了する方法がわかりません!

#!/bin/sh
for file in `find test/* -name '*.c'`; do mv $file $*; done

手伝ってくれませんか。

4

2 に答える 2

0

このコードはあなたを近づけるはずです。私は自分がしていることを正確に文書化しようとしました。

ファイル名のスペースを処理するために、BASHとGNUバージョンのfindに依存しています。.DOCファイルのディレクトリフィルでテストしたので、拡張子も変更する必要があります。

#!/bin/bash
V=1
SRC="."
DEST="/tmp"

#The last path we saw -- make it garbage, but not blank.  (Or it will break the '[' test command
LPATH="/////" 
#Let us find the files we want
find $SRC -iname "*.doc" -print0 | while read -d $'\0' i
  do
  echo "We found the file name... $i";

  #Now, we rip off the off just the file name.
  FNAME=$(basename "$i" .doc)
  echo "And the basename is $FNAME";
  #Now we get the last chunk of the directory
  ZPATH=$(dirname "$i"  | awk -F'/' '{ print $NF}' )
  echo "And the last chunk of the path is... $ZPATH"

  # If we are down a new path, then reset our counter.
  if [ $LPATH == $ZPATH ]; then
    V=1
  fi;
  LPATH=$ZPATH

  # Eat the error message
  mkdir $DEST/$ZPATH 2> /dev/null 
  echo cp \"$i\" \"$DEST/${ZPATH}/${FNAME}${V}\"
  cp "$i" "$DEST/${ZPATH}/${FNAME}${V}"
done
于 2012-09-17T01:45:49.260 に答える
0
#!/bin/bash

## Find folders under test. This assumes you are already where test exists OR give PATH before "test"
folders="$(find test -maxdepth 1 -type d)"

## Look into each folder in $folders and find folder[0-9]*.c file n move them to test folder, right?
for folder in $folders;
do
   ##Find folder-named-.c files.
   leaf_folder="${folder##*/}"
   folder_named_c_files="$(find $folder -type f -name "*.c" | grep "${leaf_folder}[0-9]")"

   ## Move these folder_named_c_files to test folder. basename will hold just the file name.
   ## Don't know as you didn't mention what name the file to rename to, so tweak mv command acc..
   for file in $folder_named_c_files; do basename=$file; mv $file test/$basename; done
done
于 2014-05-07T16:06:42.167 に答える