Score:2

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

in flag

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.

David avatar
cn flag
Really has nothing to do with the OS. The question is how to do this in a script or the command line.
Space Impact avatar
in flag
@David where would be the right place to post this? Could you please suggest?
David avatar
cn flag
It is fine to be here just being clear it is not an OS issue. The command or script would be the same on an Linux.
mchid avatar
bo flag
@David You could say that about almost any question on here. If they are using Ubuntu, it should be on topic.
mchid avatar
bo flag
Please include the version of Ubuntu you are using. Thanks.
Space Impact avatar
in flag
@mchid added the Ubuntu version that I am using.
James S. avatar
de flag
You are likely to have better luck asking this question in a more general place, as it is not a question about Ubuntu. try: https://unix.stackexchange.com/
bac0n avatar
cn flag
You can only use integer or single characters inside a sequence expression, brace expansion is performed before any other expansions, there will not be any interpretation of the text between the braces, and incorrectly formed brace expansion is left unchanged. You can do `eval rm {$a..$((a+2))}`, though.
Score:2
cn flag

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".

Space Impact avatar
in flag
It worked, Thanks for the answer. I will edit the question.
Space Impact avatar
in flag
How to remove everthing that is below or equal to `$a-1` but don't know the least number with a directory?
Tilman avatar
cn flag
Simply `rm -rf $(seq 1 $((a-1)))`. It will delete the existing directories, and the `-f` option will suppress the error messages about the non-existing ones.
Score:0
gb flag

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.

Cyrus avatar
cn flag
This is where I get goose bumps. Please note: [Why *not* parse `ls`?](http://unix.stackexchange.com/questions/128985/why-not-parse-ls)
AjayC avatar
gb flag
Thanks I don't think I have the skill set to fully understand the intricacies/issues with ```ls``` command as explained in the post. Frankly speaking I have not seen such complicated filename or directory names.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.