Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

find - Move files having only 2 lines in AIX

I have a list of files and I want to move to another directory those that contain only two lines.

Is there a way to do it in a single command?

I am on AIX 7.1.

question from:https://stackoverflow.com/questions/65832964/move-files-having-only-2-lines-in-aix

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Depends on what you mean by "a single command", which is an unusual requirement. Generally speaking, you could use find or a for loop over a wildcard:

  • with find:

    find . -type f -exec sh -c 'for f; do [ $(wc -l < "$f") -eq 2 ] && mv -- "$f" /target/dir/; done' findshell {} +
    
  • with for:

    for f in ./*; do [ -f "$f" ] && [ $(wc -l < "$f") -eq 2 ] && mv -- "$f" /target/dir; done
    

If you instead have a list of files, perhaps with ksh93 and an array variable -- files=( * ), for example -- you could loop over those filenames:

for f in "${files[@]}"; do [ -f "$f" ] && [ $(wc -l < "$f") -eq 2 ] && mv -- "$f" /target/dir; done

In the find solution, we limit the results to "file" types; for each of those, we execute a shell with as many arguments as will fit (the {} + part of the syntax); that shell is given 'findshell' as ARG[0], simply as a placeholder. The rest of the arguments are examined in the inner for loop; those that have exactly two lines are moved.

In the for solutions, we loop over the resulting file list and check to ensure we're looking at a regular file [ -f "$f" ] and that the file contains exactly two lines. If both of those conditions match, we move the file.

In all of the mv cases, we explicitly end the options using -- before giving the filename so that filenames that happen to begin with a - dash are not unintentionally mistaken as options. This is also why the manual for loop uses ./* as the wildcard, so that all of the expanded filenames start with ./, preventing those filenames from starting with a dash.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...