Remover/Exibir Arquivo/Diretório de uma estrutura de diretórios

Publicado por Italo Pessoa (última atualização em 05/10/2012)

[ Hits: 4.836 ]

Homepage: http://xconhecimento.blogspot.com.br

Download rm_r.sh




Script simples para procurar e remover ou exibir o path de um arquivo ou diretório.
O script é simples

rmr_init [-f | -d] directory [directory | file]
rmr_init [-f | -d] [-e | -r] directory [directory | file]
                                                        
-e show the path of file or directory
-r remove the file or directory
-f set the type to regular file
-d set the type to directory

Já existe o find para fazer a mesma coisa, pesquisar e remover um diretório ou arquivo, mas para efeito de aprendizagem resolvi fazê-lo

  



Esconder código-fonte

#!/bin/sh
# rm_r.sh

# Script to found and remove or display 
# a regular file or directory in a directory
# and their sub-directories

# Italo Pessoa <italoneypessoa@gmail.com>
# 01/10/2012 20:15

# # # # # # # # # # # # # HOW TO # # # # # # # # # # # # # # #
# source rm_r.sh                                             #
# rmr_init [-f | -d] directory [directory | file]            #
# rmr_init [-f | -d] [-e | -r] directory [directory | file]  #
#                                                            #
# -e show the path of file or directory                      #
# -r remove the file or directory                            #
# -f set the type to regular file                            #
# -d set the type to directory                               #
# # # # # # # # # # # # # HOW TO # # # # # # # # # # # # # # #

# list of files and directories of the directory informed
_lsDir(){

   # verify if is a regular file or a directory
   if [ "$ISFILE" ]; then
      _findMatchingFile "$1" "$2" "$ACTION"
   else
      _findMatchingDir "$1" "$2" "$ACTION"
   fi
   
   for dir in $(ls -A "$1" | sed 's/\ /(~)/g'); do
      
      # dirctory name
      dirName=$(echo "$dir" | sed 's/(~)/\ /g')

      # verify if the directory exists
      if [ -d "$1/$dirName" ]; then
         # list of files and directories of the directory informed
         _lsDir "$1/$dirName" "$2"
      fi
   done
}

# scan files on the current directory
_findMatchingFile(){

   # regex to remove spaces
   for file in $(ls -A "$1" | sed 's/\ /(~)/g'); do

      # regex to replace previous to a correspondent value of spaces
      fileName=$(echo "$file" | sed 's/(~)/\ /g')

      # verify if is a regular file and exists
      if [ -f "$1/$fileName" ]; then

         # verify if is the same name of the searched file
         if [ "$fileName" = "$2" ]; then

            # verify if has action
            if [ "$ACTION" ]; then

               # validate action
               case "$ACTION" in
                  "-r" )
                     # remove file
                     echo "Removing file:"
                     echo "$1/$fileName"
                     rm "$1/$fileName"
                     echo "Removed."
                  ;;
                  "-e" )
                     # displays the path of file, starting of the current user 
                     # directory, the directory from which the current user is
                     echo "$1/$fileName"
                  ;;
               esac
            else
               # STANDARD
               # displays the path of file, starting of the current user 
               # directory, the directory from which the current user is
               echo "$1/$fileName"
            fi
         fi
      fi
   done
}

# scan directories on the current directory
_findMatchingDir(){

   # regex to remove spaces
   for dir in $(ls -A "$1" | sed 's/\ /(~)/g'); do
      
      # regex to replace previous to a correspondent value of spaces
      dirName=$(echo "$dir" | sed 's/(~)/\ /g')
      
      # verify if is a directory and exists
      if [ -d "$1/$dirName" ]; then

         # verify if is the same name of the searched directory
         if [ "$dirName" = "$2" ]; then

            # verify if has action
            if [ "$3" ]; then
               case "$3" in
                  "-r" )
                     # remove directory
                     echo "Removing directory:"
                     echo "$1/$dirName"
                     rm -r "$1/$dirName"
                     echo "Removed."
                  ;;
                  "-e" )
                     # displays the  path of direcory, starting of the current user 
                     # directory, the directory from which the current user is
                     echo "$1/$dirName"
                  ;;
               esac
            else
               # STANDARD
               # displays the  path of direcory, starting of the current user 
               # directory, the directory from which the current user is
               echo "$1/$dirName"
            fi
         fi
      fi
   done
}

# verify is the directory exists
_dirExists(){
   if [ ! -d "$1" ]; then
      echo "ERROR: The directory \"$1\" doesn't exists."
      exit 1
   fi
}

# configure the action
_configAction(){
   case "$1" in
      "-r" )
         # remove directory or file
         ACTION="-r"
      ;;
      "-e" )
         # displays the path of file or directory
         ACTION="-e"
      ;;
      *)
         echo "ERROR: The option \"$1\" doesn't exists."
         exit 1
      ;;
   esac
}

rmr_init(){

# key to control the type of file (regular or directory)
export ISFILE=""

# key to control the action
export ACTION="-e"

   # verify how many parameters were informed
   if [ "$#" -ge 3 ]; then
       if [ "$#" -eq 4 ]; then
          case "$1" in
             "-f" )
               # verify if directory exists
               _dirExists "$3"
               # configure the action
               _configAction "$2"
               # set the type of file to regular file
               ISFILE=1
             ;;
             "-d" )
               # verify if directory exists
               _dirExists "$3"
               # configure the action
               _configAction "$2"
               # set the type of file to directory
               ISFILE=""
            ;;
            *)
               # display error message
               echo "ERROR: You must set: file(-f), directory(-d), remove(-r) or show(-e)."
               exit 1
            ;;
          esac

          # list of files and directories of the directory informed
          _lsDir "$3" "$4"

       elif [ "$#" -gt 4 ]; then
          echo "ERROR: You inserted many options"
          exit 1
       elif [ "$#" -eq 3 ]; then
          case "$1" in
             "-f" )
               _dirExists "$2"
               ISFILE=1
             ;;
             "-d" )
               _dirExists "$2"
               ISFILE=""
            ;;
            *)
               # display error message
               echo "ERROR: You must set: file(-f) or directory(-d)."
               exit 1
            ;;
          esac

          # list of files and directories of the directory informed
          _lsDir "$2" "$3" 
       fi
   else
      # display error message
      echo "ERROR: you must insert at least three parameters."
   fi
}

Scripts recomendados

RWm4aMp3 - converter arquivos de música m4a para mp3

Firewall

Gerando QR code com script e Google Chart Tools

Anti Pop-UP Speedy

tagtool


  

Comentários
[1] Comentário enviado por zirou em 05/10/2012 - 11:37h

Otimo script cara, parabéns ae

[2] Comentário enviado por italo pessoa em 05/10/2012 - 23:16h

Obrigado zirou!!


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts