A
A
Alexander Nazarov2019-01-05 06:01:02
linux
Alexander Nazarov, 2019-01-05 06:01:02

How to remove all files of a certain extension from a folder and leave only a certain number of them?

One file is deleted with the command: rm file.txt, but how, for example, to delete all .txt files from the folder and leave only 10.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SOTVM, 2019-01-05
@sotvm

create a list of found files
echo "$(find *.txt -type f)" > list.txt
leave / delete the required number of lines in it
head --lines=10 list.txt > new_list.txt
then delete all files written to new_list.txt
cat new_list.txt | xargs rm -rv
all this can be simplified / shortened, but it will be clearer this way

K
kisaa, 2019-01-05
@kisaa

There is nothing to check, but I would dig towards find | tail | xargs rm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question