Useful tricks when using the find command on Linux and backticks.

Using the Linux command-line allows a lot of flexibility when running commands and how they are run. Take the example below for instance.

rm `find ./ -name "*~"`

This command runs a command within backticks and outputs the result as an argument to the rm command. This command will therefore delete all files in the current directory and subdirectories that end with a tilde character. This is easier than using the Windows Powershell; that is an annoying shell to use and the commands are more complex to type than the Bash shell commands let alone installing more cmdlets to extend the functionality of the Powershell; on Linux you just install something with apt-get or ./configure ; make ; make install and it will work straight away; and the system of permissions that the Linux distributions use as well as the secure open source nature of Linux makes it a good choice on the desktop. Windows falls prey to many attacks by malicious software. Linux on the other hand is far more secure.

The ability to customise the BASH shell prompt as well as the many scripting languages available for Linux elevate it above Powershell in my opinion. Here is a useful script for Linux that will search for bad symlinks in the current directory. This is one I got off my Linux tips page.

function badlink()
# From Atomic magazine #43 August 2004.
	DEFAULT=$(tput sgr0);
	[ -e $FILELIST ] && $( rm -fr $FILELIST )
	function checklink()
	for badlink in $1/*; do
		[ -h "$badlink" -a ! -e "$badlink" ] && echo \
			\"$badlink\" >> $FILELIST
		[ -d "$badlink" ] && checklink $badlink
	for directory in `pwd`; do
		if [ -d $directory ] ; then
		checklink $directory;
	if [ -e $FILELIST ] ; then
		for line in $(cat $FILELIST); do
			echo $line | xargs -r rm | echo -e "$line \
	rm -fr $FILELIST
		printf "Bad symlinks not found.\n\n"
} # End Atomic function.

This command is a good way to list all backup files in a folder.

[flynn@flynn-grid-runner Documents]$ ls -hula `find ./ -name "*~"`
-rw-rw-r-- 1 flynn flynn 1.2K  07-09-12 09:22 pm ./Code/John.cpp~
-rw-rw-r-- 1 flynn flynn 6.1K  01-09-12 11:14 pm ./
-rw-rw-r-- 1 flynn flynn 2.4K  11-10-12 10:59 pm ./Document.txt~
-rw-rw-r-- 1 flynn flynn  218  03-09-12 06:20 am ./mynew.cpp~
-rw-rw-r-- 1 flynn flynn  12K  06-09-12 06:17 pm ./MySQL/acme-sample-database.sql~
-rw-rw-r-- 1 flynn flynn 9.4K  02-09-12 07:23 pm ./MySQL/acme.sql~
-rw-rw-r-- 1 flynn flynn  18K  05-09-12 09:11 am ./pan.c~
-rw-r--r-- 1 flynn flynn 5.2K  12-07-12 11:44 pm ./sysinfo/src/sysinfo.cpp~
-rw-rw-r-- 1 flynn flynn  353  05-09-12 09:05 am ./test.cpp~

And then you may delete them; but if you do it this way you can see what will be deleted.

Using backticks in a Perl script is another way to run a shell command in the script.

$ls=`ls -hula`;
printf("%s", $ls);

And this is the output you get.

[flynn@flynn-grid-runner Scripts]$ perl ../
total 24K
drwx------  2 flynn flynn 4.0K  17-10-12 09:23 am .
drwxr-xr-x 17 flynn flynn 4.0K  17-10-12 06:05 pm ..
-rw-------  1 flynn flynn 1.1K  09-10-12 11:18 am
-rw-------  1 flynn flynn  102  09-10-12 09:16 pm
-rw-------  1 flynn flynn  135  09-10-12 11:18 am
-rw-------  1 flynn flynn  880  09-10-12 11:18 am
[flynn@flynn-grid-runner Scripts]$
1 responses to “Useful tricks when using the find command on Linux and backticks.

Leave a Reply