Find

From DikapediaV2
Jump to: navigation, search

find - Search for files in a directory hierarchy.



Examples:

find /dir -name "filename"       # Searches up the "dir" directory with "filename"
find /dir -iname "filename"      # ^^^ does the same but insensitive.
find /dir -emtpy                 # Searches up all the empty files in "dir" directory. 


Displays the 5 LARGEST files in the directory THAT YOU ARE IN. (hence the dot (.)

find . -type f -exec ls -s {} \; | sprt -n -r | head -5 


Displays the 5 SMALLEST files in the directory THAT YOU ARE IN (hence the dot (.))

find . -type f -exec ls -s {} \; | sprt -n | head -5 


Other useful commands:

find /dir -type d                # Searches all directories in the "dir" directory.
find /dir -type f                # Searches all files in the "dir" directory
find /dir -type f -name ".*"     # Displays HIDDEN FILES in "dir" directory
find /dir -type d -name ".*"     # Displays HIDDEN directories in "dir" directory
find /dir -size +1M              # Displays all files that 1 Megabyte or larger
find /dir -size 2m               # Displays all files that are exactly 2 Megabytes
find /dir - size -1M             # Displays all files that are less than 1 MB


To go through a directory, and add a "index.html" file into every other directory within that specific directory: https://askubuntu.com/questions/1051401/how-to-add-empty-index-html-from-each-folder-and-subfolder

  • This will find all directories (-type d) and for each, execute the command touch {}/index.html where {} is replaced by the path name of the directory. touch creates a file if it doesn't exist; if it does, touch does nothing.
  • The \; at the end signifies the end of the touch command.
  • Just replace top/level/directory by the proper path name for your web site.
find top/level/directory -type d -exec touch {}/index.html \;


Using the Find command to change permissions recursively


This changes the permissions of the files within the DocNav directory based on whether they are executables or not.

LOCDIR=DocNav; dzdo find ./${LOCDIR} -perm /u=x -exec chmod a+x {} \; && dzdo chmod -R a+r ./$LOCDIR