Showing posts with label Find. Show all posts
Showing posts with label Find. Show all posts

Saturday, October 18, 2014

Find files owned or not owned by a user

Find all files owned by joe.baloney
$ find .  -user joe.baloney -print
Find files NOT owned by joe
find . \! -user tward -print

Sunday, January 13, 2013

Piping "find" results through xargs

Piping the results of find using xargs doesn't typically work exactly how I want. If a file or folder has a space in its name xargs doesn't handle it correctly.
From the xargs man page:
Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic filenames containing blanks and/or newlines are incorrectly processed by xargs. In these situations it is better to use the -0 option, which prevents such problems. When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator. If that program is GNU find for example, the -print0 option does this for you.
So, long story short: add "-print0" to find, and "-0" to xargs:

find . -name "*txt" -print0 | xargs -0 grep -i poisson

Thursday, December 6, 2012

Find and delete empty files/folders

Find can find empty files and/or folders and even remove them (scary).

# Find all empty files and/or folders recursively
find -depth  -empty 
# Find all empty folders, only recurse two levels  
find -maxdepth 2 -depth  -empty -type d 
# Delete what you find 
find -maxdepth 2 -depth  -empty -type d  -delete

Monday, November 26, 2012

Sorting "find" results by date

I have been meaning to look this up for a while now. Sometimes I save a file, but I'm not sure in which folder. I use "ls -ltrh" all the time to sort contents of a folder by reverse time so the most recently modified items appear at the bottom (very useful example is ls -ltrh /var/log to find most recently modified log files).

The following command will use the results of  the find command to do the same thing over multiple folders. Its too complex to type all the time, so I put it in a script in my personal script folder. Another good idea would be to alias it.


    find . -type f -printf '%T@ %p\n' | sort -k 1nr | sed 's/^[^ ]* //' | head -n 10

This example shows only the 10 most recent files (head -n 10) and doesn't filter filenames in any way. It also only examines normal files, not folders or other.

Friday, January 27, 2012

Using -exec instead of xargs

Using xargs to pipe results of find into grep has problems if find returns files/folders with spaces in the name.

find ./ -name "*html" | xargs grep "something"


Ends up giving me lots of "file doesn't exist" errors.

Using exec circumvents this problem, although using exec is apparently a less efficient way of doing things.

find ./ -name "*html" -exec grep -H "something" "{}" \;

-H tells grep to always show the filename, otherwise you only get the match.
"{}" represents the filename returned by find.
\; I'm not sure what this is, but it has to be there...

Update:
I describe a slightly simpler solution in this posting.