Tag Info

Hot answers tagged

21

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


17

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

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


11

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


11

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


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

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


10

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


9

I wouldn't even write the script -- you should be able to put the find command in directly. You can also call the delete command directly from find using the -delete action flag. Step 1: edit crontab crontab -e Step 2: add in the following line (this will run it daily at 4:30am, change to your liking): 30 4 * * * find /path/to/directory -name ...


9

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


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


8

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!


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

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


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

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


6

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


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

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


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

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


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 (fixed in bash 4.3). Otherwise, if the list of files is not too big, you could do: find . -name '*.pdf' -exec sh -c ' exec java ...


6

You don't need that extra ls -tr. This is equivalent to your command and faster: find . -type f | xargs stat --printf="%y %n\n" | sort -n Something like this will exclude a subdirectory of files: find . -type f ! -path './directory/to/ignore/*' \ | xargs stat --printf="%y %n\n" \ | sort -n This will still check every file, if you want to ...


6

As said many time on this site, leaving a variable expansion (as in $var) or command substitution (as in `cmd` or $(cmd)) (or arithmetic expansion (as in $((11 * 11))) in most shells) unquoted in Bourne/POSIX shells is the split+glob operator. The content of $var or the output of cmd (without the trailing newline characters) is split according to the ...


6

Don't forget the possibility that the server being unreachable after the rm command had nothing to do with that. It could be a coincidence! Most likely though, the current working directory was not what you thought, when the command was issued. Were you root when doing this? This is what happens when issuing the command rm -rf *: The shell resolves the ...


6

There can't be multiple files named nohup.out in a single directory, so I assume you mean that you want to remove it recursively: find . -name nohup.out -exec rm {} + If you are using GNU find, you can use -delete: find . -name nohup.out -delete In bash4+, you can also use globstar: shopt -s globstar dotglob rm -- **/nohup.out Note, however, that ...



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