Tag Info

Hot answers tagged

22

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


18

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


14

find -maxdepth 1 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" -type f | 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 ...


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.


13

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


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


12

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


12

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


11

GNU grep allows searching recursively through subdirectories: grep -r --include='*.h' 'the string' .


11

Zsh's extended glob operators support matching over / (unlike ksh's, even in zsh's implementation). Zsh's **/ is a shortcut for (*/)# (*/ repeated 0 or more times). So all I need to do is replace that * by ^.svn (anything but .svn). print -l (^.svn/)# Neat!


11

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


10

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)


10

Your find should work if you change -v -l (files that have any line not matching) to -L (files with no lines matching), but you could also use grep's recursive (-r) option: grep -rL shared.php .


9

You can use find. If, for example, you wanted to find all files and directories that had abcd in the filename, you could run: find . -name '*abcd*'


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

sftp, like cp and scp, requires that when you move a folder (and its contents, obviously), you have to explicitly tell it you want to transfer the folder recursively with the -r option. So, add -r to the command.


8

Using Gilles' solution and after reading the man find(1) again I found a more simple solution. The best option is the -newerXY. The m and t flags can be used. m The modification time of the file reference t reference is interpreted directly as a time So the solution is find -type f -newermt 20111222 \! -newermt 20111225 The lower bound in ...


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

To delete hidden files, you have to specify: rm -r images/* images/.* This will lead to an error like rm: cannot remove `.' directory `images/.' rm: cannot remove `..' directory `images/..' but it will delete hidden files. An approach without errormessage is, to use find/delete with mindepth. This is gnu-find. find images -mindepth 1 -delete Your ...


7

Try this command (find and cp with --parent option): find /source -regextype posix-extended -regex '.*(gif|jpg)' \ -exec cp --parents {} /dest \; -print


7

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


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 could specify a list of allowed resp. disallowed filename patterns: Allowed: -A LIST --accept LIST Disallowed: -R LIST --reject LIST LIST is comma-separated list of filename patterns/extensions. You can use the following reserved characters to specify patterns: * ? [ ] Examples: only download PNG files: -A png don't download CSS files: -R ...


7

If you want to see the file name and line number, POSIXly: find . -name 'CMake*' -type f -exec grep -nF /dev/null version {} + (you don't want to use ; here which would run one grep per file). That's the standard equivalent of the GNUism: find . -name 'CMake*' -type f -print0 | xargs -r0 grep -nHF {} + find (in the first), xargs (in the second) will ...


7

Doing this is as simple as: find -exec sh -c 'inline script "$0"' {} \; Or... find -exec executable_script {} \;


7

For the stated question you can use find: find . -mindepth 1 ! -type l will list all files and directories in the current directory or any subdirectories that are not symlinks. mindepth 1 is just to skip the . current-directory entry. The meat of it is the combination of -type l, which means "is a symbolic link", and !, which means negate the following ...


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



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