[英]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
dir=1
and counter=1
are setting the variables. dir=1
且counter=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.