我发现这个 bash 脚本很好用
for file in $(ls -p | grep -v / | tail -241)
do
mv $file ../01
done
我的 bash foo 很弱,基本上如果我在目录中运行会将最后 241 个文件移动到 /01 的文件夹中
问题是脚本本身必须在包含所有文件的目录中,而我当前的挂载没有执行权限。我想要做的是从文件夹外的某个地方执行这个脚本,例如我的工作目录 /home/user/
我当前的根目录与我想要编写脚本的所有文件位于不同的驱动器/分区上。
for file in **/media/storage1/allfiles** $(ls -p | grep -v / | tail -241)
do
mv $file ../01 <this should remain local to /media/storage/
done
** 编辑 ** 我通过在循环之前添加这个来让它工作cd the/directory/I/want/to/be/in
:
例如
cd /media/storage1/allfiles
for file in $(ls -p | grep -v / | tail -241)
do
mv $file ../01
done
更好的问题是:这是正确的方法,还是有正确的方法?
在 中bash
,我会执行以下操作以避免使用ls
. 它利用了这样一个事实,即您没有尝试以任何特定方式对文件进行排序,因此路径名扩展提供的任何顺序就足够了。
cd /media/storage1/allfiles
# Get *all* regular files in the target directory
for f in *; do
[[ -f $f ]] && files+=("$f")
done
# Get the file count
n=${#files[@]}
# How many files are we *not* moving
# Assume n > 241
keep=$((n - 241 ))
# Move the last 241 files
mv -- "${files[@]:keep}" ../01
据推测,241 个文件不会长到溢出命令行。
不请自来的zsh
插头...
zsh
使这更容易一些,因为您可以让 shell 使用 glob 本身进行很多选择工作。
mv /media/storage/allfiles/*(.On[1,241]) ../01
在(...)
下面的水珠是一组限制文件的列表产生水珠预选赛。.
表示只应匹配常规文件。On
按名称以相反的顺序对列表进行排序(可以通过替换n
为适当的字符来使用其他排序顺序)。[1,241]
将列表限制为前 241 个文件;由于我们以相反的顺序排序,这为您提供了最后 241 个文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句