Tag Info

Hot answers tagged

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 ...


16

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 . ...


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 ...


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

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 ...


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

all versions of find that I know will match underscores with wildcards. be warned that when doing. find . -name *.jks -print 2>/dev/null the "*.jks" might get expanded by the shell, before running the find command. e.g. $ mkdir foo $ touch a.jks foo/a.jks foo/b.jks a.jks $ find . -name *.jks -print ./a.jks ./foo/a.jks this is really because you ...


10

Assuming I understood your question, you are possibly overcomplicating it. This should do find your_directory -type f -name '[az]*[az]' This omits files whose name is a single letter a or z. If you also want to include them, you need to specify another pattern: the name must match either [az]*[az] or [az]. find your_directory -type f \( -name '[az]*[az]' ...


10

find(1) is powerful enough to do what you need. Simply collect all of the conforming names into an expression using parentheses, then negate it to show non-conforming file names. For example, to show all files not named *.txt, *.bz2, or *.zip: $ find . \! \( -name \*.txt -o -name \*.bz2 -o -name \*.zip \) You can use -not instead of \! with GNU and BSD ...


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

If you're using bash, please consider doing it the bash way. Your array would by obtained thus (no external find command): params=( *${file_name}*.trg ) and then you loop through the array like so: for file in "${params[@]}"; do echo "I'm happily reading the beautiful file $file" done If you want to remove the .trg extension and replace it with ...


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 ...


8

I think the answer is in your question. Other commands don't use {} as a placeholder. That way you can still use find's -exec option without having to worry about a bunch of nonsense to escape or work around the fact that a command uses {} just like find does.


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: ...


7

Combining GNU find options and predicates, this command should do the job: find . -depth -type d -empty -delete -depth to process the tree from the leaves -type d restricts to directories -empty restricts to empty ones -delete removes each directory


7

Well, the simple answer is, I guess, that your find implementation is following the POSIX/SuS standard, which says it must behave this way. Quoting from SUSv4/IEEE Std 1003.1, 2013 Edition, "find": -mtime n      The primary shall evaluate as true if the file modification time subtracted      from the initialization time, divided by 86400 (with any ...


7

So if a file is 1 day, 23 hours, 59 minutes, and 59 seconds old, find -mtime +1 ignores all that and just treats it like it's 1 day, 0 hours, 0 minutes, and 0 seconds old? Yes. Like man find says, "any fractional part is ignored". If you divide "1 day, 23 hours, 59 minutes, and 59 seconds" through "24 hours", you may get 1.9999, but the .9999 part is ...


7

find . -type f -name '*.php' -print0 | tee list | tr \\0 \\n xargs -r0 rm -f < list That's assuming you want to see the list before deciding to delete them. If not, you can simply do: find . -name '*.php' -type f -print -delete (note that -print0, -delete, -r, -0 are not standard but supported by the GNU implementation) Also beware that while find ...


7

For ls, use -A instead of -a. from man ls: -A, --almost-all do not list implied . and ..


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 ...



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