You are not logged in.

#1 2013-10-02 16:11:26

jwhendy
Member
Registered: 2010-04-01
Posts: 621

Find unowned files via command line?

I'm interested in making a list of files not owned by any package and sending it to a text file I can review. I found this thread, which suggests pacpal (link appears to be broken now) and this script. I could try the script, but I noted from a bit back that the Arch news page listed a one-liner for finding unowned packages in the preparation of moving /usr/lib -> /lib:

$ find /bin /sbin /usr/sbin -exec pacman -Qo -- {} + >/dev/null

Here's the script linked to above, for reference:

#!/bin/bash
# Utility to generate a list of all files that are not part of a package
# Author: Spider.007 / Sjon

TMPDIR=`mktemp -d`
FILTER=$(sed '1,/^## FILTER/d' $0 | tr '\n' '|')
FILTER=${FILTER%|}

cd $TMPDIR
find /bin /boot /etc /lib /opt /sbin /usr /var | sort -u > full
pacman -Ql | tee owned_full | cut -d' ' -f2- | sed 's/\/$//' | sort -u > owned

grep -Ev "^($FILTER)" owned > owned- && mv owned- owned

echo -e '\033[1mOwned, but not found:\033[0m'
comm -13 full owned | while read entry
do
	echo [`grep --max-count=1 $entry owned_full|cut -d' ' -f1`] $entry
done | sort

grep -Ev "^($FILTER)" full > full- && mv full- full

echo -e '\n\033[1mFound, but not owned:\033[0m'
comm -23 full owned

rm $TMPDIR/{full,owned,owned_full} && rmdir $TMPDIR
exit $?

## FILTERED FILES / PATHS ##
/boot/grub
/dev
/etc/X11/xdm/authdir
/home
/media
/mnt
/proc
/root
/srv
/sys
/tmp
/var/abs
/var/cache
/var/games
/var/log
/var/lib/pacman
/var/lib/mysql
/var/run
/var/tmp

Is there any reason to use a script like the above compared to such a simple script, which appears to just be spitting a list of all files in /bin, /urs/sbin, and /sbin into `pacman -Qo`. What I can't figure out is what the end of the command does (the `-- {} + > /dev/null`). From various attempts to pipe that output into a text file, I've noticed that it just gets filled with the owned files... so I'm guessing something in there is filtering so that unowned get printed to stdout and owned go to /dev/null. I can't figure out how to redirect the unowned to a text file.

Thanks for any suggestions.

Offline

#2 2013-10-02 16:28:29

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: Find unowned files via command line?

What I can't figure out is what the end of the command does (the `-- {} + > /dev/null`).

"--" is often used to tell the program there are no more options. I didn't see it in the manpage, but that's the case with pacman as well.* The "{} +" is part of the find -exec argument, search for {} in the find manpage.

* I tested by running "pacman -- -Q", and got the error "no operation specified".

I can't figure out how to redirect the unowned to a text file.

The >/dev/null is redirecting stdout (list of owned files) to oblivion. The list of unowned files (and other errors) goes to stderr. To redirect stderr you use "2>" so try

find /bin /sbin /usr/sbin -exec pacman -Qo -- {} + >/dev/null 2>unowned_files

But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#3 2013-10-02 16:30:22

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Find unowned files via command line?

Offline

#4 2013-10-02 18:40:03

jwhendy
Member
Registered: 2010-04-01
Posts: 621

Re: Find unowned files via command line?

alphaniner wrote:

"--" is often used to tell the program there are no more options. I didn't see it in the manpage, but that's the case with pacman as well.* The "{} +" is part of the find -exec argument, search for {} in the find manpage.

Ah. Thanks for that. I'd been googling things like " linux command line '-- {}'" and not getting any hits. I combined the arguments incorrectly! Thanks for letting me know that this was part of `find`, as I was also looking in the man page for pacman wrongly.

alphaniner wrote:

The >/dev/null is redirecting stdout (list of owned files) to oblivion. The list of unowned files (and other errors) goes to stderr. To redirect stderr you use "2>" so try

find /bin /sbin /usr/sbin -exec pacman -Qo -- {} + >/dev/null 2>unowned_files

Perfect, and now I understand how `pacman -Qo` is working: it sends the answer to stdout if it knows the answer and to stderr if it doesn't find a hit. Thanks!

Offline

#5 2013-10-02 20:10:21

jwhendy
Member
Registered: 2010-04-01
Posts: 621

Re: Find unowned files via command line?

@karol: is there a great advantage to using the script from the links you provided over something like the one-liner above? It sounds like they omit some various directories to avoid spurious output. I'm not sure just how many files I'll get hits for, so maybe the length of that script is worth it to not have to sort through half my filesystem to find the actual stale files.

Offline

#6 2013-10-02 20:15:36

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Find unowned files via command line?

Redirect the output of various methods to different files and compare the results.

A script that doesn't list false positives is doubly useful:
* you're not going to remove files you do need, even though they're not owned by any package
* you don't have to sift through thousands of lines to find the 50 files you can remove.

Offline

Board footer

Powered by FluxBB