Tag Info

New answers tagged

9

See the man page for updatedb, "If the database already exists, its data is reused to avoid rereading directories that have not changed". Whereas the find command traverses all directories regardless of whether they have changed.


0

I'm assuming by parent folder you just mean the folder with filename.txt. You can get find to print this folder name with -printf '%h\n' instead of the -exec. You can pipe this into a shell loop or xargs for example: find /path/ -name "filename.txt" -type f -mtime -2 -printf '%h\n' | xargs -i rsync ... {} /destination \; I think you need to add -R to ...


1

What about that dollar sign in front of cmd, as in $cmd = Is the dollar sign a typo? (Sorry for not making this a comment -- no reputation points.)


2

The use of -perm +mode seems to be deprecated. Maybe the help from man find can help to solve your doubt -perm mode File's permission bits are exactly mode (octal or symbolic). Since an exact match is required, if you want to use this form for symbolic modes, you may have to specify a rather complex mode string. For example -perm ...


5

You can use -o for logical OR. Beware however that all find predicates have logical values, so you'll usually need to group ORed things together with parens. And since parens also have a meaning to the shell, you'll also need to escape them: find /some/dir -maxdepth 1 \( -name '*.c' -o -name '*.h' \) -print


2

To answer your question, you can use -o option: expr1 -o expr2 Or; expr2 is not evaluated if expr1 is true. expr1 -or expr2 Same as expr1 -o expr2, but not POSIX compliant. like this: $ find . -maxdepth 1 -name "name1" -o -name "name2" ./name1 ./name2


0

You should be able to group find patterns with -or and -and to select different patterns. I don't use such options but its $ find . \( -name foo -or -name bar -or -name buz \) -and -path '*/G/*'|9 mc ./G/buz ./G/bar ./G/foo where G = common_directory. I tested this against a file structure $ find . -name foo -or -name bar -or -name buz|9 mc ./G/buz ...


2

One way is to look for one of the file names (pick the rarest one if you know which one it is) then filter the matches to retain only those where the other file exists. find . -name foo -exec sh -c '[ -e "${0%/*}/bar" ] && [ -e "${0%/*}/buz" ] && echo "${0%/*}"' {} \; You can use the shell snippet's return code if you want to use a find ...


0

I am unaware of any find option that can do it all by itself. Using greps you can achieve that - put all the file names that you want to find in a separate file , say /tmp/files_to_find, with the following pattern In your case files_to_find would be \/foo$ \/bar$ \/buz$ Then issue the following command find root_dir_to_search_in | grep -f ...


1

So I took your hex string and printed it out to bytes, but I swapped the NULs for <spaces> (mostly because I can't figure on how to get a NUL in a grep pattern): time \ ( set x58 x5e x20 x20 xfe x5a x1e xda \ x48 x20 x20 x20 x0d x20 x03 x20 \ x07 x20 x20 x20 xcd x01 x20 x20 export ...


1

From cp man page: cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY SOURCE... You are giving parameters to cp in wrong order. Source should come before destination. Correct command is : find / -type f -perm a+r -exec cp {} /home/student/abc \; Also note that you should end -exec parameter with ;


2

Try this, fixes the cp parameter order and limits to just root filesystem rather than trying to traverse /proc and the like. find / -xdev -type f -perm a+r -exec cp {} /home/student/abc \;


4

The GNU implementation of grep (also found in most modern BSDs though the latest versions are a complete (mostly compatible) rewrite) supports a -o option to output all the matched portions. LC_ALL=C grep -ao CDA | wc -l would then count all the occurrences. LC_ALL=C grep -abo CDA to locate them with their byte offset. LC_ALL=C makes sure grep doesn't ...


0

As an alternative to the find/xargs etc you might want sha1deep. It is probably in a different package though - on my box it comes in the md5deep package. As others have said the sums.sha1 is created by the shell even before find starts. A trick with ! -name sums.sha1 to find will work, as will find -maxdepth 1 -type f -printf '%P\n' | xargs shasum | grep ...


0

As the other answers already stated the issue is that the shell opens and creates the sums.sha1 file, before executing your pipeline. You can use the program sponge which is part of the moreutils package of many distributions. In contrast to the shell redirection sponge will wait until it received everything, before opening the file. It is generally used ...


1

It's pretty good actually. Assuming the /home/data/cam*/* -maxdepth 0 part is fine, and assuming your directory names don't contain newlines, I'd do it like this: find /home/data/cam*/* -maxdepth 0 -type d -mtime +30 -exec du -sm {} + | \ awk '$1 > 3000' | cut -f 2- | xargs rm -rf -exec du -sm {} + is an optimisation, it doesn't run du for each ...


3

I'd use1 find with two -exec actions e.g.: find . -type f -exec grep -qF SOME_STRING {} \; -exec sed 'COMMAND' {} \; The second command will run only if the first one evaluates to true i.e. exit code 0 so sed will process the file in question only if the file contains SOME_STRING. It's easy to see how it works: find . -type f -exec grep -qF SOME_STRING {} ...


1

Just pipe the output of gfind to xargs: gfind /tmp/ -type f \( -name "*.h" -o -name "*.cpp" \) -exec ggrep -l "LARGE_INTEGER" {} + | xargs sed -i '1s/^/#include <stdint.h>\n/' Notice that I've removed the -P option from ggrep, since you're matching a fixed string. However this solution doesn't deal well with filenames containing newlines; a safer ...


4

with zsh: shasum -- *(D.) > sums.sha1 The glob will be expanded before the redirection is made, so the sums.sha1 will not be included if it was not there in the first place. D is to include dot-files (hidden files) as find would. . is to select only regular files (like your -type f). To exclude the sums.sha1 anyway in case it was there in the first ...


5

Since you're using -maxdepth 1, I assume you don't want recursion. If so, just do it in the shell instead: for f in ~/test/*; do shasum -- "$f" done > sums.sha1 To skip directories, you can do: for f in ~/test/*; do [ ! -d "$f" ] && shasum -- "$f" done > sums.sha1 If you do need recursion and are using bash, do: shopt -s globstar ...


11

You can prevent the file from reaching xargs using: find . -maxdepth 1 -type f ! -name sums.sha1 -printf '%P\n' | xargs -r shasum -- > sums.sha1 To prevent problems with filename that have blanks or newlines or quotes or backslashes, I would however use: find . -maxdepth 1 -type f ! -name sums.sha1 -printf '%P\0' | xargs -r0 shasum -- > ...


7

Try this: find /tmp/ -type f -name "*.h" -o -name "*.cpp" \ -exec sed -i '1s/^/#include <stdint.h>\n/' {} + Also, as correctly pointed out to me, the ! -name "*.bak" is superfluous. The -name *foo pattern only matches files ending with foo. Therefore, the *.cpp and *.h already exclude *.bak.


2

The correct and complete answer is: To modify access time only with the "touch" command, you must use "-a" parameter, otherwise the command will modify the modification time too. For example, to add 3 hours: touch -a -r test_file -d '+3 hour' test_file From man touch: Update the access and modification times of each FILE to the current time. -a ...


6

These are not part of bash; find is a standalone program and does not require bash or even a POSIX shell to run. For example, it works fine with fish, which is not POSIX compliant and does not follow all the same syntax rules as bash. You could, in fact, use it with no shell at all (e.g., in a programmatic context). This is why (if you are using a POSIX ...


4

Although both curly braces {,} and semicolons ; do have special meanings in bash, in this case it is the find command itself that is interpreting them, not the shell. The -ok command of find uses the same syntax as its -exec command, so you will find a complete description in that section of its manual page (man find): -exec command ; Execute ...


4

To improve the huge speed impact on find you could simulate something like locate alias locate="if [ ! -e /tmp/locate.db -a ! -e /tmp/locate.lockdb ] then touch /tmp/locate.lockdb trap \"rm /tmp/locate.lockdb; rm /tmp/locate.db; exit\" SIGHUP SIGINT SIGTERM find /|tee /tmp/locate.db chmod 666 /tmp/locate.db rm /tmp/locate.lockdb elif [ -e /tmp/locate.lockdb ...


3

function locate_f() { find / -path "*$1*" //Edit:path (as Gilles stated) } alias locate=locate_f


1

Try alias locate "find / | grep " That should work exactly as locate - but of course it will be much slower


1

If you just want to get rid of many files as soon as possible ls -f1 /path/to/folder/with/many/files/ | xargs rm might work okay, but better don't run it on production systems or your system will freeze sooner or later. This script works nicely for many files and should not affect the ioload of the system. #!/bin/bash ...


2

The default regex type for GNU find is emacs, which doesn't support intervals. You can specify different regex types, such as posix-egrep, which will solve your issue: find . -maxdepth 1 -type d -regextype posix-egrep -regex './[0-9]{6}'


2

Personally I think this just cries out for perl, special character safe and rename is a system call not a fork exec. find . -name "*\?*.mp4" -print0|perl -n0e 'next if /\?.*\//;$o=$_;s!\?!_!g;next if -e;rename $o, $_' Ignores files in directories with ? in their name to prevent undefined behavior and checks for rename conflicts. See below for a detailed ...


4

Don't use find for this - whichever way you go with find you'll need something like a single mv per file. That's a lot of processes, and to no benefit. This is not to mention that it is simply more difficult to do that way. That's my opinion, anyway. I would use a stream or a batch tool, and tend to prefer pax: cd -P . && mkdir ../newmp4 ...


1

With zsh: autoload zmv # best in ~/.zshrc zmv '(**/)(*\?*.mp4)' '$1${2//\?/_}' That excludes hidden ones and doesn't look inside hidden directories. It will rename those files regardless of their type (regular, directories, symlink...). If you also want to consider hidden files/dirs, only rename mp4 files if they're regular files, and be ...


0

The easiest way that I've found to do it is to pipe into something else to generate commands, like sed, then execute the commands in sh. find . -name '*\?*.mp4' -print | sed 's/.*/"&";h;y/?/_/;x;G;s/\n/ /;s/^/mv /' | sh -s The find is pretty self-explanatory, as is the sh. The sed command may take a little explanation: s/.*/"&"/ surrounds the ...


1

You can use prename with find: find . -type f -name '*\?*.mp4' -exec prename -n 's:(.*/[^?]*)\?(.*$):$1_$2:' {} + prename -n will show the files that will be renamed, if you are satisfied with that run the command with removing -n (i.e. prename 's/\?/_/').


2

You could use find with a timestamp flag so that it picks up all files since the last run, regardless of how long ago that was. Feel free to replace the definition of $trigger with something more appropriate. trigger=/tmp/trigger touch "$trigger.new" test ! -f "$trigger" && touch "$trigger" find /a/b/c -newer "$trigger" -type f -exec cp -f {} ...


2

You try to use a regex in your find command. To use a regex, you need the parameter -regex or you could use awk and find to find all files that started with lib. find /usr/lib -type f | awk -F'/' '$NF ~ /^lib/' Example $ find /usr/lib -type f | awk -F'/' '$NF ~ /^lib/' /usr/lib/mate-settings-daemon/libhousekeeping.so ...


1

If you are trying to find out if a particular library is installed, you can also use rpm rpm -qa | grep lib The installed packages usually are in /usr/lib or /usr/lib64 (for 64 bit version). So you can also do ls /usr/lib | grep lib Or the find utility you were using find /usr/lib -name "lib*"


1

Assuming you want to replace the text as in you example, you can do: sed '/Name "Test"/ {n;n;s/Precision/Try/;}' To do it insensitive of case try: sed '/Name "[Tt][Ee][Ss][Tt]"/ {n;n;s/[Pp][Rr][Ee][Cc][Ii][Ss][Ii][Oo][Nn]/Try/;}' Or more simply with GNU sed: sed '/Name "Test"/I {n;n;s/precision/Try/i;}'


1

If you'd like to avoid "permission denied" errors and search recursively the whole Android filesystem, you'll need to have a "rooted" device. Afterwards, having a rooted device, you may install any Terminal emulator application, run su and run find command. E.g., you'd like to find all files with .ko extension, so please run: find / -name "*.ko" in ...


4

Use find -exec for recursive touch, with command line args for dirs to process. #!/bin/sh for i in "$@"; do find "$i" -type f -exec touch -r {} -d '+3 hour' {} \; done You can run it like this: ./script.sh /path/to/dir1 /path/to/dir2


2

In the shell, you need to distinguish filename generation/expansion (aka globbing): a pattern that expands to a list of files from pattern matching. globbing uses pattern matching internally, but it's really before all an operator to generate a list of files based on a pattern. */*.txt is a pattern which matches a sequence of 0 or more characters followed ...


8

The basic format of find is find WHERE WHAT So, in find *, the * is taken as the WHERE. Now, * is a wildcard. It matches everything in the current directory (except, by default, files/directories starting with a .). The Windows equivalent is *.*. This means that * is expanded to all files and directories in your current directory before it is passed to ...


0

First of all kept in mind the syntax of find:- find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression] What is the difference between find * and find ~ for searching a file? * is while card which matches everything. Here find * passes the list of files/dirs in the current directory and those as target names to search for. so, it will ...


1

The man page for GNU find says: -perm /mode Any of the permission bits mode are set for the file. ... and -perm +mode Deprecated, old way of searching for files with any of the permission bits in mode set. You should use -perm /mode instead. Trying to use the ‘+’ syntax with symbolic modes will yield surprising results. ... and gives ...


2

The find manual page explains: -perm +mode Deprecated, old way of searching for files with any of the per‐ mission bits in mode set. You should use -perm /mode instead. Trying to use the `+' syntax with symbolic modes will yield sur‐ prising results. […] So yes, they're the same thing, but you should use ...


0

The ownership of symbolic links don't matter. Its the referenced entitiy which does. That said, use find -l to discover symbolic links in a directory tree. Use chown -h and/or chmod -h to operate on the symbolic link. find . -type l -exec chown -h root:root {} +


2

The trick to understand a find command is to recursively evaluate and group into the resulting logic value's appropriate expression (-true or -false) the first two expressions / actions, considering that the evaluation of each pair of expressions / actions is short-circuited (hence expressions / actions as the second operand of an AND comparison will not be ...


7

If you understand the && and || operators in the shell (and also in C, C++, and derivative languages), then you understand -a and -o in find. To refresh your memory: In the shell, command1 && command2 runs command1, and, if it (command1) succeeds, it (the shell) runs command2. command1 || command2 runs command1, and, if it (command1) fails, ...


5

The order of find arguments matters. The command needs to be constructed as find -type d -print0 and then it will work as expected. I just thought I'd post this in case it's helpful to anyone.



Top 50 recent answers are included