How to remove directories that have numbers as file names in Ubuntu using command line?

I have a directory where there are subdirectories which have numbers as directories. For example I have a parent directory test now I have some subdirectories like 1,2,3,4,5,6,7. Now I have a local exported variable like export a=3. I want to delete directories that are above or equal to $a and below or equal to $a+3, So I want to delete files 4,5. How can I achieve this in Ubuntu 18.04.6 LTS.

I tried rm -rf {$a..$($a+3)}, it didn't work, I tried so many other things but not successful.

8

2 Answers

One possible solution:

rm -r $(seq $a $((a+3)))

(Provided you really meant what you illustrated with your code sample, ie. directories above or equal to $a and below or equal to $a+3, instead of what you wrote in the text, "above >$3 but below <$3+2".

3

You could use the following method to remove folders or files.

  1. Send the output of the respective folder to a text file ls >> text.txt

  2. Edit the text.txt file to remove all those entries which you don't want to remove.

  3. issue the command sudo rm -r -i $(cat text.txt). Use -i flag if you want the process to be interactive.

  4. When removing files, do not use -r flag.

This method would work where the folders that you wish to remove are not in consecutive order.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like