User Tools

Site Tools


linux:find

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux:find [2018/07/03 16:50] Wulf Rajeklinux:find [2025/02/11 21:14] (current) Wulf Rajek
Line 1: Line 1:
 ====== find examples ====== ====== find examples ======
 +
 +General note to ''-exec {} +'' vs ''-exec {} \;'':
 +The following executes program and sends a space separated list of quoted filenames to it. Program may be called multiple times if the amount of arguments exceeds ARG_MAX.
 +<code>find . -exec program {} +</code> 
 +
 +The following executes program with one! quoted filename, then executes program again with the next etc.
 +<code>find . -exec program {} \;</code> 
 +
 +
 +
  
 Find and extract all rar files in subfolders into the current folder: Find and extract all rar files in subfolders into the current folder:
Line 45: Line 55:
 Convert tabs to spaces at the beginning of lines in source code: Convert tabs to spaces at the beginning of lines in source code:
 <code> <code>
-find . -name '*.php' -type f -exec bash -c 'expand -i -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \;+find . -iname '*.php' -type f -exec bash -c 'expand -i -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \; 
 +</code> 
 +Better with 'sponge' from the moreutils package as it retains permissions: 
 +<code> 
 +find . -iname '*.php' -type f -exec bash -c 'expand -i -t 4 "$0" | sponge "$0"' {} \; 
 +</code> 
 + 
 + 
 +Fixing permissions of directories / files of a path: 
 +<code> 
 +find /path/to/base/dir -type d -print0 | xargs -0 chmod 755  
 +find /path/to/base/dir -type f -print0 | xargs -0 chmod 644 
 +</code> 
 + 
 +Recursive file renaming (e.g. from "something [xxxxp].ext" to "something.xxxxp.ext") : 
 +<code> 
 +#NOTE: use -n in rename parameter to test! 
 +find . -type f -name "*\[*" -print0 | xargs -r0 rename 's/(.*) \[(\d\d\d\d?p)\](.*)/$1.$2$3/' -- 
 +</code> 
 + 
 +Recursive file renaming extension (e.g. from "something.oldext" to "something.newext") : 
 +<code> 
 +find . -type f -name '*.oldext' -print0 | xargs -0 rename 's/\.oldext/\.newext/' 
 +</code> 
 + 
 + 
 +Move files in subdirectories to other directory while retaining directory structure 
 +<code> 
 +find . -maxdepth 2 -type f -print | cpio -pvdumB /media/music/_OrganizeA_Leftover ; find . -maxdepth 2 -type f -delete 
 + 
 +#Do this for each directory 
 +for d in */; do 
 +    cd $d; find . -maxdepth 2 -type f -print | cpio -pvdumB /media/music/_OrganizeA_Leftover ; find . -maxdepth 2 -type f -delete; cd .. 
 +done 
 + 
 +#If directory must not have trailing /, use this in substitution 
 +${d%/*} 
 + 
 +delete empty directories: 
 +find /home/something/ -type d -empty -delete 
 +</code> 
 + 
 +====== Replace spaces with underscores ====== 
 +Mass replace spaces with underscores 
 +<code> 
 +for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done 
 +</code> 
 + 
 +====== Find largest subdirectories ====== 
 +<code> 
 +du -hd1 | sort -h 
 +</code> 
 + 
 +====== Recursively changing extension ====== 
 +<code> 
 +find . -type f -name "*.part" -exec sh -c 'mv "$1" "${1%.part}.!qB"' _ {} \; 
 +</code> 
 + 
 +====== Recursively delete empty directories ====== 
 +<code> 
 +find . -type d -empty -delete
 </code> </code>
linux/find.1530633032.txt.gz · Last modified: 2023/05/29 11:53 (external edit)