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
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
#!/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 }
RWm4aMp3 - converter arquivos de música m4a para mp3
Gerando QR code com script e Google Chart Tools
Enviar mensagem ao usuário trabalhando com as opções do php.ini
Meu Fork do Plugin de Integração do CVS para o KDevelop
Compartilhando a tela do Computador no Celular via Deskreen
Como Configurar um Túnel SSH Reverso para Acessar Sua Máquina Local a Partir de uma Máquina Remota
Configuração para desligamento automatizado de Computadores em um Ambiente Comercial
Compartilhamento de Rede com samba em modo Público/Anônimo de forma simples, rápido e fácil
Cups: Mapear/listar todas as impressoras de outro Servidor CUPS de forma rápida e fácil
Criando uma VPC na AWS via CLI
Tem como instalar o gerenciador AMD Adrenalin no Ubuntu 24.04? (12)