Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I limit the results of the command find in bash?

Tags:

find

bash

The following command:

find . -name "file2015-0*" -exec mv {} .. \;

Affects about 1500 results. One by one they move a previous level.

If I would that the results not exceeds for example in 400? How could I?

like image 207
Mariano Vedovato Avatar asked Feb 09 '15 15:02

Mariano Vedovato


People also ask

What does find command do bash?

The Bash find Command 101 The find command allows you to define those criteria to narrow down the exact file(s) you'd like to find. The find command finds or searches also for symbolic links (symlink). A symbolic link is a Linux shortcut file that points to another file or a folder on your computer.

How do I stop a running command in bash?

There are many methods to quit the bash script, i.e., quit while writing a bash script, while execution, or at run time. One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.

What does find do in Shell?

The find command is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments. find command can be used in a variety of conditions like you can find files by permissions, users, groups, file types, date, size, and other possible criteria.


2 Answers

You can do this:

 find . -name "file2015-0*" | head -400 | xargs -I filename mv  filename ..

If you want to simulate what it does use echo:

 find . -name "file2015-0*" | head -400 | xargs -I filename echo mv  filename ..
like image 156
Tiago Lopo Avatar answered Sep 24 '22 20:09

Tiago Lopo


You can for example provide the find output into a while read loop and keep track with a counter:

counter=1
while IFS= read -r file
do
   [ "$counter" -ge 400 ] && exit
   mv "$file" ..
   ((counter++))
done < <(find . -name "file2015-0*")

Note this can lead to problems if the file name contains new lines... which is quite unlikely. Also, note the mv command is now moving to the upper level. If you want it to be related to the path of the dir, some bash conversion can make it.

like image 23
fedorqui 'SO stop harming' Avatar answered Sep 22 '22 20:09

fedorqui 'SO stop harming'