Tag Info

Hot answers tagged

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


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

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


6

Let us suppose that we have file1 in the current directory. Then: $ find . -maxdepth 0 -name "file1" $ find . file1 -maxdepth 0 -name "file1" file1 Now, let's look at what the documentation states: -maxdepth 0 means only apply the tests and actions to the command line arguments. In my first example above, only the directory . is listed on the ...


5

That is because your -exe action is tied to the -name "*.h" put parenthesis around the expression and it will work. the default action will -print that is why the initial expression worked. find . \( -name '*.cpp' -or -name '*.h' \) -exec echo '{}' \; Also for efficiency if you use | xargs instead of -exec it is a LOT faster with a large result set as it ...


5

Use find ... -print0 | while IFS= read -d '' construct: find ${POLLDIR} -type f -mmin +1 -print0 | while IFS= read -r -d '' -r sendfile; do echo "${sendfile}" ls -l "${sendfile}" and-so-on if success_above then mv "${sendfile}" "${donedir}/." fi done The -d '' sets the end of line character to \0 which is what separates each file found by ...


4

In general, the empty string does not denote the current directory, neither to shell commands nor in system calls. It did on some older systems, but not on POSIX-compliant systems. Occasionally you'll find a program which uses the current directory when you pass an empty string and the program expects a directory name. This is sometimes deliberate, and ...


4

You can use: find * -name "*.h" Note that files in the current directory whose name starts with . will be omitted, and files whose name starts with - will be interpreted as options by find and cause havoc, so this is not a general equivalent to find . …. The absence of of a string ending in "/" as part of a file name implies the current directory, but ...


4

The solution that might work for your case is, find . -type d '!' -exec test -e "{}/utilities.py" ';' -print Testing I created 4 sub directories named dir1, dir2, dir3 and dir with spaces. I wanted to test if this handles spaces equally well which is why I created a directory with spaces in its name. I created files file1, file2, file3 and file4 in ...


3

Here's a way to do what you want with a single file (which we'll call $file for now) and print it to standard output # prepend a "# " and remove the .markdown from the filename sed 's/\.markdown//' <<< "# $file" # print a blank line echo # output the file cat "$file" Now for what you really wanted, enclose that in a for loop to iterate over every ...


3

You could use a combination of find, du and sort like the following: find <directory> -iname "*.png" -type f -print0 | xargs -0 -n1 du -b | sort -n -r This searches for all regular files in <directory> ending with .png (case-insensitive). The result is then passed to xargs which calls du with each single file, getting its size in bytes (due to ...


3

Use while loop together with read -d '' -r: find /home/family/Music -name '*.m4a' -print0 | while read -d '' -r file; do echo "$file" done


3

Put a* and d* under quotes, so that shell would not expand them,and also add -name keyword. If you only want to search for files and not also directories for example then add -type f. find . -name 'a*' -type f -exec chmod o+r {} \; find . -name '*d' -type f -exec chmod o+x {} \; If you want to change only in current directory and not subdirectories, add ...


3

You can use find . -maxdepth 1 -mmin -$((60*5)) -type f to list all regular files in the current directory, which were changed during the last 5 hours. $((60*5)) is calculated by the shell and therefore equal to 300 min.


3

If you have spaces in file names then you need to use print0 option for file, later -0 for xargs, and lastly -I {} for second xargs. find . -iname "*.maxpat" -print0 | xargs -0 grep -l "mystring" | xargs -I '{}' open '{}' Tested with emacs as an open command.


2

I cannot think of any example where the empty string denotes the current directory .. You may be thinking of invocations like ls, but that is because ls assumes the current directory if no parameter is given, and in fact it won't take the empty string: ulmi@silberfisch:~$ ls "" ls: cannot access : No such file or directory


2

Using GNU find, you can search for all directories and files that belong to groupX: find / -group groupX From man find: -group gname File belongs to group gname (numeric group ID allowed).


2

You can move the external loop into an "internal" loop and obviate word-splitting issues like so: find /home/family/Music -name '*.m4a' -exec \ sh -c 'for i; do ffmpeg -i "$i" -acodec libvorbis -aq 6 -vn -ac 2 "$i.ogg"; done' sh {} + Notice that -print0 is no longer needed Additionally, you need to quote *.m4a to avoid accidental glob expansion by shell


2

find can take many dirs as arguments: find dir1 dir2 dir3 -name pattern so find patern dir just lists everything inside two directories: pattern and dir.


1

You wrote: It surprises me to see find iterate/walk through the complete filesystem when I do a simple find -inum 12345 find, by definition, does a tree walk starting at the given directory or directories, with a default starting directory of .. find -inum 12345 will walk through the entire directory tree starting with the current working directory. ...


1

The very simple reason is that at least for the ext2/ext3/ext4 type filesystems the filenames are stored via the directory entries data stored in directory type files. This means that those files that are from type directory have a more or less intricate system to store filenames (of the files inside of the directory) and the inodes which lead to the data ...


1

Well, just don't set the same permissions on the directory and the files within it: $ chmod g+rx directory/ $ chmod g= directory/* Here, the group members can enter and browse the directory, yet they won't be able to read the files within it. Edit: regarding your new title, I would suggest: $ chmod a+rx directory/ $ chmod u=rwX,g=rX,o= *


1

For future reference - you can use install to do this directly: install -D ./2013/01/10/IMG_0141.JPG ../Archiv/Bilder/2013/01/10/IMG_0141.JPG Note: you need to append the file path in the second argument for this to work. In other words: Incorrect: find . -ctime +365 -exec install -D '{}' ../Archiv/Bilder/ \; Correct: find . -ctime +365 -exec install ...


1

Your find command runs into the problem that the intermediate directories (in this case /home/Bruno/Archiv/Bilder/2013/01/23/) has not been created yet. That has nothing to do with the (harmless) '.' in your path. You either have to first create the whole directory structure to the target or make a small script that you call instead of mv that first creates ...


1

-exec takes the exit status of the command you put in it and uses it logicially within find So, just something simple like this should work find . -iname "*.ext" -exec grep -q "mystring" {} \; -exec open {} \;


1

And just in case you need to have the most compatible version, you can do it with regular find and sort (no reliance on gnu extensions, such as printf): find . -ls | grep -i -e '\.png$' -e '\.jpe*g' | sort -k7,7nr -k7,7 : Sort on columns 7 to 7 (ie, only 7) (... this should be a parameter, allowing one to change the column number in case on their old ...


1

grep -H will print file names for you, but since you're doing multiple greps you can just echo the file name yourself: for f in $(find . -name '20140926.log'); do echo -n "$f " grep 123456 $f | grep -c 'food="100"' # grep -c prints count of matches done


1

You have two syntax error: type f must be -type f. You need a space before \). So the fixed command: find . -type f \( -iname "SS1*.tar" -o -iname "SS2*.tar" -o -iname "SS3*.tar" -o -iname "SS4*.tar" \) -exec cp {} /backup_file/backup/weekly \;


1

Simpler: find . -type f -iname "SS[1234]*.tar" -exec cp {} /backup_file/backup/weekly \;


1

I'm not sure it can be done with a single find command, but it can certainly be done with a simple bash script. I've just tested this on Cygwin and it works as intended. You'll want to change ROOT_DIR to be the root of whatever filesystem you want to perform this search on (for example /cygdrive/d/ to search the D drive). The general idea is this: find all ...



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