Tag Info

Hot answers tagged

8

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


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

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


4

Using awk is not so complicated: find . -name '*.c' -type f | awk -F/ '{print $NF}' Or sed: find . -name '*.c' -type f | sed 's|.*/||' (assuming file names don't contain newline characters) and that will work with any find implementation.


4

What is the purpose of having these two operators then? That's an easy one: Because there are different use cases. Sometimes it is useful to truncate the target file to size 0 first, sometimes (e.g. log files) it makes more sense to append data to a file. In this case it makes no sense to append. You want a file with exactly the content of the files ...


4

It seems that the problem is \( is interpreted by the shell first. For example, you got a file in the current directory named: foo(.test Then ls foo\(.text and ls "foo(.text" both work. So, the following works: find . -regex ".*submit\(Web\)?\.m" EDIT: Single quote also works here. Difference between single quote and double quote, see ...


4

Gah. [jake@jace]/bin% ls -lhd /bin lrwxrwxrwx. 1 root root 7 May 22 2012 /bin -> usr/bin/ I'm running Fedora 17. Apparently /bin is symlinked to /usr/bin. And of course (and quite rightly) find and locate ignore symlinked directories to avoid result pollution.


2

chmod -R o-w . Will remove write permissions to others for every file in a safe way. It will however update the ctime of every file including the ones for which others already didn't have write access. With GNU chmod, you can make it show which files needed updated with the -c option: $ chmod -cR o-w . mode of `./a' changed from 0777 (rwxrwxrwx) to 0775 ...


2

You can't really influence environment "horizontally" in the pipe - the processes in a ... p_n | p_n+1 | p_n+2 ... pipe are spawned by the same shell interpreter, hence there is no way to change environment variable of say p_n from p_m, echii is in the same pipeline. If you just need to do a simple transformation of the filenames, which can be achieved with ...


2

First, why your attempt doesn't work: -printf "%h\n" prints the directory part of the .avi file name. That doesn't affect anything in the subsequent -exec action — {} doesn't mean “whatever the last printf command printed”, it means “the path to the found file”. If you want to use the directory part of the file name in that cp command, you need to modify ...


1

I would go with the second one. The redirect of stdout gets caught by bash when you hit enter, so it's not like you create a new redirect for every line of find/xargs (which might have been their thinking). If out.txt doesn't exist they should be identical, if it already has data, then the second one at least resets the file to known content (that is, no ...


1

Since you seem to be using GNU tools, you could do: find . -name '*.avi' -printf '%h\0' | tr '\1/' '/\1' | LC_ALL=C sort -zu | tr '\1/' '/\1' | awk -vRS='\0' -vORS='\0' ' NR>1 && substr($0, 1, length(l)) == l {next} {print; l=$0"/"}' | xargs -r0 cp -rt /share/USBDisk1/Movies/ The above is GNU specific: for find (because of ...


1

Your approach is a performance nightmare: You create two processes for every file! One completely uselessly because find already has this information and can easily print it. This is a better solution: find . -perm -o=rwx -printf "%m %p_\0" 2>/dev/null | while read -r -d '' perms path; do path="${path%_}" echo "${perms} '${path}'" >&2 ...


1

/007 will show your only files that have no permissions for owner and group, and all permissiosn (rwx) for other. You might have more luck with /o=rwx. That will match only the other permissions for the file. EDIT FOR CORRECTNESS: Apparently, you'll need to use -perm -o=rwx, because the /o is an inclusive filter, and would match files where other has ...


1

find -L . \ \( -type d \ \( -path "*/ignore" -o -path "*/indeed" -o \ \( -path "*/subdir/*" ! -path "*/subdir/save" \ \) \ \) \ \) \ -prune -o -print If you want extra filter (like -name "*.ext") you have to put that right before -print. The last part then looks like this -prune -o \( -name "*.ext" \) -print Note that I ...


1

With GNU find (i.e. under non-embedded Linux or Cygwin), you can use -regex to combine all these -path wildcards into a single regex. find . -regextype posix-extended \ -type d -regex '\./(\..*|Music|Documents)' -prune -o \ -type f -regex '.*(\.(bck|bak|backup)|~)' -print0 | xargs -0 --no-run-if-empty trash-put With FreeBSD or OSX, use -E ...


1

As far as I know, there is no option to tell find to read patterns from a file. An easy workaround is to save the patterns I want to exclude in a file and pass that file as input for a reverse grep. As an example, I have created the following files and directories: $ tree -a . ├── a ├── .aa ├── .aa.bak ├── a.bck ├── b ├── .dir1 │   └── bb1.bak ├── dir2 │ ...



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