Unique Directory and Folder Deletion Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unique Directory and Folder Deletion Script
# 1  
Old 06-24-2009
Unique Directory and Folder Deletion Script

Ok, so I just got charged with the task of deleting some 300 user folders in a FTP server to free up some space. I managed to grep and cut the list of user folders to delete into a list of one user folder per line.
Example:
bob00
jane01
sue03

In the home folder, there are folders a-z, and the user folders are stored in there based on the first letter of their user name...
Example:
b/bob00
j/jane01
s/sue03

So what I want to do is make a script that cd's into the first letter of the username and then deletes the folder based on my user list. Here's what I have thus far:

for firstlet in $('cat userlist.txt | cut -c 1'); do
cd $firstlet

My problem lies in the fact I need to find a way to put another conditional to delete the user folder associated with the letter it initially cd'ed with. Does anyone have any suggestions or another way of thinking this through? Any help is appreciated...thanks.
# 2  
Old 06-24-2009
Is this script going to be run locally on the FTP server or remotely using the FTP protocol? This will make a big difference in how this can be done and what commands are available.

- B
# 3  
Old 06-24-2009
This will run locally. Thanks
# 4  
Old 06-24-2009
Code:
#!/bin/ksh

while IFS= read user
do
  echo "first->[${user%%${user##?}}]"
  rm -rf ${user%%${user##?}}/${user}
done < userlist.txt

# 5  
Old 06-24-2009
Great! That worked seemed to have done it. Had to do some research on what each string operator did, but that was exactly what I needed. I really appreciate your help!
# 6  
Old 06-25-2009
Code:
cat your_folder_list_file | while read line;do
    dir=`echo $line | sed 's/\(.\).*/\1/'`
    folder=${dir}/${line}
    if rm -rf  $folder  && echo "$folder is delete" ;then
    :
    else
      echo "Fail to delete folder $folder"
    fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. Shell Programming and Scripting

Shell Script to Enter a Directory (Folder Name Contains Spaces)

I am writing a shell script to cd to a folder. folder name is "October Scripts" I am currently storing the name of folder in a shell variable as so path="October\ Scripts/". If I do cd $path I receive the error bash: cd: October\: No such file or directory What am I doing wrong? Kindly... (3 Replies)
Discussion started by: anand2308
3 Replies

3. Shell Programming and Scripting

Forceful deletion of folder not working

Hello, I am trying to delete a folder forcefully using the command: rm -rf <dir name> but its still asking for confirmation. Please help me out with a solution. scenario: bash-3.00$ chmod 777 gcc-3.4.2 bash-3.00$ bash-3.00$ bash-3.00$ bash-3.00$ rm -rf gcc-3.4.2/ rm: examine files in... (3 Replies)
Discussion started by: harishisnow
3 Replies

4. UNIX Desktop Questions & Answers

Trying to delete a directory pattern-selective deletion

Trying to delete a directory or a file using a pattern-selective deletion (using “*” and “?” ) (4 Replies)
Discussion started by: herberwz
4 Replies

5. Homework & Coursework Questions

unique words in files of folder and its subfolders

Hello, I tried to count all unique words of all files in one folder and its subfolders. Can anybody say me, why this doesnt work: ls| find -d | cat | tr "\ " "\n"| uniq -u | wc -l ??? Cat writes only the names of those files, but not the wors, which should be in them. Thanks for any advice. ... (9 Replies)
Discussion started by: Dworza
9 Replies

6. Shell Programming and Scripting

Rename folder/directory after running sort script

Hello, I'm trying to create a script that sorts documents by author (found in file name) and then create a directory for that author. For example, Input: John - Paper_1.txt John - Paper_2.txt Mark - Paper_1.txt Jill - Paper_1.txt Output: dir/John/Paper_1.txt dir/John/Paper_2.txt... (1 Reply)
Discussion started by: jl487
1 Replies

7. UNIX for Dummies Questions & Answers

isolate unique files in a folder

Hello, I have two folders. One has 1183 text files (folder A). The other (folder B) has 1160 of those 1183 files (the contents in these 1160 files are identical to the contents in the corresponding files in the other folder, but the names of the files in this folder are completely different... (2 Replies)
Discussion started by: juliette salexa
2 Replies

8. Shell Programming and Scripting

file and directory deletion script

I saw many post about this but could not find a specific answer to this question. i have the following code find . -depth -name "$FILEEXT" -ctime -30 -exec rm {} \; when i type ./deletefiles.sh *.txt for example, it will find all the txt files less than 30 days old and delete them. my... (9 Replies)
Discussion started by: new2learn09
9 Replies

9. Solaris

Script for automatic deletion of old folder

Hi, I have a folder with limited space. So i have to delete folder which are more than 5 days old automatically. So my script should be like delete the folder more than 5 days old. Can someone help me to generate a script for this. Thank you... Cheer Summer (5 Replies)
Discussion started by: summerpeh
5 Replies

10. Solaris

Deletion of Data from Lost+Found Directory

Hie I am running a sun solaris server of about 300 gigabytes disk capacity. The problem is that the machine has been having problems over the past year and at times the machine would just freeze or hang and had to be re-booted. Consequently there are too many entries in the lost+found... (1 Reply)
Discussion started by: Ranganai
1 Replies
Login or Register to Ask a Question