- Create several folder with a similar pattern:
mkdir -p ./folder/subfolder{001,002,003} - Create a symbolic link (source):
ln -s target link_name
- List size in MiB of subfolders and files in the current folder and display them sorted by size:
du -cm * | sort -nr
- Case insensitive search from the current folder of all files that have the string
dummyin their filename:find ./ -iname "*dummy*"
- Recursive and case insensitive content search on non-binary files from the current folder:
grep -RiI "string to search" ./*
- Same as above but only search string in XML files:
find ./* -iname "*.xml" -exec grep -Hi "string to search" "{}" \; - Find all Jpeg images in the system but exclude
/homeand/var/libdirectory:find / -path "/home" -prune -or -path "/var/lib" -prune -or -iname "*.jpg" -print
- Get the list of the latest 10 modified files in the current folder tree:
find ./ -printf "%TY-%Tm-%Td %TT %p\n" | sort | tail -n10
- Same as above but sorted by latest access time:
find ./ -printf "%AY-%Am-%Ad %AT %p\n" | sort | tail -n10
- Rename all mp3 files in the current folder by adding a “sub-extension”:
rename "s/\.mp3/\.my-sub-extension\.mp3/g" *.mp3
- Convert all files in the current folder to lower case:
rename 'y/A-Z/a-z/' *
- Display the total size used by all PNG files in sub-directories:
find ./ -iname "*.png" -exec du -k "{}" \; | awk '{c+=$1} END {printf "%s KB\n", c}' - List all files sharing the same name within the sub folders:
find . -type f -printf "%f\n" | sort | uniq --repeated --all-repeated=separate
Dangerous Commands
- Delete all
.pycand.pyofiles in the system:find / -name "*.py[co]" | xargs rm
- Delete all empty files and folders (run this command several times to remove nested empty directories):
find . -empty -print -exec rm -rf "{}" \; - Remove empty directories found in all subfolders starting with a dot:
find . -type d -empty -ipath "./.*" -print -exec rm -rf "{}" \; - I used those commands when I import big quantity of files from a window user:
find ./* -name "desktop.ini" -print -exec rm -f "{}" \; find ./* -name "Thumbs.db" -print -exec rm -f "{}" \; - Delete all files and folders in the current directory except the
README.txtfile:ls ./ -I "README.txt" | xargs rm -rf
Presumably because of directory structures similar to:
folder1andfolder2will be deleted on the first pass, leaving/path/to/empty. Asfinddescends into each directory structure,/path/to/emptywill be found to be non-empty on the first run, and empty on the second.Pingback: Useful Commands: Introduction | Kev's blog
Pingback: All my command lines | Kevin Deldycke