List files and delete them with find and rm

1

I tried to execute this command but I can not get it to list the files with a certain structure with awk and then delete the result. In the following case it deletes them but the output file generates it without information:

find /input1/input1/empleados/ -name "*" -type f -mtime +7 | \
      -not \( -path "*/auditoria/*" | \
      -o -path "/input1/input1/empleados/*.txt" \) | \
      -exec ls -lt {} \; | \
  awk '{FS=" "}{print $6"_"$7" "$8" "$9}' | \
  xargs rm -f > /tmp/Salida.txt
    
asked by Andres 09.06.2017 в 01:05
source

2 answers

3

I would try otherwise, I'm not sure that using ls output is a good idea, you can easily create problems with unusual file names.

Additionally I do not think you want the creation date, you may want the last modification date.

To test I created a file structure with the following commands and within that file structure I made the tests:

mkdir -p test/{auditoria,empleados} && cd test
touch -d "8 days ago" {.,auditoria,empleados}/{1..5}{,.txt}

First make sure that your find command does what you want. Search in man find the option printf , with it you can control the output:

find $PWD -type f -mtime +7 -not \( -path "$PWD/auditoria/*" -o -path "$PWD/empleados/*.txt" \) -printf '%a %p\n'

Now observe that the only thing we do is to execute the command rm with -exec and send the stdout that we controlled with -printf , so that it shows us what interests us, at tee , that passes it back to stdout and to the file tmp .

find $PWD -type f -mtime +7 -not \( -path "$PWD/auditoria/*" -o -path "$PWD/empleados/*.txt" \) -exec rm '{}' \+ -printf "%a %p\n" | tee tmp

Important : Try the commands in the test directory that we created at the beginning and then adjust the routes according to your particular case, otherwise you may end up deleting more than you want and I think that neither Stackoverflow nor its users assume any responsibility:).

Another option is to use a loop which, in my opinion, gives you more control and clarity about what you do, especially if you then have to do more work on those files.

My initial solution with a for (which I left at the end) is not a good idea because like the output of ls it will bring problems as mentioned by @fedorqui in your comment.

This would be a correct way to read find in a loop, note that we use the -print0 option that uses the null ( stderr ) character as a separator in the output instead of a new line, which can create an unwanted result with some file names:

while IFS= read -r -d '' file; do 
    stat $file --format="%y %n" | tee -a tmp
    #rm $file # ¿estás seguro de que vas a borrar lo que quieres borrar?
done < <(find $PWD -type f -mtime +7 -not \( -path "$PWD/auditoria/*" -o -path "$PWD/empleados/*.txt" \) -print0 2>/dev/null)

We send the find of /dev/null to rm in case there are errors in reading files.

Uncomment the stat command to delete the files.

Review the --format command, the -c (or %n ) option with %y and %code% give you the name of the file and the last modification date readable for humans.

This was the initial solution using a for, which is a good example of what not to do:

for file in $(find $PWD -type f -mtime +7 -not \( -path "$PWD/auditoria/*" -o -path "$PWD/empleados/*.txt" \) 2>/dev/null);do
    stat $file --format="%y %n" | tee -a tmp
    #rm $file # ¿estás seguro de que vas a borrar lo que quieres borrar?
done
    
answered by 09.06.2017 в 05:29
-2

find /input1/input1/empleados/ -type f -mtime +7 -not ( -path "/auditoria/" -o -path "/input1/input1/empleados/.txt" ) -exec ls -lt {} \; | awk '{FS=" "}{print $6"_"$7" "$8" "$9}'| xargs -I[] rm -f '[]'

    
answered by 09.06.2017 в 02:59