Linux: To recurse or not

Linux and recursion are on very good speaking terms. In fact, a number of Linux command 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
bin  etc  games  include  lib  man  sbin  share  src


python2.7  python3.6

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 in 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 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 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 like “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

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.

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  

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Leave a Reply

Your email address will not be published. Required fields are marked *