Tag Info

Hot answers tagged

23

Why do so difficult with -regex? Just do it with -name find -type f -name "[[:upper:]]*"


20

In Unix, a filename beginning with a dot, like .erlang.cookie, is considered a hidden file and is not shown by bare ls. Type ls -a to also show hidden files. From man ls: -a, --all do not ignore entries starting with . However, you can show a hidden file with ls if you specify the name: $ ls .erlang.cookie .erlang.cookie


19

Using find: find /tmp/ -type f -exec md5sum {} + | grep '^file_md5sum_to_match' If you searching through / then you can exclude /proc and /sys see following find command example : Also I had done some testing, find take more time and less CPU and RAM where ruby script is taking less time but more CPU and RAM Test Result Find [root@dc1 ~]# time find ...


17

You are telling grep to search 2 locations. grep only shows the full location if multiple locations are searched. For example touch /tmp/herp /tmp/derp cd /tmp echo "foo" > herp echo "foo" > derp Notice how if I search just 1 file, grep omits the file name grep -i "foo" /tmp/herp foo But if I specify multiple search locations, grep says where ...


17

The problem is, you didn't quote your -name parameter. Do this instead: find . -name '*.java' Explanation Without the quotes, the shell interprets *.java as a glob pattern and expands it to any file names matching the glob before passing it to find. This way, if you had, say, foo.java in the current directory, find's actual command line would be: find . ...


14

Use -prune on the directories that you're going to delete anyway to tell find not to bother trying to find files in them: find . \( -name build -o -name obj -o -name '*.so' \) -prune -exec rm -rf {} + Also note that *.so needs to be quoted as otherwise it may be expanded by the shell to the list of .so files in the current directory. The equivalent of ...


13

Never embed {} in the shell code! (by the way, some find implementations won't let you do that, and POSIX leaves the behaviour unspecified when {} is not on its own in an argument to find) find . -name accept_ra -exec sh -c 'echo 0 > "$1"' sh {} \; Or: find . -name accept_ra -exec sh -c 'for i do echo 0 > "$i"; done' sh {} + Note that the ...


12

Assuming that "foreign" means "not an ASCII character", then you can use find with a pattern to find all files not having printable ASCII characters in their names: LC_ALL=C find . -name '*[! -~]*' (The space is the first printable character listed on http://www.asciitable.com/, ~ is the last.) The hint for LC_ALL=C is required (actually, LC_CTYPE=C and ...


12

That has nothing to do with bash or sudo. The find command is using the -exec action to run the given command on each file found. In this case, the command being run is touch -d @0 If you check man touch on a GNU system, you will find that -d, --date=STRING parse STRING and use it instead of current time So, the -d is a way of choosing ...


12

A long time ago (in 7th edition, 32V, 4.2BSD, 4.3BSD), at the system-call level a zero-length pathname denoted the current working directory (when used for lookup; it was disallowed when trying to create or delete a file or directory). In System III, it was an error to use a zero-length pathname under all circumstances, and the POSIX standard has this to say ...


11

You're looking for the option -iname, which stands for "ignore case" on GNU find along with the option -type d for selecting only directories. find /mnt/md0/ -type d -maxdepth 1 -iname dcn For more a detail explanation on find switches you consult explainshells.com's explanation of find. (This will match any case: dcn , DcN, DCn) Edit 1: As state in ...


11

Script Solution #!/usr/bin/ruby -w require 'find' require 'digest/md5' file_md5sum_to_match = [ '304a5fa2727ff9e6e101696a16cb0fc5', '0ce6742445e7f4eae3d32b35159af982' ] Find.find('/') do |f| next if /(^\.|^\/proc|^\/sys)/.match(f) # skip next unless File.file?(f) begin md5sum = Digest::MD5.hexdigest(File.read(f)) ...


11

You can try to find the relevant files with find: find /usr/something -maxdepth 1 -user antoine You can then use -exec to create a zip file from the results of find: find /usr/something -maxdepth 1 -user antoine -exec zip /tmp/file.zip {} + leave out the maxdepth if you want to recurse.


10

What is costly, is doing system calls on the files (for the system calls themselves and for the I/O). Things like -type, -mtime require a lstat(2) system call on the file. -name, -path, -regex don't (though of course it will have done system calls to the directories they contain to read their content). Usually, find does an lstat() anyway (because it needs ...


9

It is the implied brackets. Add explicit brackets. \( \) find . -type f \( -name "*.htm*" -o -name "*.js*" -o -name "*.txt" \) -exec sh -c 'echo "$0"' {} \; or using xargs ( I like xargs I find it easier, but apparently not as portable). find . -type f \( -name "*.htm*" -o -name "*.js*" -o -name "*.txt" \) -print0 | xargs -0 -n1 echo


9

Well I would be tempted to do something like this instead, making the entire operation a single command. find /home/domain/imap/domain.com/ -mtime +190 \ \( -ipath '*/Maildir/new/*' -o -ipath '*/Maildir/cur/*' \) \ -delete


9

Because GNU find doesn't support \n as an escape sequence. The regexp \n matches the character n. GNU find copies the traditional Emacs syntax, which doesn't have this feature either¹. While GNU find supports other regex syntax, none support backslash-letter or backslash-octal to denote control characters. You need to include the control character literally ...


9

There are errors in your assumptions, but first some background: You should discern two uses of -exec: with \; the {} will be replaced by a single item found with + the {} will be replaced by many items (as many as the commandline can hold). Therefore your example of -exec use invokes as many cp command as items found by find. Using find ... -exec ...


8

Combining GNU find options and predicates, this command should do the job: find . -type d -empty -delete -type d restricts to directories -empty restricts to empty ones -delete removes each directory The tree is walked from the leaves without the need to specify -depth as it is implied by -delete.


8

find . -type f -name "*.htm*" -o -name "*.js*" -o -name "*.txt" is short for: find . \( \( -type f -a -name "*.htm*" \) -o \ \( -name "*.js*" \) -o \ \( -name "*.txt" \) \ \) -a -print That is, because no action predicate is specified (only conditions), a -print action is implicitly added for the files that match the ...


8

You can do the entire task using just find: $ find . -type f ! \( -perm 755 -o -perm 644 \) -printf "%m\t%p\n" Example Make all permutations of permissions (000-777). $ touch {0..7}{0..7}{0..7} $ for i in {0..7}{0..7}{0..7}; do chmod $i $i;done $ find . -type f | wc -l 512 A sample of our find command's list of files it's finding: $ find . -type f ! ...


8

Use the -perm test to find in combination with -not: find -type d -not -perm 775 -o -type f -not -perm 664 -perm 775 matches all files with permissions exactly equal to 775. -perm 664 does the same for 664. -not negates the test that follows, so it matches exactly the opposite of what it would have: in this case, all those files that don't have the ...


8

The pattern given to -name has to match the entire base filename. The behaviour of the -name pattern is defined as: The primary shall evaluate as true if the basename of the current pathname matches pattern This means it's true when the whole of the basename matches the pattern you gave. You can think of a pattern as being basically like a shell glob: ...


8

With GNU mv: find path_A -name '*AAA*' -exec mv -t path_B {} + That will use find's -exec option which replaces the {} with each find result in turn and runs the command you give it. As explained in man find: -exec command ; Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments ...


8

You can just do the whole thing with (GNU) find and sort, no need for du: $ find . -iname '*png' -printf '%s %p\n' | sort -rn 68109 ./7.png 21751 ./2.png 21751 ./1.png 5393 ./6.png 2542 ./5.png 1717 ./4.png 1003 ./3.png 878 ./10.png 793 ./9.png 587 ./8.png


7

Simply: find . ! -name '*2013*'


7

Taking advantage of GNU mv's -t option to specify the target directory, instead of relying on the last argument: find . -name "*" -maxdepth 1 -exec mv -t /home/foo2/bulk2 {} + If you were on a system without the option, you could use an intermediate shell to get the arguments in the right order (find … -exec … + doesn't support putting extra arguments ...


7

For fast search (but not definitive): locate -br ^settings.xml$ From man 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. -b, --basename Match only the base name against the specified patterns. This is ...


7

I am a little confused by wording of the question. If what you want to do is delete everything in the ./Library/Caches directory apart from a single folder called Snapshots, there is no need to use find. In bash, shell globs are the simplest way: shopt -s extglob # probably already enabled echo Library/Caches/!(Snapshots) If this prints all the ...



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