RHEL5, Bash and directory removal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting RHEL5, Bash and directory removal
# 1  
Old 12-12-2008
RHEL5, Bash and directory removal

Hello,
I am trying to create a simple bash script that will run on a linux box and when invoked, go through a specified directory and remove the the oldest directory contained there in. This is perhaps not the optimal forum but I am hoping that someone out there can lend an assit before I lose my mind.

Running from the command line the ls* line executes fine, run from a script it does not.

Also, if I attempt to perform ls /data/backups -1A | wc -l again in the loop to update the DirCount, it does not work.

Thanks in advance

#!/bin/bash
while [ $DirCount -ge $MAX]
do
ls -t /data/backups | tail --lines=1|xargs rm -rf
let DirCount-=1
done

Last edited by rstrst; 12-12-2008 at 09:19 PM..
# 2  
Old 12-12-2008
Quote:
Originally Posted by rstrst
Hello,
I am trying to create a simple bash script that will run on a linux box and when invoked, go through a specified directory and remove the the oldest directory contained there in. This is perhaps not the optimal forum but I am hoping that someone out there can lend an assit before I lose my mind.

Running from the command line the ls* line executes fine, run from a script it does not.

In what way does it not execute? What happens? Do you get any error messages? Any output?
Quote:

Also, if I attempt to perform ls /eq36/srmd/data/backups -1A | wc -l again in the loop to update the DirCount, it does not work.

Please enclode code in [code] tags.
Quote:
Code:
#!/bin/bash
while [ $DirCount -ge $MAX]


Where have you initialized $DirCount?
Quote:
Code:
do
ls -t /data/backups | tail --lines=1|xargs rm -rf


Have you looked at the output of ls -t /data/backups | tails --line=1? That's what you are passing to rm.
Quote:
Code:
let DirCount-=1
done

# 3  
Old 12-12-2008
Quote:
Originally Posted by cfajohnson
In what way does it not execute? What happens? Do you get any error messages? Any output?
Please enclode code in [code] tags.
Where have you initialized $DirCount?
Have you looked at the output of ls -t /data/backups | tails --line=1? That's what you are passing to rm.

The line in question does not throw any errors, the end result that I am looking for (and see when it is run from the command line) simply doesn not happen (i.e. the oldest directory being removed)

All variables were initialized at the top of the script and opearted as advertised. Initialization is as follows prior to loop :

[
NUMBER_OF_BACKUPS=2
BACKUP_DIR=/data/backups
DATE_FORMAT="+%Y-%m-%d_%H_%M_%S"
SRM_DIR=sub
DirCount=`ls /data/backups -1A | wc -l`

if [ $DirCount -ge $NUMBER_OF_BACKUPS ]
then
echo "Reached Maximum of $NUMBER_OF_BACKUPS Backups : Performing Cleanup"
fi
]

Yes, I have looked at the output of the ls -t /data/backups | tails --line=1 command. It contained the name of the oldest directory as expected. Using bash -x to run the script I see rm -rf <name>, agian as I would expect however the directory fails to be removed. Permissions all seem to be set open to allow this to occur.
# 4  
Old 12-12-2008
Quote:
Originally Posted by rstrst
Yes, I have looked at the output of the ls -t /data/backups | tails --line=1 command. It contained the name of the oldest directory as expected. Using bash -x to run the script I see rm -rf <name>, agian as I would expect however the directory fails to be removed. Permissions all seem to be set open to allow this to occur.

What does <name> contain?

What does ls -ld <name> show?

What is your current directory (echo $PWD or pwd)?
# 5  
Old 12-13-2008
Quote:
Originally Posted by cfajohnson
What does <name> contain?

What does ls -ld <name> show?

What is your current directory (echo $PWD or pwd)?

<name> contians the oldest directory name. In this case TestDir1

ls -ld displays permissions of drwxr x r x myname mygroup size date time and TestDir1

pwd shows /data/backups
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create directory and sub-directory with awk and bash

In the below I am trying to create a parent directory using the R_2019 line from f1 if what above it is not empty. I then create sub-directories under each parent if there is a match between $2 of f1 and $2. Inside each sub-folder the matching paths in $3 and $4 in f2are printed. If there is no... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. UNIX for Beginners Questions & Answers

Sourcing file from parent directory bash

"Debian 9 64x - LXDE" I try to source a file from my parent directory: #!/bin/bash #source.bash . ../links.bash but i get "file not found". I tried . "../links.bash" and . '../links.bash'. I got on all methods the same result. If i use the absolute path it works, but i don't want to... (4 Replies)
Discussion started by: int3g3r
4 Replies

3. Programming

Cant remove directory on bash script

hi all, this is my script and as you can see in the screenshot i attach its not deleting the directory in the source folder #!/bin/bash cd /vol/cha-work/_ARCHIVE/to_be_archived/audio/robert_test temp=/mnt/robert_test/temp dest=/vol/cha-archive/audio echo "is this archive for an... (2 Replies)
Discussion started by: robertkwild
2 Replies

4. Shell Programming and Scripting

Bash to delete file in directory

Will the below bash delete all the "snps.ivg" in the given directory? Thank you :) find N:\all_array\Samples -maxdepth 1 -type f -name "snps.ivg" -delete (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Linux

bash:umount/: no such file or directory

I am trying to connect two system (let's say for time being) together such that it supports clustering. for this I got the following packages: # sudo apt-get install pacemaker sysv-rc-conf glusterfs-server glusterfs-examples glusterfs-client chkconfig nmap ntp Next, I did... (0 Replies)
Discussion started by: dr_mabuse
0 Replies

6. Shell Programming and Scripting

bash script using scp (pw typed by hand) followed by removal of files

Hello, I tried to write a bash script (code is below) that does scp files that contain a certain string, and that subsequently deletes only those files that have been copied (in my case new files are created every second so it is important to only delete those that have been copied). The key is... (0 Replies)
Discussion started by: kjartan
0 Replies

7. Shell Programming and Scripting

bash script directory size

hello! i need to make a script that get a folder name in parameter and i get back the size of the folder include the subfolders! but i dont know how i need to start :S Example: a folder contain the followings a: drwxr-xr-x 2 user user 4096 febr 25 08.27 b -rw-r--r-- 1 user user 2 febr... (3 Replies)
Discussion started by: impish
3 Replies

8. Shell Programming and Scripting

Bash: How to get part of the directory name?

How can I write a script that determines the directory the user is in, and displays that path up until a particular point? Specifically, I need to find the text "packages" in the directory name, then I need to capture that directory and the one below it. For example, if the user is in the... (5 Replies)
Discussion started by: RickS
5 Replies

9. UNIX for Dummies Questions & Answers

starting bash in a directory

I wish to start bash in a directory at a per invocation basis. i.e, this has nothing to do with a user's home directory. In my program, I should be able to give: "bash "/lib/examples" in one place and bash "/lib/src" in another. A shell would start in that folder for me to work in. This... (2 Replies)
Discussion started by: sentinel
2 Replies

10. Solaris

bash: ./<scriptname>: No such file or directory

I have simply downloaded a file to my local folder, made one small change and uploaded it back, ever since it will not run the file. I have now uploaded the original, unchanged file, which still doesn;t work. Evertime it is run ot says bash: ./nrcardprint: No such file or directory The... (10 Replies)
Discussion started by: Heidi.Ebbs
10 Replies
Login or Register to Ask a Question