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


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

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


15

My server had been infected and the attacker spread (...) malicious code (...). NUKE IT FROM ORBIT! That's the only way to be sure that everything is secure again. If you want you could make an image an analyze how the attacker got in the system but for that system the only salvation is reinstall from scratch. No removing only the "dot files" will make ...


14

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


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

The second example: find . -name '*.txt' -print0 | xargs -0 cat > out.txt Is completely legal and will recreate the file, out.txt each time it's run, while the first will concatenate to out.txt if it runs. But both commands are doing essentially the same thing. What's confusing the issue is the xargs -0 cat. People think that the redirect to out.txt ...


10

Just swap \0 and \n: find ... -print0 | tr '\0\n' '\n\0' | head | tr '\0\n' '\n\0' Note that some head implementations can't cope with NUL characters (and they're not required to by POSIX), but where find supports -print0, head and text utilities generally support NUL characters. You can also use a function to wrap any command between the two trs: ...


10

From man find: -exec command {} + This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that ...


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

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


10

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


9

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


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

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


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

The backticked expression: echo {} | tr mkv m4v (which is not what you want, for a variety of reasons; see below) is expanded once, when the find command is parsed. If you're using bash, it will normally be expanded to {}: $ echo {} | tr mkv m4v {} And, indeed, that happens on every shell installed on my machine except fish, which outputs an empty line. ...


8

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 better still find . -type f -name "*.htm*" -o -name "*.js*" -o -name "*.txt" -print0 | xargs -0 -n1 echo


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


7

That would happen if /var/lib/tomcat7/conf is a symbolic link to /etc/tomcat7. By default, find (the coreutils version anyway) will not follow symlinks. Try with the -L flag: find -L / -name "server.xml" -print


7

Believe it or not this is extensively documented in the find man page. On the CentOS and Ubuntu systems I have to hand there are even examples of this. -perm /mode Any of the permission bits mode are set for the file. Symbolic modes are accepted in this form. You must specify u',g' or `o' if you use a symbolic mode. See the ...


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

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.


7

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" \) \ \) -print That is, because no action predicate is specified (only conditions), a -print action is implicitly added for the files that match the conditions. ...


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


6

"Don't use ls in scripts" is a problem with POSIX ls "only"; for GNU ls see --quoting-style=. GNU sort solves the problem with --zero-terminated. If it must be compatible then you could use find ... -exec for passing one file name at a time to a script which does the escaping. If at least bash is available: start cmd:> testfunc () { echo ...



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