LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   loop through a DIR to delete files by inode (https://www.linuxquestions.org/questions/linux-general-1/loop-through-a-dir-to-delete-files-by-inode-788075/)

j-me 02-09-2010 02:56 PM

loop through a DIR to delete files by inode
 
I have a directory that contains some files (over a 1,000) that have a '\' in the filename. There are also some good files that I need to keep. therefore I need a script to delete based on inode.

What I have thus far:
list="$(ls -il /opt/PC/log/*RPOUT*.xml)" #this gives me the list
# I need to get the inode of.
for i in $list
do
find . -inum $i -exec rm -i {} \;
echo "delete file" $i >> /home/me/tmp/del_inode.txt
done
echo 'completed'

I know this is not right ... what I know is is the 'find' is. I need to loop through the $list gleaning inodes to get all the files with '\' delete. I'm not sure how. The $i gives me the whole line. I just want the first position.
set $() does not seem to work ... any ideas?

anomie 02-09-2010 03:13 PM

That approach seems overly complicated to me. Have a look at this:
Code:

$ touch foo 'ba\r' baz 'b\o\o'

$ ls
ba\r  baz  b\o\o  foo

$ rm *\\*

$ ls
baz  foo

P.S. Before attempting to delete anything, take backups of the directory.

j-me 02-09-2010 03:24 PM

I'll be ... loop was way too complicated
 
Thanks anomie.

I was way over thinking this. That worked like a champ.

Thank you!!! ;) you get a gold star for the day ...

j-me 02-09-2010 03:39 PM

rm: Argument list too long
 
what about:

-bash: /bin/rm: Argument list too long

is there a solution for that? It appears to be a limitation for rm as there is a very large list of files.

David the H. 02-09-2010 03:57 PM

Perhaps this will work.
Code:

find . -name "*\\\*" -delete
Of course, don't add the -delete option until after you've confirmed which files it will be deleting.

j-me 02-09-2010 04:05 PM

rm - Argument list too long
 
You guys are just awesome with solutions. There was 12137 files and rm puked but find worked fine.

Thanks!!!


All times are GMT -5. The time now is 03:01 PM.