How to recursively go through all local or remote directories

You can use find command or recursdir command to recurse through local or remote directories to command/find files or create tar files.

recursdir command pass a C script to recursively perform operations on files. recursdir is an excellent tool for automatic stuff. It provides C style programming functions and statements such as:

  • strncmp()
  • exec()
  • system()
  • strstr()
  • strcat()
  • printf()
  • popen()
  • if ( expr ) { take-action }
  • if ( expr ) { take-action } else { do-something-else; }
  • /* comments */
  • Detecting file types and macros
  • All of the logical, arithmetic and bitwise C operators are supported. These are ( ) >=

Install recursdir

recursdir is part of mirrordir package:
$ sudo apt-get install mirrordir

Recursively find file and take action

For example find out all *.bak file and delete them, using find command:
find /path/to/dir -iname "*bak" -exec rm {} \;
Following is a fast and overkill equivalent of find, using recursdir :
recursdir /path/to/dir -C 'exec("/bin/rm","-f",PATH);'

Find and print out all the *.CPP (C++) files on your system, enter (note usage of if and printf() C like syntax):
recursdir /path/to/dir -C 'if (!glob ("*.cpp", FILE)) printf ("%s\n", PATH);'
find /path/to/dir -iname "*.cpp"

Backup a remote ftp site to local tape:

This is my favorite command to mirror remote ftp tree:
recursdir --tar-file /dev/mt

Write complex directory manipulation scripts

You can write complex scripts using recursdir. For example, following will removes all core files from your Linux 32 bit system:

recursdir / -C '
              long l;
              /* look for /proc */
              if (strncmp (PATH, "/proc", 5)) {
                  if (S_ISREG (stat.st_mode) && !strcmp ("core", FILE)) {
                      if (strstr (popen ("file " + PATH), "ELF 32-bit LSB core")) {
                          l = l + stat.st_size;
                          printf ("removing: %s, cumu. total = %ldkB\n", PATH, l >> 10);
                          exec ("rm", "-f", PATH);

recursdir offers many more advanced options such as encryption, backups compression and many more, please refer the man page for more information.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @