简体   繁体   English

在Linux中循环浏览文件夹?

[英]Loop through folders in linux?

I want to move files from one directory to another 我想将文件从一个目录移动到另一个目录

find /data/1990 -name "*.dat" -exec mv {} /data1/1990/ \;

that was easy. 那很简单。 noW I have in /data/ many years 1990,1991,1992,1993,1994,1995 现在我在/ data /中有很多年1990,1991,1992,1993,1994,1995

How to loop over these years? 这些年来如何循环?

You can do it like this: 您可以这样做:

for i in `ls /data/`; 
do 
    echo "element=$i";  #put your code for each element
done

I'd make my life easier by changing my current working directory: 通过更改当前的工作目录,我可以使生活更轻松:

cd /data
find 199[012345] -name "*.dat" -exec echo mv {} /data1/{} \;

You didn't tell us what shell are you using (and they differ just like programming languages do). 您没有告诉我们您使用的是什么shell(它们就像编程语言一样有所不同)。 But in bash or zsh, to provide year range you can use curly brackets, ie: 但是在bash或zsh中,要提供年份范围,可以使用大括号,即:

for year in {1990..1995}; do
    find /data/$year -name "*.dat" -exec mv {} /data1/$year \;
done

You can also use arithmetic evaluation and more modern(?) block syntax: 您还可以使用算术评估和更现代的(?)块语法:

for ((year=1990; year<1995; year++)) {
    find /data/$year -name "*.dat" -exec mv {} /data1/$year \;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM