Script to delete files in Linux

Script to delete files in Linux

Linux is a powerful operating system, it consists of various tools which can be utilised by the user to achieve maximum efficiency in his/her work routine. Linux provides user with a fluid environment where the user can run some scripts to carry out some basic working and functionalities. Today we will see how can we make a script to list and delete all the files of similar type with just a single click, we will also make sure to make this script interactive so that it can be even used by a script-kidde 🙂

Let’s consider an example where you might have a directory consisting of a lot of ‘.C’ or ‘.java files’, now after completion of your project you might want to delete all the files of type ‘.C’ only. Our script can be easily used in such cases and is very quick and efficient. Here is a step by step illustration-

  • The first step is to ask the user to input the type of file he might want to delete, to print this into the terminal we will use the command ‘echo’ followed by the text you want to display

  • So the next step is to accept the input from the user, this can be simply done by the command ‘read’ followed by the variable you want to store the input in (we have taken it as ‘x’)

  • The task now is to find all the files of type ‘.x’ where ‘x’ is the variable entered by the user indicating the extension of file. This can be done in multiple ways, we will use the command ‘find’ to display as this command can be even used in deletion operation. The command goes as follows –

  • Here find is followed by ‘-name’ which signify the name which we have taken equal to ‘*.$x’ (here ‘*’ signifies all, ‘$’ is used to get the value of ‘x’) we have then used ‘-print’ to print the complete result.

 

  • The above command displays all the files then we ask user whether he wants to delete the listed files, depending open his input we delete the files

  • Here we again use the ‘find’ command again in conjunction with the
    ‘rm’ (remove) through a redirection operator (>) which basically means that remove will evaluate the result of ‘find’ command. The ‘find’ command is used similarly as in previous steps.

  • If user selects 0 then else statement is executed and the program is then exitedExecuting the script –
  • The user can open terminal and move to the directory where script is placed. After moving to the correct directory the user can use command as ‘./filename.sh’ to execute the script

  • The user can now interact withe script and get the results according below is attached representation of deleting files of type ‘.C’
  • This is the code if you are too lazy to type 🙂
  • echo "Hello"
    echo "Enter type of files you want to delete"
    read x
    echo "All files of type $x"
    find . -name "*.$x"  -print
    echo "Confirm Delete?(1/0)"
    read z
    if [ $z -eq 1 ] ;
    then
    rm>find . -name *.$x
    echo "Successful"
    else
    echo "Exiting.."
    fi
    
    

 

Previous Script to Make an Instagram Bot to get Followers
Next How to make files Hidden in Linux

About author

You might also like

Scripting 0 Comments

How to make files Hidden in Linux

Linux provides us the functionality to hide the files for security as per user preference, users can easily hide files by using some simple command or even use GUI for

Scripting 0 Comments

Script to Make an Instagram Bot to get Followers

Backstory: So last week during my internship one of the work assigned to me was to go and follow some random profiles on Instagram through the company’s page. You might wonder why

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply