Hot answers tagged

39

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


39

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


36

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


35

Try doing this : du -s dir or du -sh dir needs -h support, depends of your OS. See man du


34

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 all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match. ...


34

Use this instead: cp -R inputFolder/. outputFolder This works in exactly the same way that, say, cp -R aaa/bbb ccc works: if ccc doesn't exist then it's created as a copy of bbb and its contents; but if ccc already exists then ccc/bbb is created as the copy of bbb and its contents. For almost any instance of bbb this gives the undesirable behaviour that ...


32

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


32

If you want to grep recursively in all .eml.gz files, you can use: find -name \*.eml.gz -print0 | xargs -0 zgrep "STRING" You have to escape the first '*' so that the shell does not interpret it. "-print0" tells find to print a null character after each file it finds; "xargs -0" reads from standard input and runs the command after it for each file; ...


30

rm -r works on each of its arguments in turn. If an argument is a directory, it lists the directory (with the opendir and readdir functions or some equivalent method), and operates on each entry in turn. If an entry is a directory, it explores that entry recursively. This is exactly the same method that other applications use to traverse directories ...


29

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


28

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


27

Most POSIX commands that have recursive directory traversal option (ls, chmod, chgrp, chmod, cp, rm) have -R for that. rm also has -r because that's what it was initially. Now, the behaviour varies when symlinks are found in walking down the tree. POSIX tried to make things consistent by adding the -L/-H/P options to give the user a chance to decide what ...


24

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 .


23

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


21

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


19

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


19

rm recursion only works downwards correct? rm -r x y will delete x and y and everything inside them (if they are directories), but not their parents or anything outside them. Running: sudo rm -R *.QTFS will delete all *.QTFS files in current directory and its children, correct? No. It will delete all files named *.QTFS, any files recursively ...


18

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


18

There's a lot of confusion here because there isn't just one zgrep. I have two versions on my system, zgrep from gzip and zgrep from zutils. The former is just a wrapper script that calls gzip -cdfq. It doesn't support the -r, --recursive switch.1 The latter is a c++ program and it supports the -r, --recursive option. Running zgrep --version | head -n 1 will ...


17

Don't copy the folder, only copy the contents: ## Create the target directory. The -p suppresses error messages ## if the directory already exists mkdir -p outputFolder ## Copy the contents recursively, this will not recreate the parent cp -R inputfolder/* outputfolder/ This way you both ensure that the target directory is created the first time the ...


16

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.


16

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!


15

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*'


14

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


14

use find to output file names with the file size. sort. print out the largest one. find . -type f -printf "%s\t%p\n" | sort -n | tail -1 A "pure" bash solution shopt -s globstar max_s=0 for f in **; do if [[ -f "$f" ]]; then size=$( stat -c %s "$f" ) if (( $size > $max_s )); then max_s=$size max_f=$f fi fi done echo ...


13

To delete all files and directories(including the hidden ones) in a directory, you can try the following: use ls -Ab to match all files/directories cd dir_name && rm -rf * .* use find to match all files/directories find dir_name -mindepth 1 -delete or, if your find doesn't support -delete: find dir_name -mindepth 1 -exec rm -rf {} delete ...


13

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


13

sftp, like cp and scp, requires that when you copy 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.


13

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



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