Log File Maintenance and Cleanup
Log files sometimes take up a lot of disk space. Some applications have internal processes that run periodically to manage them and some do not. Often, this becomes a problems after a while as the logs consume all of your partition space.
You can manage these files yourself with a simple script running in a cronjob (or systemd timers if you’re so inclined) if they have a common naming convention and you have the proper access.
Finding Duplicate Files in a Directory Tree
Sometimes I need to find all of the duplicate files in a directory tree. I have this issue all of the time when I move my music collection. Here is a nifty script to sort these things out:
#!/bin/bashfind -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 -D --all-repeated=separate
Processing The Results of The Find Command
As mentioned in the previous post, the find command searches for files, but it has another very useful feature in that it can also perform an action on the files it finds.
I am a vim user, and let’s assume that I want to find my editor’s backup files in the current directory trees. These filesall end with a tilda (~) character. We would use this command to search for them:
Using the Find Command to Search for Files
One of the most useful and flexible GNU utilities is the find command. Understanding how to use this command is very important to make you Linux life more efficient.
The general syntax of the find command is:
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
That looks like a lot, but most of the time you may only need 2 things:
find [path] [expression]
Move Files Older Than So Many Days with Find
You may want to clean up a directory that has files older than a certain number of days, for example, 30 days. You can do this with the find command:
To move files older than 30 days in current folder to the old folder:
$ find . -mtime +30 -exec mv {} old/ \;
Rename Files That Start With a Special Character
Suppose you find that you have a file with a special character and you want to delete it:
$ ls-badfile.txt PrintHood reg57.txtFavorites Recent scripts$ rm -badfile.txtrm: invalid option -- 'b'Try 'rm ./-badfile.txt' to remove the file '-badfile.txt'.Try 'rm --help' for more information.$ ls *.txtls: invalid option -- 'e'Try 'ls --help' for more information.
How to recursively find the latest modified file in a directory
find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "
How To Count All The Files Extension Recursively In Linux
To count all the files by file extension recursively on the command line $ find . -type f | sed -n ’s/..*.//p’ | sort | uniq -c 40 3g2 5 AVI 13 DS_Store 28 JPG 30 MOV 133 MP4 64 THM 1 docx 18 jpg 1 json 4 m3u 89 m4a 2 m4r 156 m4v 41 mkv 112 mov 38 mp3 587 mp4 1 nfo 2 osp 30 png 1 sh 4 srt 6 svg 10 torrent 6 txt 5 webm 10 zip