Hot answers tagged

58

You could concoct something with -printf, but the easiest is just to tack on -print on the end. This will show what was successfully deleted.


40

You're almost right. -mtime 365 will be all files that are exactly 365 days old. You want the ones that are 365 days old or more, which means adding a + before the number like this -mtime +365. You may also be interested in the -maxdepth 1 flag, which prevents you from moving items in sub directories. If you want to be sure that you are only moving files, ...


29

You can use the -printf command line option with %f to print just the filename without any directory information find . -type f -mtime -14 -printf '%f\n' > deploy.txt or you can use sed to just remove the ./ find . -type f -mtime -14 | sed 's|^./||' >deploy.txt


28

Use find ... -print0 | xargs -0 ... e.g. find /path/to -name "*.html" -print0 | xargs -0 grep -l "rumpus" from the find man page -print0 True; print the full file name on the standard output, followed by a null character (instead of the newline character that ‘-print’ uses). This allows file names that contain ...


28

with this trick you can see the current folder - but no progress bar - sorry. watch readlink -f /proc/$(pidof find)/cwd


24

Not directly related to question, but might be interesting for some that stumble here. find command doesn't directly support -older parameter for finding files older than some required date, but you can use negate statement (using accepted answer example): touch -t 201003160120 some_file find . ! -newer some_file will return files older than provided date....


22

Use the du with its -A flag: root@pg78:/usr/local/pgsql/data/base/218204 # du -A -h 221350.219 1.0G 221350.219 root@pg78:/usr/local/pgsql/data/base/218204 # du -h 221350.219 501M 221350.219 Very handy. It even works with -d for recursive goodness: root@pg78:/usr/local/pgsql/data/base # du -h -c -d0 . 387G . 387G total root@pg78:/usr/local/...


21

According to man 7 capabilities CAP_DAC_READ_SEARCH * Bypass file read permission checks and directory read and execute permission checks; * Invoke open_by_handle_at(2). This worked for me. (lines beginning with '#' are root, those with '$' are non-root) in this case the non-root user is in the wheel group. # cp /usr/bin/find /usr/...


18

GNU find's manpage says that all POSIX finds are supposed to detect filesystem loops and emit error messages in these cases, and I have tested find . -follow -printf "" on GNU find, which was able to find loops of the form ./a -> ./b and ./b -> ./a printing the error find: `./a': Too many levels of symbolic links find: `./b': Too many levels of ...


18

You can you -maxdepth and -mindepth with your modified/accessed/changed attribute search of choice, i.e. find -maxdepth 1 -mtime 4 for 4 days. Don't forget to exclude the . and .. results that find returns. Useful link to many find examples.


18

What about locate? locate reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to standard output, one per line. If --regex is not specified, PATTERNs can contain globbing characters. If any PATTERN contains no globbing characters, locate behaves as if the pattern were PATTERN. By ...


17

How about just using rm -vf for verbose rm output. $ touch file1 file2 file3 $ find . -name "file?" -exec rm -vf {} \; removed `./file2' removed `./file3' removed `./file1'


17

You're on the right track -- you just need to quote the pattern so that it gets interpreted by find and not by your shell: sudo find / -type f -name '*.pem'


15

You do not need to use xargs, because find can execute commands itself. When doing this, you do do not have to worry about the shell interpreting characters in the name. find /path/to -name "*.html" -exec grep -l "rumpus" '{}' + from the find man page -exec command {} + This variant of the -exec action runs the specified command on the selected ...


14

Looking at strace -eopen find . -type f with GNU find (4.4.2 from Debian Squeeze) the answer appears to be "no, find does not open files", but it does open directories: open("details", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 5 open("..", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = 5 open("..", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|...


13

You can use any of the forms: find . -size +2M -exec rm {} + find . -size +2M -exec rm {} \; The semicolon should be escaped!


13

find /my/dir -type f | while read filename ; do fuser -s $filename || echo $filename ; done This uses find just to generate a list of files. Then the while loop iterates over the list of results, for each result it runs fuser -s $filename which will exit with success if something is using the file. The || echo $filename part means "if fuser failed (...


13

Also: find -name "*.avi" -exec ./myscript '{}' \; Womble's answer definitely works in the majority of cases, but Xargs isn't perfect with spaces across all implementations. I've never found find's 'exec' mode to break. Solaris was particularly cranky in that respect. Obviously, the '-print' would be better for simply generating output for a list of files ...


13

It looks like your /dev/null may have been deleted at some point so when you started writing to it you are writing to a plain file rather then the character special null device. You an confirm this by looking at the output of ls $ ls -l /dev/null crw-rw-rw-. 1 root root 1, 3 Sep 28 08:11 /dev/null If I'm right then you won't see a character special device....


11

Simplest solution that doesn't print, but exits 0 when results found find /tmp -name something | grep -q "."


11

You could install the GNU version of du(1): cd /usr/ports/sysutils/coreutils && make install clean Then you can use the --apparent-size flag: /space# zfs create space/comptest /space# zfs set compression=on space/comptest /space# dd if=/dev/zero of=/space/comptest/zerofile bs=1M count=40 /space/comptest# gdu 2K . /space/comptest# gdu --...


11

Do man xargs and look at the -I flag. find . -type f -exec grep -ilR "MY PATTERN" {} \; | xargs -I % cp % /dest/dir/ Also, find expects a \; or + after the -exec flag.


10

AFAIK, Linux doesn't record the creation time, so the short answer is you cannot. For the modification time, try this: $ find /docs -type f -printf '%T@ %p\n' | sort -k1 -n or: $ find /docs -type f -print0 | xargs -0 stat -c "%y %n" | sort


10

The ./ should be harmless. Most programs will treat /foo/bar and /foo/./bar as equivalent. I realize it doesn't look very nice, but based on what you've posted, I see no reason why it would cause your script to fail. If you really want to strip it off, sed is probably the cleanest way: find . -type d -mtime 14 | sed -e 's,^\./,,' > deploy.txt If you're ...


10

In PowerShell run: $fw=New-object -comObject HNetCfg.FwPolicy2 $fw.rules | findstr /i "whaturlookingfor" better yet: $fw.rules | select name | select-string "sql"


9

-exec rm {} \; you can use.. man find -exec command ; Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `{}' is replaced by the current file name being processed everywhere it ...


9

find /folder -type -f -uid 0 will find all files under the directory owned by root, -user root would also work, naturally. If all files below /folder should be owned by the same user/group, you could simply do an recursive chown: chown -R user:group /folder


8

Try removing the escaped double quotes. I believe rm thinks those are part of the filename. find ./ -inum 167794 -exec rm {} \;


8

If find and xarg versions on your system doesn't support -print0 and -0 switches (for example AIX find and xargs) you can use this: find /your/path -name "*.html" | sed 's/ /\\ /g' | xargs grep -l "rumpus" Here sed will take care of escaping the spaces for xargs.


Only top voted, non community-wiki answers of a minimum length are eligible