Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to find files on Linux and make it easy to find them again

How-To
Aug 19, 20224 mins
Linux

The cd command is easy to use, but adding a couple tricks to your toolbox can make moving around, finding, and remembering the locations of files and directories simpler.

data files
Credit: Dell

The cd command makes it easy to switch to another directory on Liniux, but only if you know where you’re heading. In this post, I discuss a couple of tricks for moving between known locations and provide a script for finding and “remembering” files or locations that you might want to reuse.

One of the easiest things to do with the cd command is return to your home directory regardless of where you are sitting in the file system at the moment. Just type cd by itself, and you’ll be back in your home directory. Typing cd ~ will do the same thing, though adding the tilde won’t get you there any faster.

The cd .. command will back you up one directory while cd . does nothing at all. The logic of the latter is, after all, “move me into the directory that I’m already sitting in”. The cd – command, on the other hand, will take you back to whatever directory you were located in previously. It also confirms your move before sending the next command line prompt.

$ cd /tmp   # move to /tmp
$ cd ~      # move to home directory
$ cd –      # move back to previous directory   
/tmp
$

Using the find command

The find command is used to locate directories or files. To find a directory or file, use a command like one of these:

$ find /usr -type d -name bin -ls          # find a directory
$ find /usr -type f -name zcat -print      # find a file

The -type specifications aren’t needed unless you want to be sure that you’ll see only files or directories. The difference between -ls and –print is that -ls shows both the names of the located files or directories and their details (permissions, sizes, etc.) while the -print shows only the names.

If you’re searching for files or directories outside of your home directory, adding 2>/dev/null to the end of the command will mean that you won’t run into “Permission denied” errors for those files or directories you don’t have read access to.

$ find /usr -type d -name bin -ls 2>/dev/null
$ find /usr -type f -name zcat -print 2>/dev/null

For files or directories that you expect to use often, creating symbolic links that point to them can save you from having to remember their locations or find them again and again.

Creating a symbolic link is easy. Find the file and then use the ln -s command to create the symbolic link as in this example:

$ find / -type f -name little_endian.h 2>/dev/null
/usr/include/linux/byteorder/little_endian.h
$ ln -s /usr/include/linux/byteorder/little_endian.h .

The ln -s command above would create the symbolic link in your current location. You will probably want this to be your home directory.

Saving locations with a script

The script below will find a file or directory for you and, if you want, create a symbolic link that points to it. In fact, if it finds multiple files or directories that match your specification, it will offer to create a symbolic link to each of them and will add a digit to the end of each link after the first to differentiate them. If you answer “n” to the prompt asking whether to create a symlink, it will not create any symbolic links, but will still show you the files or directories that it found.

#!/bin/bash

echo -n "What are you looking for?> "
read name

echo -n "File or directory? [f/d]> "
read type

echo -n "Starting point?> "
read start

locs=`find $start -type $type -name $name -ls 2>/dev/null | awk '{print $NF}'`

num=""

for loc in $locs
do
    echo $loc
    echo -n "Create a symlink? [y/n]> "
    read ans
    if [ $ans == "y" ]; then
        if [ -f $name$num ]; then		# remove
            rm $name$num			# old
        fi					# links
        echo "ln -s $loc $name$num"
        ln -s $loc $name$num
        ((num++))
    fi
done

Note: The script as shown will delete existing files or links with the same names as those it would create. Delete the lines with the comments if you do not want this to happen. If you do, you can rename the conflicting files and then run the script again.

Wrap-Up

Moving around in the Linux file system is easy, but with a few commands, you can make it easier to find and remember files and directories that you are likely to need often.

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.

More from this author