Friday, December 12, 2008

Using inode numbers to handle wacky filenames

Lately I keep on running into filenames with spaces and whacky characters at first I renamed them all but they keep on getting into the process queue. So here is how I handle them in shell scripts.

Filenames with spaces in are still evil, but we do not mind evil.

#! /bin/sh

# Code Example with limited explanation, It is self explanetory.

FILE_LIST=`ls -i1 | awk '{print $1}'`

for FILE_INODE in $FILE_LIST
do
echo -n "INODE:$FILE_INODE "

# To get the File Name
FILE_NAME=`find -inum ${FILE_INODE}`
echo "FILENAME:$FILE_NAME"

# Example of giving the filename as an argument to a program
cat "$FILE_NAME" > /dev/null

# Passing filename to program straight from find, This is handy for deleting some really obscure files. eg. files with no name.
find -inum ${FILE_INODE} -exec cat {} > /dev/null \;
done

No comments: