Tag Info

Hot answers tagged

16

I normally use this style of command to run grep over a number of files: find / -xdev -type f -print0 | xargs -0 grep -H "800x600" What this actually does is make a list of every file on the system, and then for each file, execute grep with the given arguments and the name of each file. The -xdev argument tells find that it must ignore other filesystems ...


14

Actually it doesn't search anywhere. It waits for input from standard input. Try this: beast:~ viroos$ grep foo when you type line containing "foo" and hit enter this line will be repeated otherwise cursor will be moved to new line but grep won't print anything.


12

find -iname '*.xml' Otherwise, your shell expands *.xml to XYZ.xml, and the command that actually gets executed is find -iname XYZ.xml The reason it works if there are no XML files in the current directory is that shells generally leave wildcards unexpanded if they don't match anything. In general, any time you want wildcards to be expanded by a ...


9

This will list all the PDFs: $ find dir/ -name '*.pdf' ./dir/subdir2/subsubdir1/document.pdf ./dir/subdir3/another-document.pdf You can pipe that to xargs to get it as a single space-delimited line, and feed that to tar to create the archive: $ find dir/ -name '*.pdf' | xargs tar czf dir.tar.gz (This way omits the empty directories)


9

find -maxdepth 1 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" | wc -l; done Thanks to Gilles and xenoterracide for safety/compatability fixes. The first part: find -maxdepth 1 -type d will return a list of all directories in the current working directory. This is piped to... The second part: while read -r dir; do begins a while loop ...


9

Generally speaking, when you're looking for files in a directory and its subdirectories recursively, use find. The easiest way to specify a date range with find is to create files at the boundaries of the range and use the -newer predicate. touch -t 201112220000 start touch -t 201112240000 stop find . -newer start \! -newer stop


8

You want find(1). This will do exactly what you want. You can also specify various filter conditions such as file type (don't include directories), newer than the time stamp on a given file etc. The man page will describe these in more detail. Also, take a look at the -exec option; you may be able to use this instead of iterating over the output.


8

For cp, the destination is the last argument on the command line. You have specified 2/g as the last argument. Before cp is executed, the command parameters are expanded. 1/* expands to 1/a 1/b 1/c. 2/* expands to 2/f 2/g. The final executed command is cp -r 1/a 1/b 1/c 2/f 2/g, which will copy all the arguments (except the last one) to 2/g. If you are ...


8

This will delete all the files with a name ending in .swp, ~, .netrwhist, .log or .bak anywhere under your home directory. No prompt, no confirmation, no recovery, the files are gone forever. find ~ -type f \( -name '*.swp' -o -name '*~' -o -name '*.bak' -o -name '.netrwhist' \) -delete (I purposefully omit *.log because it sounds dangerous, this is not a ...


7

find is very useful for selectively performing actions on a whole tree. find . -type f -name ".Apple*" -delete Here, the -type f makes sure it's a file, not a directory, and may not be exactly what you want since it will also skip symlinks, sockets and other things. You can use ! -type d, which literally means not directories, but then you might also ...


7

Recursive means that cp copies the contents of directories, and if a directory has subdirectories they are copied (recursively) too. Without -R, the cp command skips directories. -r is identical with -R on Linux, it differs in some edge cases on some other unix variants. By default, cp creates a new file which has the same content as the old file, and the ...


7

I rolled this script that does a recursive pattern search from the current directory. It uses busybox's sh and sed. Tested with busybox 1.17.1; your mileage may vary on 1.00. #!/bin/busybox sh sed="busybox sed" search_in() { searchterm="$1" searchdir="$2" prefix="$3" ( cd "$searchdir" for file in * do ...


7

You can do this with GNU find and GNU mv: find /dir1 -mindepth 2 -type f -exec mv -t /dir1 -i '{}' + Basically, the way that works if that find goes through the entire directory tree and for each file (-type f) that is not in the top-level directory (-mindepth 2), it runs a mv to move it to the directory you want (-exec mv … +). The -t argument to mv lets ...


6

You should check out the billiant little grep/find replacement known as ack. It is specifically setup for searching through directories of source code files. Your command would look like this: ack --csharp GetTypes


6

Assuming you don't need precision to the seconds, this should work. find . -type f -mmin -$(((`date +%s`-`date -d 20111222 +"%s"`)/60)) \! -mmin +$(((`date +%s`-`date -d 20111224 +"%s"`)/60)) EDIT: Changed cmin to mmin after @Eelvex's comment. EDIT: '\!' missing


6

Update: fixed a typo in the script: changed print $NF to print $3; also tidied things up, and added some comments. Assuming file names do not contain \n, the following prints out a sorted list which breaks (as in: section control breaks) at unique file name, unique md5sum, and shows the corresponding group of file paths. #!/bin/bash # Choose which ...


6

In order to do recursive globs in bash, you need the globstar feature from bash version 4 or higher. From the bash manpage: globstar If set, the pattern ** used in a pathname expansion context will match a files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and ...


6

With zsh or ksh93 -G, you could do: java -jar ../jsignpdf-1.4.3/JSignPdf.jar ./**/*.pdf -a You could do the same with fish or bash -O globstar, but beware that those may traverse symlinks when descending the directory tree. Otherwise, if the list of files is not too big, you could do: find . -name '*.pdf' -exec sh -c ' exec java -jar ...


5

Why not use rsync instead? rsync -a /branch2/media/ /branch1/media/ The reason why mv can't move /branch2/media/cd/ to /branch1/media is because /branch1/media already has a cd/ in it. mv refuses to clobber non-empty directories.


5

xargs is rarely useful, because it expects input quoted in a highly peculiar way that no common tool produces. And as you've noticed mycommand $(find …) is no good, because it first concatenates all the file names and then splits at whitespace. Use the -exec primary of find to make it execute md5 with no intervening shell that would require quoting. If your ...


5

Normally you wouldn't want to actually search EVERYTHING on the system. Linux uses file nodes for everything, so some "files" are not things you would want to search. For example /dev/sda is the physical block device for your first hard drive. You probably want to search the mounted file systems not the raw disk device. Also there is /dev/random which spits ...


5

With bash ≥4 or zsh and GNU tar: tar -czf dir.tar.gz dir/**/*.pdf This might not work if you have a very large number of PDF files and the command line is too long. Then you would need a more complex find-based solution (again, using GNU tar): tar -cf dir.tar -T /dev/null find dir -name '*.pdf' -exec tar -rf dir.tar {} + gzip dir.tar Alternatively (and ...


5

Try using the globstar option in bash; I believe that zsh has a similar option. $ shopt -s globstar $ echo pylib/**/pyerector.py pylib/pyerector.py pylib/pyerector/pyerector.py You can read more about it in the manpage.


4

If your shell is bash ≥4, put setopt globstar in your ~/.bashrc. If your shell is zsh, you're good. Then you can run grep -n GetTypes **/*.cs **/*.cs means all the files matching *.cs in the current directory, or in its subdirectories, recursively. If you're not running a shell that supports ** but your grep supports --include, you can do a recursive ...


4

You can use a tool called ack. It is descibed as: ack is a tool like grep, designed for programmers with large trees of heterogeneous source code. Basically it is similar to grep in that it searches files for patterns, but with a few key differences. Namely, it searches recursively and ignores version control and backup files by default (e.g. CVS, ...


4

You could use grep: grep "text" /path/to/directory/* For recusive search you could use -r option for grep: grep -r "text" /path/to/directory/* or ** in path: grep "text" /path/to/directory/**/* but, availability of ** operator is shell dependent - as far I know it is in zsh and bash (4 only?), it may not be available in other shells.


4

Try: find /path/to/start/at -type f -print | wc -l as a starting point, or if you really only want to recurse through the subdirectories of a directory (and skip the files in that top level directory) find `find /path/to/start/at -mindepth 1 -maxdepth 1 -type d -print` -type f -print | wc -l


4

- bare solutions - If you want find to skip the found directory's contents, but continue searching in other directories, use -prune as @laebshade suggested. The full command should then look like find . -type d -name somename -prune -exec ... On the other hand, if you want find to entirely stop searching and after finding the first matching directory, ...


4

If you want to use find, this would be the fastest way: find . -type f -exec grep pattern {} + However, Gnu grep is very likely already installed on your machine. It is part of the default installation on Solaris 11 in /usr/gnu/bin/grep. Same for Solaris 10 where you find it in /usr/sfw/bin/ggrep. On older releases, it might have been installed from ...


4

try adding: --no-parent "Do not ever ascend to the parent directory when retrieving recursively. This is a useful option, since it guarantees that only the files below a certain hierarchy will be downloaded." In my experience it also prevents downloading from other sites.



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