Difference between revisions of "Lostfiles"

From ArchWiki
Jump to: navigation, search
m (" don't " -> " do not ")
Line 1: Line 1:
 
[[Category:Scripts (English)]]
 
[[Category:Scripts (English)]]
 
__NOTOC__
 
__NOTOC__
 
== Description ==
 
 
 
'''Lostfiles''' is a script for detecting orphaned files (files which are not owned by any Arch Linux packages).
 
'''Lostfiles''' is a script for detecting orphaned files (files which are not owned by any Arch Linux packages).
  
The script ignores by default a series of directories where packages should not install files. Some files might appear as removed if they're placed in those directories which are not checked (for example, some packages install files in /tmp - didn't have time to track which of them).
+
The script ignores by default a series of directories where packages should not install files. Some files might appear as removed if they're placed in those directories which are not checked.
 
+
For comments or suggestions, contact [[User:IceRAM|the author (IceRAM)]].
+
  
 
== Script source ==
 
== Script source ==
Line 14: Line 9:
 
  #!/bin/bash
 
  #!/bin/bash
 
   
 
   
  # LostFiles v0.1
+
  # LostFiles v0.2
 
  # License: GPL v2.0 http://www.gnu.org/licenses/gpl.html
 
  # License: GPL v2.0 http://www.gnu.org/licenses/gpl.html
 
   
 
   
Line 20: Line 15:
 
  # Mircea Bardac (dev AT mircea.bardac.net)
 
  # Mircea Bardac (dev AT mircea.bardac.net)
 
  # http://mircea.bardac.net/
 
  # http://mircea.bardac.net/
 +
# Modified by Jan Janssen
 
   
 
   
 
  # Description:
 
  # Description:
Line 32: Line 28:
 
         exit
 
         exit
 
  fi
 
  fi
   
+
  comm -3 \
cd /tmp
+
<(pacman -Qlq | sed -e 's|/$||' | sort -u) \
echo "Loading registered files..." 1>&2
+
<(find / -not \( \
pacman -Ql | sed -e 's|.* ||' | sort -u -o /tmp/pac-list
+
-wholename '/dev' -prune -o \
+
-wholename '/etc/ssl' -prune -o \
echo "Finding local files..." 1>&2
+
-wholename '/home' -prune -o \
find / -not \( \
+
-wholename '/lost+found' -prune -o \
      -wholename '/home/*' -prune -o \
+
-wholename '/media' -prune -o \
      -wholename '/mnt/*' -prune -o \
+
-wholename '/mnt' -prune -o \
      -wholename '/tmp/*' -prune -o \
+
-wholename '/proc' -prune -o \
      -wholename '/sys/*' -prune -o \
+
-wholename '/root' -prune -o \
      -wholename '/var/*' -prune -o \
+
-wholename '/run' -prune -o \
      -wholename '/root/*' -prune -o \
+
-wholename '/sys' -prune -o \
      -wholename '/proc/*' -prune -o \
+
-wholename '/tmp' -prune -o \
      -wholename '/media/*' -prune -o \
+
-wholename '/usr/share/mime/application' -prune -o \
      -wholename '/dev/*' -prune \) \
+
-wholename '/usr/share/mime/audio' -prune -o \
      -and -type f > find-list-t
+
-wholename '/usr/share/mime/image' -prune -o \
echo "Finding local directories..." 1>&2
+
-wholename '/usr/share/mime/inode' -prune -o \
find / -not \( \
+
-wholename '/usr/share/mime/interface' -prune -o \
      -wholename '/home/*' -prune -o \
+
-wholename '/usr/share/mime/message' -prune -o \
      -wholename '/mnt/*' -prune -o \
+
-wholename '/usr/share/mime/multipart' -prune -o \
      -wholename '/tmp/*' -prune -o \
+
-wholename '/usr/share/mime/text' -prune -o \
      -wholename '/sys/*' -prune -o \
+
-wholename '/usr/share/mime/uri' -prune -o \
      -wholename '/var/*' -prune -o \
+
-wholename '/usr/share/mime/video' -prune -o \
      -wholename '/proc/*' -prune -o \
+
-wholename '/usr/share/mime/x-content' -prune -o \
      -wholename '/root/*' -prune -o \
+
-wholename '/var/abs' -prune -o \
      -wholename '/media/*' -prune -o \
+
-wholename '/var/cache' -prune -o \
      -wholename '/dev/*' -prune \) \
+
-wholename '/var/lock' -prune -o \
      -and -type d -exec echo "{}/" \; >>  find-list-t
+
-wholename '/var/lib/pacman' -prune -o \
echo "Finding local links..." 1>&2
+
-wholename '/var/run' -prune -o \
find / -not \( \
+
-wholename '/var/tmp' -prune \) | sort -u \
      -wholename '/home/*' -prune -o \
+
) | sed -e 's|^\t||;'
      -wholename '/mnt/*' -prune -o \
+
      -wholename '/tmp/*' -prune -o \
+
      -wholename '/sys/*' -prune -o \
+
      -wholename '/var/*' -prune -o \
+
      -wholename '/proc/*' -prune -o \
+
      -wholename '/root/*' -prune -o \
+
      -wholename '/dev/*' -prune \) \
+
      -and -type l >>  find-list-t
+
cat find-list-t | sort -u -o find-list
+
diff -U 1 pac-list find-list > diff-list
+
cat diff-list | grep -v '^ .*' | grep -v '^\@.*'
+
echo "Done." 1>&2
+
+
# Cleanup
+
rm -f diff-list find-list find-list-t pac-list
+
 
+
(Author's comment: well, now that I'm looking over it, it could be simplified by getting the find filters into a variable and using the variable in all the commands)
+
 
+
== Usage ==
+
 
+
The script must be run as '''root'''.
+
 
+
Typical usage & output:
+
./lostfiles > changes
+
Loading registered files...
+
Finding local files...
+
Finding local directories...
+
Finding local links...
+
Done.
+
Where:
+
* ''changes'' is a file containing a list of added/removed files
+
* "Loading registered files..." - doesn't take time, since pacman's DB is used
+
* "Finding local *..." - takes longer, since the script uses find to look for all the installed files
+
 
+
== Output ==
+
* "+" lines denote files which are not in pacman's DB
+
* "-" lines denote files which are in pacman's DB but are no longer in the filesystem
+
[some bugs in pacman ([http://bugs.archlinux.org/task/3698 bug#3698]) may create erroneous output when it comes to directories]
+
 
+
== Sample Situation ==
+
Assuming you have some application with a custom installer for which you do not know (yet) how to make a PKGBUILD. You could create a snapshot of the filesystems '''before''' and one '''after'''. Making a diff between the two snapshots will reveal the changes in the files (added/removed files) which occurred during the install.
+

Revision as of 12:10, 28 November 2011


Lostfiles is a script for detecting orphaned files (files which are not owned by any Arch Linux packages).

The script ignores by default a series of directories where packages should not install files. Some files might appear as removed if they're placed in those directories which are not checked.

Script source

#!/bin/bash

# LostFiles v0.2
# License: GPL v2.0 http://www.gnu.org/licenses/gpl.html

# Initially scripted by the Arch Linux Community
# Mircea Bardac (dev AT mircea.bardac.net)
# http://mircea.bardac.net/
# Modified by Jan Janssen

# Description:
# Search for files which are not part of installed Arch Linux packages

# Usage:
#  lostfiles > changes
# changes is a file containing a list of added/removed files

if [ $UID != "0" ]; then
       echo "You must run this script as root." 1>&2
       exit
fi
comm -3 \
	<(pacman -Qlq | sed -e 's|/$||' | sort -u) \
	<(find / -not \( \
		-wholename '/dev' -prune -o \
		-wholename '/etc/ssl' -prune -o \
		-wholename '/home' -prune -o \
		-wholename '/lost+found' -prune -o \
		-wholename '/media' -prune -o \
		-wholename '/mnt' -prune -o \
		-wholename '/proc' -prune -o \
		-wholename '/root' -prune -o \
		-wholename '/run' -prune -o \
		-wholename '/sys' -prune -o \
		-wholename '/tmp' -prune -o \
		-wholename '/usr/share/mime/application' -prune -o \
		-wholename '/usr/share/mime/audio' -prune -o \
		-wholename '/usr/share/mime/image' -prune -o \
		-wholename '/usr/share/mime/inode' -prune -o \
		-wholename '/usr/share/mime/interface' -prune -o \
		-wholename '/usr/share/mime/message' -prune -o \
		-wholename '/usr/share/mime/multipart' -prune -o \
		-wholename '/usr/share/mime/text' -prune -o \
		-wholename '/usr/share/mime/uri' -prune -o \
		-wholename '/usr/share/mime/video' -prune -o \
		-wholename '/usr/share/mime/x-content' -prune -o \
		-wholename '/var/abs' -prune -o \
		-wholename '/var/cache' -prune -o \
		-wholename '/var/lock' -prune -o \
		-wholename '/var/lib/pacman' -prune -o \
		-wholename '/var/run' -prune -o \
		-wholename '/var/tmp' -prune \) | sort -u \
	) | sed -e 's|^\t||;'