Answer the question
In order to leave comments, you need to log in
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question