Found 20 files with the largest size

No Comments »

e.g.

 # find /var/log -type f -exec du -sm {} \; | sort -n -r | head -20 

 
41      /var/log/cabal/WorldSvr_10_01.log
35      /var/log/cabal/DBAgent_10.log
34      /var/log/cabal/WorldSvr_10_10.log
5       /var/log/maillog.4
5       /var/log/maillog.3
5       /var/log/cabal/WorldSvr_10_05.log
5       /var/log/cabal/WorldSvr_10_04.log
5       /var/log/cabal/WorldSvr_10_02.log
4       /var/log/cabal/WorldSvr_10_03.log
4       /var/log/cabal/LoginSvr_01.log
3       /var/log/maillog.2
3       /var/log/maillog.1
1       /var/log/yum.log
1       /var/log/Xorg.0.log.old
1       /var/log/Xorg.0.log
1       /var/log/wtmp.1
1       /var/log/wtmp
1       /var/log/secure.4
1       /var/log/secure.3
1       /var/log/secure.2

Share

find — how to replicate a complex directory and file structure

No Comments »

Consider an example where you have a directory, called timit with many sub-directories that contain lots of files. Now, lets say you want to create a mirror copy of this complex directory structure in the /tmp directory, as well as a selective copy of its files based on certain pattern. Lets say we only want to copy files that have the following string somewhere in their filename, snr15. There are many possible solutions to this problem, you could write a perl or bash script, you could possibly use the rsync utility, but the simplest way I could find (on the Fedora Forum) is to use the Linux find command. Let me show you how it is done. It is a two step process. First step is to create a mirror copy of the directory structure as follows.

$ find timit/ -type d -exec mkdir -p /tmp/{} \;

# ©2007 linux.dsplabs.com.au

The second step is to create a copy of the files matching a specific criteria.

$ find timit/ -type f -name "*snr15*" -exec cp -p {} /tmp/{} \;

# ©2007 linux.dsplabs.com.au

If you wanted to conserve the disk space you could create hard links to the original files instead of actually copying them. This is especially useful if you are not going to modify any of the linked files (keep in mind that changes to linked files under /tmp/timit would also be reflected under ./timit and vice versa). Here is the modified command.

$ find timit/ -type f -name "*snr15*" -exec ln {} /tmp/{} \;

# ©2007 linux.dsplabs.com.au

If you prefer soft links instead of hard links, then replace the ln above with ln -s. OK, to finish up, let me mention a couple of other useful find one-liners. Either of the following commands could be used to remove all of the files containing a snr30 sub-string from the original directory.

$ find timit/ -type f -name "*snr30*" -delete $find timit/ -type f -name "*snr30*" -print0 |xargs -0 rm -f

On the other hand, to remove all of the files that did not contain the snr30 sub-string the following command could be used.

$ find timit/ -type f ! -name "*snr30*" -delete

Linux find command is a very versetile and useful tool, make good use of it!

Share
keep looking »