Commands tagged find (379)

  • Sometimes, you don't want to just replace the spaces in the current folder, but through the whole folder tree - such as your whole music collection, perhaps. Or maybe you want to do some other renaming operation throughout a tree - this command's useful for that, too. To rename stuff through a whole directory tree, you might expect this to work: for a in `find . -name '* *'`;do mv -i "$a" ${a// /_};done No such luck. The "for" command will split its parameters on spaces unless the spaces are escaped, so given a file "foo bar", the above would not try to move the file "foo bar" to "foo_bar" but rather the file "foo" to "foo", and the file "bar" to "bar". Instead, find's -execdir and -depth arguments need to be used, to set a variable to the filename, and rename files within the directory before we rename the directory. It has to be -execdir and won't work with just -exec - that would try to rename "foo bar/baz quux" to "foo_bar/baz_quux" in one step, rather than going into "foo bar/", changing "baz quux" to "baz_quux", then stepping out and changing "foo bar/" into "foo_bar/". To rename just files, or just directories, you can put "-type f" or "-type d" after the "-depth" param. You could probably safely replace the "mv" part of the line with a "rename" command, like rename 'y/ /_/' *, but I haven't tried, since that's way less portable.

    find . -depth -name '* *' -execdir bash \-c 'a="{}";mv -f "$a" ${a// /_}' \;
    DewiMorgan · 2012-02-28 04:03:40 1
  • Just want to post a Perl alternative. Does not count hidden files ('.' ones). Show Sample Output

    perl -le 'print ~~ map {-s} <*>'
    MarxBro · 2012-02-21 21:09:48 0
  • Applies each file operator using the built-in test. testt /home/askapache/.sq /home/askapache/.sq -a True - file exists. -d True - file is a directory. -e True - file exists. -r True - file is readable by you. -s True - file exists and is not empty. -w True - the file is writable by you. -x True - the file is executable by you. -O True - the file is effectively owned by you. -G True - the file is effectively owned by your group. -N True - the file has been modified since it was last read. Full Function: testt () { local dp; until [ -z "${1:-}" ]; do dp="$1"; [[ ! -a "$1" ]] && dp="$PWD/$dp"; command ls -w $((${COLUMNS:-80}-20)) -lA --color=tty -d "$dp"; [[ -d "$dp" ]] && find "$dp" -mount -depth -wholename "$dp" -printf '%.5m %10M %#15s %#9u %-9g %#5U %-5G %Am/%Ad/%AY %Cm/%Cd/%CY %Tm/%Td/%TY [%Y] %p\n' -a -quit 2> /dev/null; for f in a b c d e f g h L k p r s S t u w x O G N; do test -$f "$dp" && help test | sed "/-$f F/!d" | sed -e 's#^[\t ]*-\([a-zA-Z]\{1\}\) F[A-Z]*[\t ]* True if#-\1 "'$dp'" #g'; done; shift; done } Show Sample Output

    testt(){ o=abcdefghLkprsStuwxOGN;echo $@;for((i=0;i<${#o};i++));do c=${o:$i:1};test -$c $1 && help test | sed "/^ *-$c/!d;1q;s/^[^T]*/-$c /;s/ if/ -/";done; }
    AskApache · 2012-02-21 16:54:53 2

  • 4
    find . -maxdepth 1 -type f | wc -l
    marek158 · 2012-02-21 16:37:43 1
  • You must have the android sdk installed with 'adb' executable on your system. This is just a way to loop over files in a folder using 'find' to locate and install android apps. Show Sample Output

    find ~/path/to/apk/files -name '*.apk' -exec adb install {} \;
    dacookiemonn2be · 2012-02-16 03:59:44 0
  • Output of this command is the difference of recursive file lists in two directories (very quick!). To view differences in content of files too, use the command submitted by mariusbutuc (very slow!): diff -rq path_to_dir1 path_to_dir2 Show Sample Output

    diff --suppress-common-lines -y <(cd path_to_dir1; find .|sort) <(cd path_to_dir2; find .|sort)
    knoppix5 · 2012-02-13 12:49:33 1

  • 2
    find . ! -name "*.tar.gz"
    ohe · 2012-02-09 13:10:36 1
  • Finally, we can make the file "unchangeable" sudo chattr +i

    find /protocollo/paflow -type f -mtime +5 | xargs tar -cvf /var/dump-protocollo/`date '+%d%m%Y'_archive.tar`
    0disse0 · 2012-02-03 16:24:08 0
  • Find all files under "." that are invalid NTFS filenames. Find locates all files, and grep shows the invalid ones.

    find . | grep -E "(\||\\|\?|\*|<|\"|:|>)"
    dj_bushido · 2012-01-21 17:59:20 0
  • Find is used to "find" all filenames - grep shows those that are invalid.

    find . | grep -E "(\||\\|\?|\*|<|\"|:|>|\+|\[|\])"
    dj_bushido · 2012-01-21 17:54:58 0
  • I use this find command example to find out all the executable files you can modify it to find readonly file as well.

    find . -perm 777 ?print
    javabuddy · 2012-01-21 10:20:52 0
  • This command does a basic find with size. It also improves the printout given (more clearer then default) Adjusting the ./ will alter the path. Adjusting the "-size +100000k" will specify the size to search for. Show Sample Output

    find ./ -type f -size +100000k -exec ls -lh {} \; 2>/dev/null| awk '{ print $8 " : " $5}'
    Goez · 2012-01-21 04:19:35 0
  • you can find a special things(with defined -iname "*sql*") from in most of one direcroty(for example from both /etc/ and /pentest/) and then you can want to grep only include "map" word Show Sample Output

    find /etc/ /pentest/ -type f -iname "*sql*" | grep map
    celalerdik · 2012-01-15 13:30:32 0
  • you can find all "trace" phrases within everywhere else under the pentest directory.. Show Sample Output

    find /pentest/ -type f -iname "*trace*"
    celalerdik · 2012-01-14 20:43:30 0
  • A different approach to the problem - maintain a small sorted list, print the largest as we go, then the top 10 at the end. I often find that the find and sort take a long time, and the large file might appear near the start of the find. By printing as we go, I get better feedback. The sort used in this will be much slower on perls older than 5.8. Show Sample Output

    find . -type f|perl -lne '@x=sort {$b->[0]<=>$a->[0]}[(stat($_))[7],$_],@x;splice(@x,11);print "@{$x[0]}";END{for(@x){print "@$_"}'
    bazzargh · 2012-01-08 14:43:43 0

  • 0
    find $(pwd) -maxdepth 1 -name "*" -printf "%p\n"
    Jubal · 2012-01-06 18:04:14 0
  • Good for finding outdated timthumb.php scripts which need to be updated, anything over 2.0 should be secure, below that timthimb is vulnerable and can be used to compromise your website. Show Sample Output

    find `pwd` -type f \( -iname thumb.php -or -iname timthumb.php \) -exec grep -HP 'define ?\(.VERSION' {} \;
    djkee · 2011-12-27 11:33:54 1

  • 0
    find `pwd` -maxdepth 1 -exec ls --color -d {} \;
    kev · 2011-12-16 06:18:35 2
  • This sums up the page count of multiple pdf files without the useless use of grep and sed which other commandlinefus use. Show Sample Output

    find -iname "*.pdf" -exec pdfinfo -meta {} \;|awk '{if($1=="Pages:"){s+=$2}}END{print s}'
    Barabbas · 2011-12-13 15:02:11 0
  • Better awk example, using only mplayer, grep, cut, and awk. Show Sample Output

    mplayer -endpos 0.1 -vo null -ao null -identify *.avi 2>&1 |grep ID_LENGTH |cut -d = -f 2|awk '{SUM += $1} END { printf "%d:%d:%d\n",SUM/3600,SUM%3600/60,SUM%60}'
    Coderjoe · 2011-12-12 15:49:07 0

  • 0
    find /usr/include/ -name '*.[c|h]pp' -o -name '*.[ch]' -print0 | xargs -0 wc -l | tail -1
    f4m8 · 2011-12-02 07:20:09 0
  • Count your source and header file's line numbers For example for java change the command like this find . -name '*.java' -exec cat {} \;|wc -l Show Sample Output

    find /usr/include/ -name '*.[c|h]pp' -o -name '*.[ch]' -exec cat {} \;|wc -l
    kerim · 2011-12-01 19:58:52 3

  • 0
    find . -type f -exec awk '/linux/ { printf "%s %s: %s\n",FILENAME,NR,$0; }' {} \;
    Neo23x0 · 2011-11-29 12:32:06 1
  • cd / find `pwd` -name '.*' -prune -o \( -name *.h -o -name *.cpp \) -print | cscope -bi- export CSCOPE_DB=/cscope.out vim +'set cst'

    find `pwd` -name '.*' -prune -o \( -name *.h -o -name *.cpp \) -print | cscope -bi-
    kev · 2011-11-02 08:43:40 1

  • 4
    find . ! -name "$(printf '*[\001-\037\177]*')"
    totti · 2011-10-25 17:29:19 1
  • ‹ First  < 4 5 6 7 8 >  Last ›

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: