find - Script bash delete older files on directories with special chracters

07
2014-07
  • Abdel Karim Mateos Sanchez

    I use a script for delete older files on Trash or directories type Junk, or similar.

    It works fine except when it has special characters or spaces in the names

    /home/theos98/mail/theos98.es/dmalmierca/.Trash.Renfe Mart&AO0-n Fernandez
    /home/theos98/mail/theos98.es/dmalmierca/.Trash.Renfe Cordoba
    /home/theos98/mail/theos98.es/dmalmierca/.Trash.Renfe - Sevilla
    

    In this scenario not work

    I use two formats but not work

    for p in $(cat /tmp/listado.txt); do  find "$p" -type f -mtime +50 -delete; done
    

    or

    for p in $(cat /tmp/listado.txt); do  find $p -type f -mtime +50 -delete; done
    

    And two get this error

     /home/theos98/mail/theos98.es/dmalmierca/.Trash.Renfe Not found
    

    I dont' know how can pass to script correct line.

  • Answers
  • slhck

    The for statement "reads" input word by word. You need to read it line by line.

    while read -r line; do find "$line" -type f ...; done < /tmp/listado.txt
    

    The -r prevents backslashes in the lines from being interpreted.


  • Related Question

    bash - Apply a command to all files in a directory, one directory at a time
  • NickG

    I want to apply replaygain information to all the MP3 files in my music collection. To do this, I'm using a tool called mp3gain (on Linux).

    In order to apply the album gain correctly, I need to run the mp3gain command on a per directory basis. In other words, I need to find a directory, run mp3gain on all files in that directory, then repeat for every other directory. So far, I've come up with this:

     find . -type d -exec mp3gain {}/*.mp3 \;
    

    The output looks like this (only showing a couple of the many directories):

    [...]
    ./dev2/Physicist/*.mp3
    Can't open ./dev2/Physicist/*.mp3 for reading
    ./Real Things/*.mp3
    Can't open ./Real Things/*.mp3 for reading

    It appears to me that the '*' is being escaped, so rather than looking for all files ending in '.mp3', it is looking for a file called '*.mp3'.

    What command should I use?


  • Related Answers
  • John T

    Looks like it's not globbing properly. How about something like this:

    #!/bin/bash
    OLDIFS=$IFS
    IFS=$(echo -en "\n\b")
    for dir in $(find . -type d)
    do
            mp3gain $dir/*
    done
    IFS=$OLDIFS
    

    as a single command:

    OLDIFS=$IFS;IFS=$(echo -en "\n\b");for dir in $(find . -type d);do mp3gain $dir/*;done;IFS=$OLDIFS
    
  • user19647

    It might not look nice, but this command will find all directories that contain mp3 files write them to a tempfile and then go through that tempfile listing the contents of each dir. If you're happy with the Output, you can go ahead and plug your mp3gain command into it in place of the ls.

    Setting the IFS variable to a newline character is important so that you can work with files and directories that contain spaces.

    The reason I've chosen to list directories containing mp3s first is in case mp3gain throws an error when encountering empty sets of files. In this way it never encounters such a situation.

    IFS=$'\n'; for i in `find -type f -iname *.mp3`; do dirname $i ; done | sort | uniq > ~/mp3directories.txt && for i in `cat ~/mp3directories.txt`; do ls -1 $i/*.mp3 ; echo ; done
    
  • Rebol Tutorial

    You can use Rebol it works on Unix and Windows

    How to apply a function to all files in a directory recursively

    http://reboltutorial.com/blog/how-to-apply-a-function-to-all-files-in-a-directory-recursively/