21

いくつかのフォルダーがあり、それぞれに 15,000 から 40,000 枚の写真が入っています。これらのそれぞれをサブフォルダーに分割し、それぞれに 2,000 個のファイルを入れたいと考えています。

外出先で必要な各フォルダーを作成し、すべてのファイルを移動する簡単な方法は何ですか?

現在、フォルダー内の最初の x 個のアイテムを既存のディレクトリに移動する方法しか見つけることができません。これを 20,000 個のアイテムを含むフォルダーで使用するには、10 個のフォルダーを手動で作成し、コマンドを 10 回実行する必要があります。

ls -1  |  sort -n | head -2000| xargs -i mv "{}" /folder/

forループに入れてみたのですが、mkdirでフォルダがうまく作れず困っています。それを回避した後でも、20 個のファイル (新しいグループの開始) ごとにフォルダーを作成するだけのプログラムが必要です。ファイルごとに新しいフォルダーを作成したい。

では...どうすれば多数のファイルを、それぞれ任意の数のファイルのフォルダーに簡単に移動できますか?

どんな助けも非常に...まあ...役に立ちます!

4

8 に答える 8

5

The code below assumes that the filenames do not contain linefeeds, spaces, tabs, single quotes, double quotes, or backslashes, and that filenames do not start with a dash. It also assumes that IFS has not been changed, because it uses while read instead of while IFS= read, and because variables are not quoted. Add setopt shwordsplit in Zsh.

i=1;while read l;do mkdir $i;mv $l $((i++));done< <(ls|xargs -n2000)

The code below assumes that filenames do not contain linefeeds and that they do not start with a dash. -n2000 takes 2000 arguments at a time and {#} is the sequence number of the job. Replace {#} with '{=$_=sprintf("%04d",$job->seq())=}' to pad numbers to four digits.

ls|parallel -n2000 mkdir {#}\;mv {} {#}

The command below assumes that filenames do not contain linefeeds. It uses the implementation of rename by Aristotle Pagaltzis which is the rename formula in Homebrew, where -p is needed to create directories, where --stdin is needed to get paths from STDIN, and where $N is the number of the file. In other implementations you can use $. or ++$::i instead of $N.

ls|rename --stdin -p 's,^,1+int(($N-1)/2000)."/",e'
于 2015-12-25T14:51:57.543 に答える
4

私は次のようなものに行きます:

#!/bin/bash
# outnum generates the name of the output directory
outnum=1
# n is the number of files we have moved
n=0

# Go through all JPG files in the current directory
for f in *.jpg; do
   # Create new output directory if first of new batch of 2000
   if [ $n -eq 0 ]; then
      outdir=folder$outnum
      mkdir $outdir
      ((outnum++))
   fi
   # Move the file to the new subdirectory
   mv "$f" "$outdir"

   # Count how many we have moved to there
   ((n++))

   # Start a new output directory if we have sent 2000
   [ $n -eq 2000 ] && n=0
done
于 2015-03-18T09:27:55.460 に答える
0

確かに、そのためのスクリプトを作成する必要があります。スクリプトに含めるヒント:

まず、ソース ディレクトリ内のファイルの数を数えます

NBFiles=$(find . -type f -name *.jpg | wc -l)

この数を 2000 で割って 1 を足すと、作成するディレクトリの数が決まります

NBDIR=$(( $NBFILES / 2000 + 1 ))

最後に、ファイルをループして、サブディレクトリ全体に移動します。2 つの複雑なループを使用する必要があります。1 つは目的のディレクトリを選択して作成し、もう 1 つはこのサブディレクトリに 2000 個のファイルを移動し、次のサブディレクトリを作成して次の 2000 個のファイルを新しいディレクトリに移動します。

于 2015-03-18T09:27:00.230 に答える