Sunday, 12 May 2019

Find and optionally delete empty folders and files under Linux

To find all zero size files, simply use:
find ./ -type f -size 0
or:
find ./ -type f -empty
This commands will find all zero size files in the current directory with subdirectories and then print the full pathname for each file to the screen.
  • The ./ means start searching from the current directory. If you want to find files from another directory then replace the ./ with the path to needed directory. For example, to search everything under the system log directory you need to replace ./ with /var/log.
  • The -type f flag is specifies to find only files.
  • The -size 0 and -empty flags is specifies to find zero length files.
To find and then delete all zero size files, there are variants you can use:
find ./ -type f -size 0 -exec rm -f {} \;
find ./ -type f -size 0 | xargs rm -f
find ./ -type f -size 0 -delete
The xargs will cause all the filenames to be sent as arguments to the rm -f commands. This will save processes that are forked everytime -exec rm -f is run. But is fails with spaces etc in file names.
The -delete is the best when it is supported by the find you are using (because it avoids the overhead of executing the rm command by doing the unlink() call inside find().

Empty directories

To find all empty directories, simply use:
find ./ -type d -empty
This command will find all empty directories in the current directory with subdirectories and then print the full pathname for each empty directory to the screen.
  • The ./ means start searching from the current directory. If you want to find files from another directory then replace the ./ with the path to needed directory. For example, to search everything under the system log directory you need to replace ./ with /var/log.
  • The -type d flag is specifies to find only directories.
  • The -empty flag is specifies to find empty directories.
To find and then delete all empty directories, use:
find ./ -depth -type d -empty -exec rmdir {} \;
or:
find ./ -type d -empty -delete
The -delete is the best when it is supported by the find you are using.

Wednesday, 8 May 2019

Simple synchronising and back up of two folders for Linux

To simply synchronise / back up two folders I use rsync.
I have several folders on two different networked devices (NAS's) I want to keep in sync.
I use a script to this. I could automate this but I like to see the magic happen. Maybe I'll automate this later with a cron job but for now:
I log into one of the NASs using SSH.
Note: I have setup public and private certificates so I don't need to enter passwords. I'll leave that as an exercise for the user.

$ ssh aUser@192.168.0.3

then I run my script on that box:

remotebox:~# sh syncFiles.sh

The contents of this script is as follows:
rsync -avP --delete aUser@192.168.0.4:Shares/Public/movies/ /mnt/HD/HD_a2/movies/
rsync -avP --delete aUser@192.168.0.4:Shares/Public/music/ /mnt/HD/HD_a2/music/
rsync -avP --delete aUser@192.168.0.4:Shares/Public/pictures/ /mnt/HD/HD_a2/pictures/


As you can see from above I backup movies, pictures and music from 3 folders.

The syntax is as follows:
rsync <parameters> <source> <destination>

The parameters are:
 -a means "all" files/folders,

-v means "verbose" so we can see what is happening,
-P means show a progress bar,
--delete means if something is deleted from the source, delete it from the destination. This is great for synchronisation but maybe not what you want for your backup? Use with caution. If in doubt, don't use it initially.
<source> of course is what to copy and
<destination> is where to put it.