Shell Script to Delete a File from Every Directory Above the Present Working Directory Last Updated : 12 Nov, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to delete a file from every directory above the parent working directory using shell script in Linux. To delete a file from a directory we use the rm command in Linux/UNIX. Syntax: rm [OPTION]... [FILE]... Using shell script, we have to delete the input file from every directory above the present working directory. The filename will be passed as an argument. The following diagram shows the tree structure of the desktop directory. In the above image, we are currently present in the /Desktop directory. Present working Directory can be seen using the pwd command.In the tree structure in the above diagram, we have a GFGfile.txt file. GFGfile.txt is a regular text file.This file is located in 4 directories, namely, GFG, AAAA, BBBB, CCCC.Now we will go to the "CCCC" directory using the cd command. We will delete the other GFGfile.txt above the present working directory. i.e. All GFGfile.txt will be deleted except the file present in the "CCCC" directory.Approach:Initialize flag=1Initialize homeDir as '/home' directoryLoop until flag becomes 0 else go to step 13.go to the Previous directory using the "cd .." command.set pwd Command to present working directoryremove the file using the rm commandif the present working directory is equal to the home directory then set a flag to 0Go to step 7Stop Below is the implementation. # check whether arguments are passed or not if [ $# -eq 0 ] then # if arguments are not passed then display this echo "pass the file name" # exit the program exit # end if fi # store the argument in fileName fileName=$1 # set flag flag=1 # set homeDir to home directory homeDir=/home # loop till flag becomes 1 while [ $flag -eq 1 ] do # go to previous directory cd .. # set pwdCommand to present directory pwdCommand=`pwd` # print pwd/filename echo "$pwdCommand/$fileName" # remove the file from present directory rm "$pwdCommand/$fileName" 2> /dev/null # set flag to 0 if we reach to /home directory if [ $pwdCommand = $homeDir ] then flag=0 # end if fi # end while done Output: To run the shell script use ./delFile.sh <filename> Give execution permission to shell script using: chmod +x delFile.sh All the GFGfile.txt files have been delete above the present working directory Comment More infoAdvertise with us Next Article Shell Script to Delete a File from Every Directory Above the Present Working Directory S shantanujain18 Follow Improve Article Tags : Linux-Unix Shell Script Linux-Shell-Commands Similar Reads Shell Script to Delete Zero Sized Files From a Directory A zero-sized file is a file that contains no data and has a length of 0. It is also called a zero-byte file. Incomplete transfers can be responsible for the zero-sized file. You can create a zero-sized file intentionally by running the following command in the terminal : touch zero_sized_file_name O 3 min read Shell Script To Show Names of All Sub-Directories Present in Current Directory In this given program, we are required to write a shell script to print the list of all the sub-directories present in the current directory. Currently, when we are running this program, the terminal is opened at the root directory location and hence, we are getting a list of sub-directories present 2 min read How to check if a directory or a file exists in system or not using Shell Scripting? Shell scripting is really a powerful and dynamic way to automate your tasks. To test if a directory or file already exists in the system or not we can use shell scripting for the same along with test command. To proceed with the test script lets first check the test manual. To open a manual use the 2 min read Shell Script to Delete the Zero Sized File Using If and For There are many files in our system which are of no use for us as temporary files. So we need to clear all those files, but it is very hectic to find each file and delete them. Here we will create a script that will automatically search for the zero-sized file and delete them all. Before starting, we 2 min read How to move all files from one directory to another using Python ? In this article, we will see how to move all files from one directory to another directory using Python. Â In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil m 2 min read Like