Want to remove directories names 1 2 3 ...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to remove directories names 1 2 3 ...
# 1  
Old 08-19-2014
Want to remove directories names 1 2 3 ...

Dear All,

I have some list of directories names 1 2 3 .... n.
Number can vary each time when I run some software to create these folders.
When ever I wanted to run new case I have to remove all the sub-directories named 1 2 3 4 .... in case directory.
How can I do it using rm -r command?

Thanks & Regards,
linuxUser_

Last edited by rbatte1; 08-19-2014 at 01:24 PM.. Reason: Changed CODE tags to ICODE tags
# 2  
Old 08-19-2014
Blade

Hello,

Following may help. But please test it once in a QA environment first.

Code:
for i in 1 2 3 .. 10
do
    rm -rf $i
    if [[ `echo $?` == 0 ]]
    then
        echo "Directory "$i " has been deleted."
    else
        echo "Directory " $i " has not been deletd."
    fi
 
done


Thanks,
R. Singh

Last edited by RavinderSingh13; 08-19-2014 at 07:22 AM.. Reason: not sure why blade symbol is coming
# 3  
Old 08-19-2014
try
Code:
#!/bin/ksh 
x=1 
while [ $x -le 4 ] 
do 
rm -rf $x
x=$(( $x + 1 )) 
done

replace 4 by number of dirs you have.
This User Gave Thanks to Makarand Dodmis For This Post:
# 4  
Old 08-19-2014
Try
Code:
for i in [1-9] [1-9][0-9]; do echo rm -r $i; done

Remove echo if happy; add error handling if need be.

Last edited by rbatte1; 08-19-2014 at 01:26 PM..
# 5  
Old 08-19-2014
Thanks for the code but its not good way for me to use manually entered numbers.

Quote:
Originally Posted by RavinderSingh13
Hello,

Following may help. But please test it once in a QA environment first.

Code:
for i in 1 2 3 .. 10
do
    rm -rf $i
    if [[ `echo $?` == 0 ]]
    then
        echo "Directory "$i " has been deleted."
    else
        echo "Directory " $i " has not been deletd."
    fi
 
done

Thanks,
R. Singh
---------- Post updated at 04:11 PM ---------- Previous update was at 04:09 PM ----------

I will test your code. Its looking very simple.
I am currently written code very same as makarandhs code.
Thanks by the way for teaching something new Smilie

Quote:
Originally Posted by RudiC
Try
Code:
for i in [1-9] [1-9][0-9]; do echo rm -r $i; done

remove echo if happy; add error handling if need be.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove dates from file names

Hi, I'm using a shell script. I have extracted current date files to a directory1 and the date should be removed on both sides of a CSV file. FYI... I'm looking to remove the date from the file name and not inside the CSV file. Directory1 2017-07-12_gmr_tag_log_20170711.csv... (0 Replies)
Discussion started by: shivamayam
0 Replies

2. Post Here to Contact Site Administrators and Moderators

Urgent : please remove the names from the post

Hi Sir, There is an escalation going on this and could you please remove the words from the following link . this is really critical. https://www.unix.com/shell-programming-and-scripting/196647-remove-headings.html please remove following terms fiappl17dev fiappl18dev ... (1 Reply)
Discussion started by: ptappeta
1 Replies

3. Shell Programming and Scripting

Compare files with different names in different directories

Hi, I have a requirement to compare files in different directories with different names. The files have a datestamp in their name (It might not be a sequential datetimestamp). This is for Redhat Linux. I have more than 5 directories and more than 10 file in each directory to be compared. ... (4 Replies)
Discussion started by: GosarJunk
4 Replies

4. Shell Programming and Scripting

Comparing the pattern of the file names in 2 different directories

Hi, I have got a requirement for which i need your help. The following problem is required to get solved in PERL SCRIPT. Here is the requirement. There are 4 folders say SRC_DIR1, SRC_DIR2 and TGT_DIR_1,TGT_DIR_2 (Note: both path of SRC_DIR1 & SRC_DIR2 are different but both path of... (4 Replies)
Discussion started by: shadow_fawkes
4 Replies

5. Shell Programming and Scripting

**URGENT ** : Comparing the pattern of the file names in 2 different directories

Hi, I have got a requirement for which i need your help. The following problem is required to get solved in PERL SCRIPT. Here is the requirement. There are 4 folders say SRC_DIR1, SRC_DIR2 and TGT_DIR_1,TGT_DIR_2 (Note: both path of SRC_DIR1 & SRC_DIR2 are different but both path of... (1 Reply)
Discussion started by: shadow_fawkes
1 Replies

6. Shell Programming and Scripting

Extract function names and directories from php files

I need a script that extracts function names from php files together with their location (path and file in which they are defined). The php files are located in several directories under a base directory. Ideally the output should be something like: "Path/FileName/FunctionName" for a... (2 Replies)
Discussion started by: bamse
2 Replies

7. UNIX for Dummies Questions & Answers

Creating named directories from a list of names

Hi There, I'm a competent computer user that is learning basic unix commands. This is a slightly hypothetical question (for now at least!) but I can see a need for doing something like this in the near future. So I recently learned this command mkdir foo{1..100} which of course create's... (4 Replies)
Discussion started by: lookslikejames
4 Replies

8. Shell Programming and Scripting

Remove spaces between file names

Hi All, I have spaces in between file names. "Material Header.txt" "Customer Header.txt" "Vendor Header.txt" And how can I remove spaces between file names like below MaterialHeader.txt CustomerHeader.txt VendorHeader.txt Thanks Srimitta (10 Replies)
Discussion started by: srimitta
10 Replies

9. UNIX for Dummies Questions & Answers

remove all directories and sub-directories

Please could someone advise me the unix command to remove all directories and sub-directories with files within the directory structures. Thank you (6 Replies)
Discussion started by: venhart
6 Replies

10. Shell Programming and Scripting

Compare File Names in Different Directories...

I do not know much about shell scripting and need to create a script and I am at a loss. If someone can help me, that would be great!! I have two directories: /dir1 /dir2 I need to get the sequence number which is part of the filename in /dir1 and delete all files in /dir2 that are... (4 Replies)
Discussion started by: stky13
4 Replies
Login or Register to Ask a Question