Cant remove directory on bash script


 
Thread Tools Search this Thread
Top Forums Programming Cant remove directory on bash script
# 1  
Old 03-24-2017
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

Code:
#!/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 audio tar press (t) or an audio directory press (d)"
	read option

	case $option in

		d)

			echo "please specify full path to directory you want to be made into a tar"
			read -e dir

#			echo "please enter ID number ie ID1234"
#			read id

#			echo "please specify where you want the tar file to be stored"
#                       read -e dest

			cd "$dir"

			base=$(basename "$dir")

				echo -e "create "$base" into "$base".tar\n"
                                echo -e "move "$base".tar to "$dest"\n"
#                               echo -e "remove "$base".tar from "$dir"\n"
				echo -e "remove "$base" from "$dir"\n"

                                        echo "is this information correct, press (y) or press (n)"
                                        read correct

                                        case $correct in

                                                y)
							echo "the script will now continue";;

                                                n)
							echo "please re-run the script inputting correct details"
                                                        exit;;

                                                *)
							echo "invalid selection, please re-run the script"
                                                    	exit;;

                                        esac				

			date >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo "" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
#			echo -e ""$id"\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo -e ""$dir"\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo -e "how many files = `find . -type f | wc -l`\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo -e "size of directory = `du -sh`\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			ls -R >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			
			cd ..

			if ! cp -R "$base" "$temp" ; then
				echo "something went wrong rsync command, please do manually"
				exit
			fi

			cd "$temp"

			if ! tar -cf "$dest"/"$base".tar "$base" ; then
				echo "something went wrong creating the tar, please do manually"
				exit
			fi

			if ! rm -rf "$temp"/"$base" ; then
				echo "something went wrong with the rm -rf command, please do manually"
				exit
			fi

			if ! rm -rf "$dir" ; then
				echo "something went wrong with the rm -rf command, please do manually"
				exit
			fi
			;;

		*)

			echo "invalid selection, please re-run the script"
			exit;;

	esac

where it says please enter the directory i have entered -

testdirtobearchived

and as you can see it creates the tar file fine and stores it in the destination folder but it doesnt remove the directory in robert_test

why is this?

many thanks

rob
Cant remove directory on bash script-scriptpng
# 2  
Old 03-24-2017
First: screen shots don't help way as much as do execution logs posted in text form, e.g. created by setting the shell's -x (xtrace) option. The latter can be analysed line by line, whereas your screen shot doesn't allow for almost anything...
Second: if you exit anyway after an error has occurred, why not use the shell's -e (exit on non-zero status) and trap builtin?

Where exactly is the script stuck? With all the confusing directory definitions and cding, I can't see a file removal in robert_test as "$temp"/"$base" should translate to /mnt/robert_test/temp/testdirtobearchived
# 3  
Old 03-28-2017
sorry its been so long in replying back but sorted it

Code:
#!/bin/bash

source=/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 audio tar press (t) or an audio directory press (d)"
	read option

	case $option in

		t)
			cd "$source"

			echo "please specify full path to tar file"
			read -e tar

			base=$(basename "$tar")

#			echo "please enter ID number ie ID1234"
#			read id

#			echo "please specify where you want the tar file to be stored"
#			read -e dest

#				echo -e "rename "$base" to "$id"_"$base"\n"
	                       	echo -e "move "$base" to "$dest"\n"
                        	echo -e ""$base" will be removed from "$tar"\n"

					echo "is this information correct, press (yes) or press (no)"
					read correct
	
					case $correct in

						yes)
							echo "the script will now continue";;

						no)
							echo "please re-run the script inputting correct details"
							exit;;

						*)
							echo "invalid selection, please re-run the script"
							exit;;

					esac

#			if ! mv "$tar" "$base" ; then
#				echo "something went wrong with the move command, please do manually"
#				exit
#			fi

			if ! mv "$base" "$dest" ; then
				echo "something went wrong with the move command, please do manually"
				exit
			fi

#			if ! rm -f "$base" ; then
#				echo "something went wrong with the rm -f command, please do manually"
#				exit
#			fi
			;;

		d)
			cd "$source"

			echo "please specify full path to directory you want to be made into a tar"
			read -e dir

#			echo "please enter ID number ie ID1234"
#			read id

#			echo "please specify where you want the tar file to be stored"
#                       read -e dest

			cd "$dir"

			base=$(basename "$dir")

				echo -e "COPY "$base" to "$temp"\n"
                                echo -e "CREATE "$base".tar in "$temp"\n"
				echo -e "COPY "$base".tar to "$dest"\n"
				echo -e "REMOVE "$base".tar from "$temp" \n"
                                echo -e "REMOVE "$base" from "$temp"\n"
				echo -e "REMOVE "$base" from "$source"/"$dir"\n"

                                        echo "is this information correct, press (yes) or press (no)"
                                        read correct

                                        case $correct in

                                                yes)
							echo "the script will now continue";;

                                                no)
							echo "please re-run the script inputting correct details"
                                                        exit;;

                                                *)
							echo "invalid selection, please re-run the script"
                                                    	exit;;

                                        esac				

			date >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo "" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
#			echo -e ""$id"\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo -e ""$dir"\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo -e "how many files = `find . -type f | wc -l`\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			echo -e "size of directory = `du -sh`\n" >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			ls -R >> /vol/cha-work/_ARCHIVE/inventories/"$base".csv
			
			cd ..

			if ! cp -R "$base" "$temp" ; then
				echo "something went wrong with the recursive copy command, please do manually"
				exit
			fi

			cd "$temp"

			chmod -R 777 "$temp"

			if ! tar -cf "$base".tar "$base" ; then
				echo "something went wrong creating the tar, please do manually"
				exit
			fi

			if ! cp "$base".tar "$dest" ; then
				echo "something went wrong copying the tar, please do manually"
				exit
			fi

			if ! rm -f "$temp"/"$base".tar ; then
				echo "something went wrong removing the tar, please do manually"
				exit
			fi


			if ! rm -rf "$temp"/"$base" ; then
				echo "something went wrong removing from temp, please do manually"
				exit
			fi

			cd "$source"/"$dir"
			cd ..

			if ! rm -rf "$base" ; then
				echo "something went wrong removing from source, please do manually"
				exit
			fi;;

		*)

			echo "invalid selection, please re-run the script"
			exit;;

	esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove original file from directory after bash executes

The below bash works great, except I can not seem to delete the original file $f from the directory. Thank you :) For example, after the bash executes there are 8 files in the directory: 123.txt (original file) 123_remove.txt 123_index.txt 123_final.txt 456.txt (original file)... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. UNIX for Dummies Questions & Answers

Script to remove zip files from a directory

Hi Folks, There is a job which generates a .zip files every day at /usr/app/generated directory , now please advise for the script that will delete this zip files permanently.but while deleting it should make sure that it will not delete the last two days recently generated zip files and this... (1 Reply)
Discussion started by: punpun66
1 Replies

3. Shell Programming and Scripting

How to remove comments from a bash script?

I would like to remove comments from a bash script. In addition, I would like to remove lines that consist of only white spaces, and to remove blank lines. #!/bin/bash perl -pe 's/ *#.*$//g' $1 | grep -v ^]*$ | perl -pe 's/ +/ /g' > $2 # # $1 INFILE # $2 OUTFILE The above code... (10 Replies)
Discussion started by: LessNux
10 Replies

4. Shell Programming and Scripting

shell to script to remove directory

Hello All I want to delete the directory in my tree structure . all the filenames like this dd-mm-yyyy Example 30-01-2011 31-01-2011 01-02-2011 I want to grep the latest last four days created directorey by not using the find command . how do i do that ? (1 Reply)
Discussion started by: kvk_shell
1 Replies

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

6. Shell Programming and Scripting

An Issue with the script which used to remove a file from the current directory.

Hello forum members, I am writing a script to two tasks. 1: displaying the list of the files in the current directory. 2: removing the specifed file from the list. I have written a sample script ,so can u please verfiy and correct. echo Enter list of files ls *.txt read textfile rm -f... (3 Replies)
Discussion started by: sivaranga001
3 Replies

7. Shell Programming and Scripting

How to get rid of cannot remove file error in bash script?

Hi Guys, I am creating a couple of temp. files in a script. After completing the script, I am using the rm command to delete these files. The files are getting deleted but I am getting "filename - cannot find file;no such file or directory" error in my bash shell terminal window. I am using... (3 Replies)
Discussion started by: npatwardhan
3 Replies

8. Shell Programming and Scripting

Remove shell script and its directory

Hi, I have shell script which is located in /opt/Test/test.sh code in test.sh is as follows #!/bin/sh rm -rf /opt/Test exit 0 when I tried to execute the above script from its location, it shows the following error rm: Cannot remove any directory in the path of the... (2 Replies)
Discussion started by: raghu.amilineni
2 Replies

9. Shell Programming and Scripting

remove directory x seconds after script completes

Hi guys, I am working with a script within a Mac OS X package installer. The package installer will run the bash script once the files have been copied/installed. I have a little trouble with the last line of my script causing the package installer to crash once in a while and I have narrowed it... (1 Reply)
Discussion started by: tret
1 Replies

10. Shell Programming and Scripting

Script to remove all empty files within the directory structure?

Hi I need to write a shell script which basically searches for all the empty files within the directory structure, lists them before asking the user to confirm if they would like to delete them. If the user deletes the file then a notice would appear confirming the file is deleted. I've be... (5 Replies)
Discussion started by: cat123
5 Replies
Login or Register to Ask a Question