files
one-liners (mostly) to manage files
create subdirectory for each file
for file in *; do [ -f "$file" ] && mkdir -p "${file%.*}" && mv "$file" "${file%.*}/"; done
merge files and add filename to text
find . -maxdepth 1 -type f -name "*en.srt" -exec sh -c 'echo "=== {} ==="; cat "{}"' \; > combined_files.txt
find and delete
find . -type f -path '*4.3*/*' -name '*.yml' -exec rm {} -i \;
(-i interactive)
find files (.pl.) and count words
find ./ -type f -name "*.pl.*" -exec wc -w {} +
find the newest file recursively
find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head
(supports filenames with spaces). Performance improved with xargs
:
find $1 -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- | head
find image files in directory
find . -type f -exec file --mime-type {} + | grep "image/"
or simply by extension (-iname for case insensitive seach)
find . -type f -iname "*.{png,gif,jpg,jpeg}"
windows gitbash version:
find . -type f | grep -iE '\.png$|\.gif$|\.jpg$|\.jpeg$'
or just all contents of images/ dirs
find . -type d -name "images" -exec find {} -type f \;
find images in html files:
grep -r . -e '<img[^>]*src="[^"]*"' --include=*.html > images_in_htmls.txt
find images in markdown files:
grep -r . -e '\!\[' --include=*.md > images_in_mds.txt
count files in subdirectories
mindepth and maxdepth to only check immediate descendants. add -name e.g "*.md" etc to only count certain filetype
find . -mindepth 1 -maxdepth 1 -type d -exec sh -c 'echo -n "{}: "; find "{}" -type f | wc -l' \;
batch rename files
$ for file in *; do
extension="${file##*.}"
filename="${file%.*}"
new_filename="${filename}_pl.${extension}"
mv "$file" "$new_filename"
done
Last updated