Americas

  • United States
sandra_henrystocker
Unix Dweeb

Linux: To recurse or not

How-To
Feb 17, 20185 mins
LinuxRed HatUbuntu

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.

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  

 

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.