it-swarm.cn

如何从包含数千个文件夹的文件夹中移动100个文件?

我有一个包含数千个文件的目录。如何将100个文件(任何文件都可以)移动到另一个位置。

43
gaijin
for file in $(ls -p | grep -v / | tail -100)
do
mv $file /other/location
done

假定文件名不包含空格,换行符(假设默认值$IFS),通配符(?,_*[)或以-

36
ennuikiller

在zsh中最简单:

mv -- *([1,100]) /other/location/

这将移动前100个非隐藏文件(任何类型,更改([1,100])(.[1,100])仅用于常规文件,或(^/[1,100])表示按目录顺序排列的目录)以外的任何类型。您可以使用oglob qualifier 选择不同的排序顺序,例如移动最旧的100个文件:

mv -- *(Om[1,100]) /other/location/

使用其他shell,您可以循环退出并提早退出。

i=0
for x in *; do
  if [ "$i" = 100 ]; then break; fi
  mv -- "$x" /other/location/
  i=$((i+1))
done

另一种可移植的方式是 构建文件列表并删除除最后100个文件之外的所有文件

37

如果您不使用zsh:

set -- *
[ "$#" -le 100 ] || shift "$(($# - 100))"
mv -- "[email protected]" /target/dir

将最后一个(按字母顺序)移动100个。

8
Stéphane Chazelas

以下对我有用。抱歉,如果它以前发布过,但是我没有在快速扫描中看到它。

ls path/to/dir/containing/files/* | head -100 | xargs -I{} cp {} /Path/to/new/dir
5
Joe

Shell中的以下oneliner会有所帮助。

 foreach i(`find Source_Directory -type f --max-depth 1 | tail -100`);做; {mv $ i Target_Directory};完成
3
diham
3
den2042

mmv 是出色的实用程序,它也使您可以批量重命名文件。 (我不得不 Sudo apt-get install mmv安装在我的计算机上。)简单的用法示例:假设您有一个目录,其扩展名为.JPG,您想将其更改为小写的.jpg。以下命令可以解决问题:

mmv \*.JPG \#1.jpg

反斜杠用于表示通配符即将出现。 */JPG匹配任何带有JPG扩展名的内容。在命令的“ to”部分中,#1使用第一个通配符中的匹配文本来重命名文件。当然,您可以在#1之前放置其他路径来移动文件。

1
Pete
#!/bin/bash
c=1; d=1; mkdir -p NEWDIR_${d}
for jpg_file in *.jpg
do
if [ $c -eq 100 ]
then
d=$(( d + 1 )); c=0; mkdir -p NEWDIR_${d}
fi
mv "$jpg_file" NEWDIR_${d}/
c=$(( c + 1 ))
done

试试这个代码

1
juicebyah

如果您对使用ls有兴趣,可以使用以下命令

$ ls -rt source/* | head -n100 | xargs cp -t destination

这是如何运作的 ??

  • ls -rt source/*-命令列出具有相对路径的所有文件
  • head -n100-前100个文件
  • xargs cp -t destination-将这些文件移到目标文件夹
1
Rohith Yeravothula

尝试这个:

find /source/directory -type f -maxdepth 1 -print | tail -100 | xargs -J % mv % /other/location/
0
Saumil

如果要安全/处理包含空格,换行符,引号,反斜杠等的文件名,则必须使用以空字符结尾的分隔符:

find "$srcdir" -maxdepth 1 -type f -print0 | head -z -n 100 | xargs -0 -r -- mv -t "$destdir" --

EDIT2:注意:如果您没有head -z(- 出于任何原因 ),您可以将以上head -z -n 1000替换为tr '\0\n' '\n\0' | head -n 1000 | tr '\0\n' '\n\0'(或 请参阅其他方式

-maxdepth 1将避免在$srcdir的子目录中查找文件,因此列出的唯一文件是$srcdir中的文件。
-print0将在每个列出的文件之间使用\0而不是换行符(\n)-这有助于使用xargs处理包含换行符和空格的文件。
head -z将以\0终止(而不是以newline(\n)终止)行作为行。 -n 100将仅列出find找到的前100个文件。
如果要查看将执行xargs的命令,请添加-t(或--verbose)。
xargs -0“输入项目以空(\0)字符而不是空格终止,并且引号和反斜杠不是特殊的(每个字符均按字面意义使用)”
如果没有要移动的文件(即mv没有找到任何文件),则 xargs -r将不会运行find
--终止参数的处理,作为程序的选项,更多详细信息 这里

示例输出(运行一个mv命令,也可以处理名称中带有换行符的文件):

$ find /tmp/t -maxdepth 1 -type f -print0 | head -z -n 100 | xargs -t -0 -r -- mv -t /tmp -- ; echo "exit codes: ${PIPESTATUS[@]}"
mv -t /tmp -- /tmp/t/file containing quotes"' then spaces /tmp/t/file containing quotes"' /tmp/t/file containing a slash n here\n /tmp/t/file containing a new line here
and continues /tmp/t/s /tmp/t/-x and -L 1. /tmp/t/of replace-str in the initi /tmp/t/-thisfile_starts_with_a_hyphen and has spaces and a -hyphen here /tmp/t/-thisfile_starts_with_a_hyphen and has spaces /tmp/t/-thisfile_starts_with_a_hyphen /tmp/t/another      with       spaces /tmp/t/one with spaces /tmp/t/c /tmp/t/a 
exit codes: 0 0 0

$ ls -1R /tmp/t
/tmp/t:
a
'another      with       spaces'
b
c
'file containing a new line here'$'\n''and continues'
'file containing a slash n here\n'
'file containing quotes"'\'''
'file containing quotes"'\'' then spaces'
'of replace-str in the initi'
'one with spaces'
s
'some dir'
-thisfile_starts_with_a_hyphen
'-thisfile_starts_with_a_hyphen and has spaces'
'-thisfile_starts_with_a_hyphen and has spaces and a -hyphen here'
'-x and -L 1.'

/tmp/t/b:
'file with spaces'

'/tmp/t/some dir':
'some file'

对于find

-maxdepth levels
       Descend at most levels (a non-negative integer) levels of direc‐
       tories below the starting-points.  -maxdepth 0
        means only apply the tests and actions to  the  starting-points
       themselves.
-type c
       File is of type c:

       b      block (buffered) special

       c      character (unbuffered) special

       d      directory

       p      named pipe (FIFO)

       f      regular file

       l      symbolic link; this is never true if the -L option or the
              -follow  option is in effect, unless the symbolic link is
              broken.  If you want to search for symbolic links when -L
              is in effect, use -xtype.

       s      socket

       D      door (Solaris)
-P     Never follow symbolic links.  This  is  the  default  behaviour.
       When find examines or prints information a file, and the file is
       a symbolic link, the information used shall be  taken  from  the
       properties of the symbolic link itself.
-L     Follow symbolic links.  When find examines or prints information
       about files, the information used shall be taken from the  prop‐
       erties  of  the file to which the link points, not from the link
       itself (unless it is a broken symbolic link or find is unable to
       examine  the file to which the link points).  Use of this option
       implies -noleaf.  If you later use the -P option,  -noleaf  will
       still  be  in  effect.   If -L is in effect and find discovers a
       symbolic link to a subdirectory during its search, the subdirec‐
       tory pointed to by the symbolic link will be searched.

       When the -L option is in effect, the -type predicate will always
       match against the type of the file that a symbolic  link  points
       to rather than the link itself (unless the symbolic link is bro‐
       ken).  Actions that can cause symbolic links  to  become  broken
       while  find  is executing (for example -delete) can give rise to
       confusing behaviour.  Using -L causes  the  -lname  and  -ilname
       predicates always to return false.

对于head

-n, --lines=[-]NUM
       print the first NUM lines instead of  the  first  10;  with  the
       leading '-', print all but the last NUM lines of each file
-z, --zero-terminated
       line delimiter is NUL, not newline

编辑:某人 提到 他们没有head -z,这是我使用的版本(在Fedora 25中):

$ head --version
head (GNU coreutils) 8.25
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie and Jim Meyering.

$ rpm -qf /usr/bin/head
coreutils-8.25-17.fc25.x86_64

对于xargs

-0, --null
       Input  items  are  terminated  by a null character instead of by
       whitespace, and the quotes and backslash are not special  (every
       character is taken literally).  Disables the end of file string,
       which is treated like any other  argument.   Useful  when  input
       items  might  contain  white space, quote marks, or backslashes.
       The GNU find -print0 option produces  input  suitable  for  this
       mode.
-r, --no-run-if-empty
       If the standard input does not contain any nonblanks, do not run
       the command.  Normally, the command is run once even if there is
       no input.  This option is a GNU extension.
-P max-procs, --max-procs=max-procs
       Run  up  to max-procs processes at a time; the default is 1.  If
       max-procs is 0, xargs will run as many processes as possible  at
       a  time.   Use the -n option or the -L option with -P; otherwise
       chances are that only one exec will be  done.   While  xargs  is
       running,  you  can send its process a SIGUSR1 signal to increase
       the number of commands to run simultaneously, or  a  SIGUSR2  to
       decrease  the number.  You cannot increase it above an implemen‐
       tation-defined limit (which is shown with  --show-limits).   You
       cannot  decrease  it  below  1.  xargs never terminates its com‐
       mands; when asked to decrease, it merely waits for more than one
       existing command to terminate before starting another.

       Please  note  that  it is up to the called processes to properly
       manage parallel access to shared  resources.   For  example,  if
       more  than one of them tries to print to stdout, the ouptut will
       be produced in an indeterminate order (and very likely mixed up)
       unless  the  processes  collaborate in some way to prevent this.
       Using some kind of locking scheme is one  way  to  prevent  such
       problems.   In  general, using a locking scheme will help ensure
       correct output but reduce performance.  If  you  don't  want  to
       tolerate  the  performance  difference,  simply arrange for each
       process to produce a separate output file (or otherwise use sep‐
       arate resources).
-t, --verbose
       Print  the command line on the standard error output before exe‐
       cuting it.

对于cp

-t, --target-directory=DIRECTORY
       copy all SOURCE arguments into DIRECTORY
-v, --verbose
       explain what is being done
0
user306023

我来了这里,但是我需要将文件的一部分(每个99个)从/DIR1复制到/DIR2。我将在此处粘贴脚本以帮助otherz:

#!/bin/bash
# Thanks to <Jordan_U> @ #ubuntu
# 06 Dec 2014

i=0
copy_unit=98

for file in /DIR1/*; do
  cp "$file" /DIR2
  if [[ "$i" -ge "$copy_unit" ]]; then
    echo "Pausing, press enter to continue"
    read
    i=0
  fi
  ((i++))
done
0
Enissay

https://unix.stackexchange.com/a/105042/66736 启发的另一种变化:

cp `ls -d ./* | head -n 100` tmpi

它可能不是最快或最优雅的方法,但您可以将其保留在内存中。

0
lucidbrot