简体   繁体   English

使用 Linux 将特定数量的文件从一个目录复制到另一个

[英]Copy specific number of files from on directory to another using Linux

I have a directory containing 8000+ fits files and I am wondering if there is a way to copy increments of them unto other directories such that I could have either 8 directories with about 1000 fits files in them each or 4 directories with 2000 fits files?我有一个包含 8000 多个 fit 文件的目录,我想知道是否有办法将它们的增量复制到其他目录,这样我可以有 8 个目录,每个目录有大约 1000 个 fit 文件,或者 4 个目录有 2000 个 fit 文件?

Something like this could make it: 这样的事情可以做到:

dir=1
counter=1

for file in spec*
do
   echo "cp $file dir_$dir"
   ((counter++))
   (( $counter%1000 == 1 )) && ((dir++))
done

Explanation 说明

  • dir=1 and counter=1 are setting the variables. dir=1counter=1正在设置变量。
  • for file in spec* loops through spec* pattern name files. for file in spec*循环通过spec*模式名称文件。
  • echo "cp $file dir_$dir" will output like cp spec123 dir_1 / dir_2, ... I used echo so that you can check the behaviour before going ahead and doing the proper cp . echo "cp $file dir_$dir"将输出类似于cp spec123 dir_1 / dir_2,...我使用了echo这样你就可以在继续之前检查行为并做正确的cp
  • ((counter++)) increments the counter variable counter. ((counter++))递增counter变量计数器。
  • (( $counter%1000 == 1 )) && ((dir++)) if $counter is on a form 1000K+1, increment the value of $dir . (( $counter%1000 == 1 )) && ((dir++))如果$counter在表单1000K + 1上,则增加$dir的值。

To extend the script fedorqui wrote to your need, you can do the following: 要扩展fedorqui编写的脚本以满足您的需求,您可以执行以下操作:

create a file, 创建一个文件,

$ nano mycopy.sh (you can choose your own name) $ nano mycopy.sh (你可以选择自己的名字)

paste the following and save the file: 粘贴以下内容并保存文件:

source=$1
dir=1
counter=1

mkdir dir_$dir
for file in `ls $source`
do
   cp -r $source$file dir_$dir/
   ((counter++))
   (( $counter%1000 == 1 )) && ((dir++)) && ((`mkdir dir_$dir`))
done

make your file executable by 让你的文件可执行

$ chmod u+x mycopy.sh

now execute the script by running 现在通过运行来执行脚本

$ ./mycopy.sh MyDirectoryWithManyFiles/

where MyDirectoryWithManyFiles/ is the directory containing your files 其中MyDirectoryWithManyFiles /是包含文件的目录

The script will create sub directories and copy 1000 files max inside them 该脚本将创建子目录并在其中最多复制1000个文件

Here's a way of doing it in Python: 这是在Python中实现它的一种方式:

Create a new file called " manage_files.py " or something, put this code into it: 创建一个名为“ manage_files.py ”的新文件,将此代码放入其中:

# Hat-tip to http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-with-python
import os
import glob
from subprocess import call

# Initialise
my_dir = 'files_are_here'
num_files = 10

# Get a list of all files in my_dir
os.chdir(my_dir)
all_files = glob.glob('*')

for i in range((len(all_files) / num_files) + 1):
    files = all_files[i * num_files:(i + 1) * num_files]
    dirname = "dir_%s" % i
    call(["mkdir", dirname])
    file_names = ' '.join(files)
    arg = ['mv', '-t', '%s/' % dirname]
    arg.extend(files)
    call(arg)

Then, from the Linux shell type python manage_files.py and the magic will do its stuff. 然后,从Linux shell类型python manage_files.py和魔术将做它的东西。

I hope it helps you!我希望它对你有帮助!

Here, rootfile folder contains several files, those names are included like ..om500.. or ..om501.. or ..om502..在这里, rootfile文件夹包含多个文件,这些文件的名称包括..om500....om501....om502..

So, I wanted to copy a specific number of each files name to a new folder inside rootfile folder, rootrate .因此,我想将每个文件名的特定数量复制到rootfile文件夹中的新文件夹rootrate

#!/bin/bash
mkdir rootfile/rootrate
for ((j=500;j<503;j++)); do
echo $j
file=(`ls rootfile/*om${j}_*.root`)
if [ "$j"  -eq "500" ];
then
  max=60
fi
if [ "$j"  -eq "501" ];
then
  max=8  
fi
if [ "$j"  -eq "502" ];
then
  max=120
fi

for ((i = 0; i<max ; i++)); do
  echo "${file[i]}"
  echo $i
  cp "${file[i]}" rootfile/rootrate/ 
done
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Linux中将具有特定扩展名的文件从一个目录复制到另一个目录 - How to copy files with a particular extension from one directory to another in linux 如何将文件从 linux 目录复制到另一个给定长度参数 - How to copy files from a linux directory to another given a length parameter 在 Unix/Linux 中将特定文件从一个文件夹复制到另一个文件夹 - Copy specific files from one folder to another in Unix/Linux 在 Linux 中将文件从一个用户主目录复制到另一个用户主目录 - Copy files from one user home directory to another user home directory in Linux 从Linux上的另一台计算机复制目录 - copy directory from another computer on Linux 如何在具有特定编号的目录中查找具有特定模式的文件? 的Linux - How to find files with specific pattern in directory with specific number? Linux 是否可以将文件从本地 windows 目录复制到远程 linux 目录? - Is it possible to copy files from local windows directory to remote linux directory? 如何在父目录linux的子目录中查找和复制文件 - how to find and copy files in a sub directory from parent directory linux Linux 在 bash/python 脚本中将下一个“n”个文件从一个文件夹复制到另一个文件夹 - Linux copy next 'n' number of files from one folder to another in bash/python script 如果文件不在另一个目录中,则将文件复制到目录 - Copy files to directory if they are not in another directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM