Some Linux commands recurse without being asked, while others have to be nudged with just the right option. Here are some ways to use it to make your tasks easier. Credit: Torley Linden Linux and recursion are on very good speaking terms. In fact, a number of Linux commands recurse without ever being asked, while others have to be coaxed with just the right option. When is recursion most helpful and how can you use it to make your tasks easier? Let’s run through some useful examples and see. Easy recursion with ls First, the ls command seems like a good place to start. This command will only list the files and directories in the current or specified directory unless asked to work a little harder. It will include the contents of directories only if you add a -R option. It provides a -r option, but that option causes the listing to be in reverse order as shown below, while -R delves into the various subdirectories. $ ls /usr/local bin etc games include lib man sbin share src $ ls -r /usr/local src share sbin man lib include games etc bin $ ls -R /usr/local /usr/local: bin etc games include lib man sbin share src /usr/local/bin: /usr/local/lib: python2.7 python3.6 /usr/local/lib/python2.7: dist-packages site-packages Duplicating a directory with cp The recursive option with the cp command allows you to easily duplicate a directory. $ cp -r bin bin- $ ls bin- case1 killit prime1 sieve tryme $ ls bin case1 killit prime1 sieve tryme Thorough cleansing with rm One of the more heavily used recursive options is the -r command that is used with the rm command, allowing directories and their contents to be removed in a single command. Commands like this will clean out directories that are no longer needed, but they will not follow symbolic links (only remove them). Deep digging with find The find command doesn’t need to be cajoled into recursing. You give it a starting point where you want to search, and it traverses into every directory that it can starting at that point. If you want the find command not to recurse at all or if you want to limit how deeply it delves into subdirectories, you have to impose limitations with the -maxdepth setting. When set to 1, maxdepth will mean that the find command will only look in the current directory. When set to 2, it will look into subdirectories, as well, but it will not look any further. $ find . -maxdepth 2 -type f -ls The find command also provides a lot of other opportunities to make recursive changes with its -exec option. Add the command you want to run with syntax like “-exec chmod a-x {};” to your find command to remove all execute permission from the specified files. This command would remove execute permission for any file in the current directory and below but would not touch directories themselves. Note that “{}” represents the file names and “! -type d” expresses “not a directory”. $ sudo find . ! -type d -exec chmod -x {}; Groping with grep The grep command is another command that needs to be told if you want it to recurse. But unless you want to see a lot of complaints, such as “bin is a directory”, you’re better off limiting what it tells you by adding the -s (suppress error messages) option. $ grep -rs world * code/oops.c: printk("Goodbye worldn"); oops.c: printk("Goodbye world1n"); Changing ownership with chown The chown command uses the -R option to recurse. Want to change ownership of all files within a particular directory (and below), just use chown -R. chown -R jdoe /home/jdoe Zipping along with zip The zip command offers an easy option for recursively pulling files into a zip file — -r. A command like this one will create a zip file of all the files in the bin directory. $ zip -r bin.zip bin Making directory trees with mkdir While not strictly recursion, the mkdir command does something similar when it allows you to create a deep directory structure with a single command. The -p (parent) option tells the mkdir command to create any of the directories that don’t exist — whatever is required to end up with the directory specified. $ mkdir -p fun/summer/beach/sand/toys None of those directories need to exist ahead of time. Without the -s, you would get a “No such file or directory” error when the command hits the first directory that isn’t already a part of your directory structure. If you recursively created your new directory structure in error, you can undo the operation with rmdir -p. This command will remove the entire directory structure we would have created with the command above provided the directories are still empty. $ rmdir -p fun/summer/beach/sand/toys Recursion options Here’s a table showing the commands and options discussed above. command option comment chown -R cp -R or -r grep -R or -r -R follows symlinks find NA recurses by default ls -R mkdir -p p = parent dirs rm -R or -r zip -r Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe