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. 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 Saving locations with symbolic links 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. Related content how-to Compressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. By Sandra Henry-Stocker May 13, 2024 4 mins Linux opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to The logic of && and || on Linux These AND and OR equivalents can be used in scripts to determine next actions. By Sandra Henry-Stocker May 02, 2024 4 mins Linux how-to Using the apropos command on Linux By Sandra Henry-Stocker Apr 24, 2024 3 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe