How to toggle between two directories in linux’s command line

Lori Kaufman
How to toggle between two directories in linux’s command lineLori Kaufman

Lori Kaufman is a technology expert with 25 years of experience. She’s been a senior technical writer, worked as a programmer, and has even run her own multi-location business. Read more.

How to toggle between two directories in linux’s command line

If you want to create a directory containing several subdirectories, or a directory tree, using the command line in Linux, generally you have to use the mkdir command several times. However, there is a faster way to do this.

Let’s say we’ve created a directory called htg, and want to create four subdirectories within it. In a normal situation, we’d use the mkdir command to create the htg directory. Then, we’d need the cd command to change to the new htg directory and, finally, we we’d use the mkdir command again four times to create the four subdirectories.

How to toggle between two directories in linux’s command line

This can all be combined into one command, and we’ll show you how.

To create a new directory with multiple subdirectories you only need to type the following command at the prompt and press Enter (obviously, change the directory names to what you want).

The -p flag tells the mkdir command to create the main directory first if it doesn’t already exist (htg, in our case). The words in the brackets are part of the “brace expansion list”. Each of the items in the brace expansion list is appended separately to the preceding path (htg/).

How to toggle between two directories in linux’s command line

For example, the above command is expanded into htg/articles, htg/images, htg/note, htg/done, all four of the subdirectories being created under the htg directory. As you can see in the screenshot below, it worked.

How to toggle between two directories in linux’s command line

You can also use a brace expansion list in the mkdir command if you’re creating subdirectories in a directory that already exists, as shown below. In this example, the htg directory already exists so the subdirectories are simply added under that directory.

How to toggle between two directories in linux’s command line

You can also nest brace expansion lists in the mkdir command. For example, in the articles subdirectory under the htg directory, we want to create two subdirectories called new and rewritten. So, we type the following command at the prompt and press Enter.

You can also use the full path if you want, as I’ve done in the example below:

The four subdirectories are created under the htg directory and then the two subdirectories, new and rewrites, are created under the articles subdirectory.

How to toggle between two directories in linux’s command line

It’s that easy. You can also combine the mkdir command with the cd command to make a directory and change to it with one command.

  • › How to Add Images to Questions in Google Forms
  • › How to Delete Your Photos on Facebook
  • › Razer Thinks You Want a PC Gaming Headset That Vibrates
  • › How to Get a Link to a Photo or Video on Instagram
  • › The Fix for Slow AMD Processors on Windows 11 Is Here

How to toggle between two directories in linux’s command line Lori Kaufman
Lori Kaufman is a technology expert with 25 years of experience. She’s been a senior technical writer, worked as a programmer, and has even run her own multi-location business.
Read Full Bio »

How to toggle between two directories in linux’s command line

If you’re working with files in the Terminal and switching back and forth between two directories, we’re about to save you some time. There is a shortcut command that allows you to toggle between two directories on the command line.

First, as usual, change to your first directory by typing the following command at the prompt and pressing Enter.

RELATED: Use Tab Completion to Type Commands Faster on Any Operating System

Where First/Directory/ is name of the directory you want to change to.

You can also use tab completion to enter the directory name by typing a few characters in the name and then pressing Tab to finish entering the name.

How to toggle between two directories in linux’s command line

Now, we want to switch to a second directory, so we use the cd command and tab completion again.

How to toggle between two directories in linux’s command line

Now, instead of typing the first directory name to go back to that directory, you can type the following command at the prompt and press Enter.

That’s simply the cd command followed by a space and then a dash. The name of the directory you’re toggling to displays and then you’re taken to that directory.

How to toggle between two directories in linux’s command line

To toggle back to the second directory again, use the cd – command again.

How to toggle between two directories in linux’s command line

You can also press the up arrow key once to access the last command used from the command line history, which in this case was cd – . So, at this point, to toggle back and forth between these two directories, all you do it press the up arrow key and Enter. That’s quicker than typing out the path each time, even when you use tab completion.

In an earlier article, we reviewed 9 best file comparison and difference (Diff) tools for Linux and in this article, we will describe how to find the difference between two directories in Linux.

Normally, to compare two files in Linux, we use the diff – a simple and original Unix command-line tool that shows you the difference between two computer files; compares files line by line and it is easy to use, comes with pre-installed on most if not all Linux distributions.

The question is how do we get the difference between two directories in Linux? Here, we want to know what files/subdirectories are common in the two directories, those that are present in one directory but not in the other.

The conventional syntax for running diff is as follows:

By default, its output is ordered alphabetically by file/subdirectory name as shown in the screenshot below. In this command, the -q switch tells diff to report only when files differ.

Again diff doesn’t go into the subdirectories, but we can use the -r switch to read the subdirectories as well like this.

Using Meld Visual Diff and Merge Tool

There is a cool graphical option called meld (a visual diff and merge tool for the GNOME Desktop) for those who enjoy using the mouse, you can install it as follows.

Once you have installed it, search for “meld” in the Ubuntu Dash or Linux Mint Menu, in Activities Overview in Fedora or CentOS desktop and launch it.

You will see the Meld interface below, where you can choose file or directory comparison as well as version control view. Click on directory comparison and move to the next interface.

How to toggle between two directories in linux’s command line

Select the directories you want to compare, note that you can add a third directory by checking the option “3-way Comparison”.

How to toggle between two directories in linux’s command line

Once you selected the directories, click on “Compare”.

How to toggle between two directories in linux’s command line

In this article, we described how to find the difference between two directories in Linux. If you know any other commandline or gui way don’t forget to share your thoughts to this article via the comment section below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Related Posts

How to toggle between two directories in linux’s command line

How to toggle between two directories in linux’s command line

How to toggle between two directories in linux’s command line

How to toggle between two directories in linux’s command line

How to toggle between two directories in linux’s command line

How to toggle between two directories in linux’s command line

7 thoughts on “How to Find Difference Between Two Directories Using Diff and Meld Tools”

There is a great tool called Beyond Compare here .

It is a GUI tool with versions for Linux, Mac & Windows. It can also connect to a remote server using sftp.

Many thanks for sharing, we will check it out.

Nevermind, we do have a QT version of Meld. It’s called KDiff3

KDE has Kompare but only works for files…

Can i use diff command with remote host , assume second folder on B server than how to use diff command.

You can find difference between two files, one local and another remote, like this:

May be the best tool, in this case, is rsync.

Got something to say? Join the discussion. Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most of the intermediate and almost all advanced users prefer CLI over GUI mode, because there are plethora of command line tricks to make things much easier and faster. Today, I’d like to share one simple tip. This simple bash trick can help you to automatically switch to a directory without using cd command. All you need to do is just enter the path of the directory in the Terminal, and you will be landed in that particular directory. This could be useful in scripting and for those who use command line a lot at work. Read on.

Switch to a directory without using cd command

Let us say you wanted to go to the directory Downloads. You don’t need to type “cd Downloads” every time to go that directory. Instead, just type “Downloads” in the Terminal. Sounds cool, yeah? Indeed!

Let us simply type Downloads in the Terminal, and see what happens.

You should type “cd Downloads” to go the Downloads directory, right? But, wait! You don’t have to use cd command anymore. All you have to do is add a single line in the bashrc file.

To do so, edit your bashrc file:

And, add the following line at the end:

Update the changes made in the bashrc file with the following command:

All done! You can now go to any directory without using cd command. Just enter the path of the directory like below. Bash will automatically prepend cd when entering just the path of any directory in the Terminal.

How to toggle between two directories in linux’s command line

Look at the above screenshot. Did I use “cd” command? Nope. I just entered the Directory name and automatically went to the respective directory.

Another advantage of this method is it not only helps you switch to successive directories, but also go to any different paths.

For example, let us say you are currently in /home/sk/Downloads directory. You want to go to /var/mail/ . How would you do that? Simple. Just type the full path of the respective directory i.e /var/mail/ in our case. Again you don’t need to mention ‘cd’ command. Look at the following screenshot.

How to toggle between two directories in linux’s command line

Similarly, you can go to any location without having to use ‘cd’ command.

It may not be helpful for you all the time. Because cd is just two letter command. It won’t be long to type just two characters. However, this is a just proof that shows we can do some cool stuff in Linux.

I am new to Linux and Ubuntu and have tried changing to folders/directories with some difficulty.

Could someone explain why the following commands failed to change to the desired target folder/directory?

4 Answers 4

The filesystem is GNU/Linux is like a tree, except that the root is on top. 🙂 So you have structure like:

If you want to move inside the tree, one option is to use relative paths. If you are in /home/sharon , then typing cd Downloads will work, because Downloads is an immediate child of your current directory. If you are in the subfolder Documents and want to change directory ( cd ) to Downloads , you have to go up ( .. ) and then to Downloads . So the correct command would be cd ../Downloads .

You could also enter an absolute path. So the Downloads folder is a subfolder of sharon which is a subfolder of home which is … (you get the idea :-)) So you can also enter cd /home/sharon/Downloads wherever you are in the filesystem.

always refers to the home directory of the current user ( /home/sharon in your case). If you enter cd

/Downloads you’ll land in your Downloads folder.

. refers to the current directory, so cd ./Downloads is roughly equivalent to cd Downloads .

.. means “parent directory”.

/ at the beginning of file path refers to the root directory.

The next nice thing is tab expansion. If you enter cd

/Dow Tab (last is pressing Tabulator key), the bash automatically expands it to cd

As the others said GNU/Linux is case sensitive. So it makes a difference if you enter Home , hOme or home . Furthermore I hope that you see now that there is a difference between /home and home . The first is adressed absolute while the last is relative to your current directory.

Having migrated to Linux from Windows, I would like to find an alternative software to Winmerge or rather learn command line tools to compare and sync two folders on Linux. I would be grateful if you could tell me how to do the following tasks on the command line. (I have studied diff and rsync, but I still need some help.)

We have two folders: “/home/user/A” and “/home/user/B”

Folder A is the place where regular files and folders are saved and folder B is a backup folder that serves as a complete mirror of folder A. (Nothing is directly saved or modified by the user in folder B.)

My questions are:

How to list files that exist only in folder B? (E.g. the ones deleted from folder A since the last synchronization.)

How to copy files that exist in only folder B back into folder A?

How to list files that exist in both folders but have different timestamps or sizes? (The ones that have been modified in folder A since last synronization. I would like to avoid using checksums, because there are tens of thousands of files and it’d make the process too slow.)

How to make an exact copy of folder A into folder B? I mean, copy everything from folder A into folder B that exists only in folder A and delete everything from folder B that exists only in folder B, but without touching the files that are the same in both folders.

8 Answers 8

This puts folder A into folder B:

If you want the contents of folders A and B to be the same, put /home/user/A/ (with the slash) as the source. This takes not the folder A but all of it’s content and puts it into folder B. Like this:

  • -a Do the sync preserving all filesystem attributes
  • -v run verbosely
  • -u only copy files with a newer modification time (or size difference if the times are equal)
  • –delete delete the files in target folder that do not exist in the source

How to toggle between two directories in linux’s command line

How to toggle between two directories in linux’s command line

You could unison tool developed by Benjamin Pierce at U Penn.

Let us assume you have two directories,

/home/user/Documents/dirA/ and /home/user/Documents/dirB/

To synchronize these two, you may use:

$ unison -ui text /home/user/Documents/dirA/ /home/user/Documents/dirB/

In output, unison will display each and every directory and file that is different in the two directories you have asked to sync. It will recommend to additively synchronize (replicate missing file in both locations) on the initial run, then create and maintain a synchronization tree on your machine, and on subsequent runs it will implement true synchronization (i.e., if you delete a file from . /dirA , it will get deleted from . /dirB as well. You can also compare each and every change and optionally choose to forward or reverse synchronize between the two directories.

Optionally, to launch graphical interface, simply remove the -ui text option from your command, although I find the cli simpler and faster to use.

The answer from TuxForLife is pretty good, but I strongly suggest you use -c when syncing locally. You can argue that it’s not worth the time/network penalty to do it for remote syncs, but it is totally worth it for local files because the speed is so great.

This shows how having the same size and time stamps can fail you.

This is what I’m using for backing up personal files, where I don’t care about everything covered by -a , and want more useful information printed.

-r, –recursive
This tells rsync to copy directories recursively.

-t, –times
This tells rsync to transfer modification times along with the files and update them on the remote system.

-u, –update
This forces rsync to skip any files which exist on the destination and have a modified time that is newer than the source file. (If an existing destination file has a modification time equal to the source file’s, it will be updated if the sizes are different.)

This tells rsync to delete extraneous files from the receiving side (ones that aren’t on the sending side), but only for the directories that are being synchronized.

This option lets you have fine-grained control over the information output you want to see.

From rsync –info=help

While less explicit, this is seemingly equivalent and shorter:

-v, –verbose
A single -v will give you information about what files are being transferred and a brief summary at the end [stats1].

Need to navigate through your files in the Linux terminal? You need the cd command – here’s how to use it.

System navigation is one of the most basic tasks that a user can perform on an operating system. While many file managers are available that allow you to switch directories and navigate your disk drives in a graphical way, doing the same using the command line can provide you better control over your system.

Luckily, Linux provides you with a command known as cd, which allows you to easily change the current working directory on your terminal.

Here’s how you can use the cd command on Linux, the only utility that you’ll ever need to traverse through directories on your system storage.

How To Use the cd Command in Linux

The cd command stands for Change Directory. One of the most fundamental commands in Linux, cd has become a global command for switching directories. Some other implementations of the command such as chdir, used in MS-DOS systems, are also present.

Basic Syntax

The basic syntax of the cd command is:

. where options are the arguments passed with the command and path is the absolute or relative path to the directory.

Absolute and Relative Pathnames

Before you can use the cd command to its full extent, you must know the difference between absolute and relative pathnames. The absolute pathname is the complete path to the directory, starting from the / (root) folder.

On the other hand, the relative pathname is derived from the current working directory. You can move up and down the directory tree efficiently with the cd command if you’re well-versed on relative paths.

For example, if your current working directory is /home, and you want to change the directory to /Desktop. Then, using the absolute pathname:

On the other hand, if you want to switch to the /Desktop directory using a relative path, all you have to type is:

Cd keeps track of what directories are above and below the current directory, to allow a user to quickly shift to other directories without having to type the whole pathname.

If you don’t know the exact name of the subfolder that you want to switch to, use the ls command to list down every directory in that folder.

Switch to the Home Directory

On a Linux system, the /home directory is a special directory reserved for a user’s personal files, programs, and subfolders. When you log in to your system, the home directory is set as the current working directory by default.

The home directory has a special character assigned to it—the

(tilde) character. Instead of specifying the complete path to your home directory (/home/username), you can just pass the

character with the cd command to change the current working directory to /home.

Similarly, you can navigate to other user’s home directory as follows.

In the previous section, we switched the present working directory to /Desktop. In the command, you can use the

character to denote the /home directory and strip down the command to half of its size.

Navigate to the Root Folder

Just like the home directory, the / character denotes the /root directory on a Linux-based operating system. To switch to the root folder anytime:

Change to the Previous Working Directory

If you’re working with multiple directories at a time, you can easily switch back and forth to the previous working directory using the – (hyphen) character.

For example, if the current working directory is /home and you switch to the /root directory. Then, /root will become the current working directory, and /home will be the previous working directory.

Typing the following command will take you to the previous directory i.e. /home.

Also, issuing the cd command followed by a space character will take the user to the previous working directory.

Switch to the Parent Directory

The directory which consists of one or more sub-directories is known as the parent directory. Simply put, if you have /Desktop and /Downloads folders in your /home directory, then the /home directory will be the parent directory for /Desktop and /Downloads.

The .. and . characters stand for the parent directory and the current directory respectively.

Use the double dots character (..) to switch to the parent directory.

The aforementioned command will take you one level up the directory tree. You can also pass additional .. characters to move further up the directory tree.

To move two levels above your current working directory:

You can also pass a specific directory one level above the present working directory.

Change to a Directory Name With Spaces

Not every folder on your system will have a one-word name. Some of them might include the space character. For example, /home/username/Important Documents.

In such situations, specifying the directory name will return an error.

To switch to directories that have spaces in the name, wrap the pathname in quotes as follows. Note that you can use both single and double quotes in the command.

Alternatively, you can use the backward slash (\) character to escape the spaces.

System Navigation Through the Command Line

The Linux terminal is a powerful text interface for controlling the working of your computer. You can carry out almost any task using the command line. Changing directories, copying files and folders remotely, listing file information, editing configuration files, you name it. There’s a command for everything that you want to do on Linux.

However, there’s a drawback to having so many commands. Not everyone can learn and memorize the commands, not to mention the hefty amount that’s available to use in the first place. But as a beginner Linux user, you won’t be using all of the utilities that are available on your system.

Instead, learning some basic commands that will help you in performing fundamental operations is more than enough to get started with the operating system.

Want to gain familiarity with Linux? Start with these basic Linux commands to learn standard computing tasks.

How to toggle between two directories in linux’s command line

Deepesh is the Junior Editor for Linux at MUO. He writes informational guides on Linux, aiming to provide a blissful experience to all newcomers. Not sure about movies, but if you want to talk about technology, he’s your guy. In his free time, you can find him reading books, listening to different music genres, or playing his guitar.

Subscribe to our newsletter

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!

How to toggle between two directories in linux’s command line

To copy files or directories in Unix-based operating systems (Linux and MacOS), you use the cp command.

The cp command is a relatively simple command, but its behavior changes slightly depending on the inputs (files vs directories) and the options you pass to it.

To view the documentation or manual for the cp command, run man cp at your terminal:

The basic form of this command takes an input source (or sources) that you want to copy (files or directories) and a destination to copy the files or directories to:

How to copy a file to the current directory

To copy a file, pass the file you want to copy and the path of where you want to copy the file to.

If you have a file named a.txt , and you want a copy of that file named b.txt :

By default the cp command uses your current directory as the path.

How to copy a file to another directory

To copy a file to a directory that is different from your current directory, you just need to pass the path of the other directory as the destination:

After the cp command, the previously empty directory-1 now contains the file a.txt .

By default the copied file receives the name of the original file, but you can also optionally pass a file name as well:

How to copy multiple files to a directory

To copy more than one file at a time you can pass multiple input sources and a directory as destination:

Here the two input sources ( first.txt and second.txt ) were both copied to the directory directory-1 .

How to copy a directory to another directory

If you try to pass a directory as the input source, you get this error:

To copy a directory, you need to add the -r (or -R ) flag—which is shorthand for –recursive :

Here directory-1 containing the file a.txt is copied to a new directory called directory-2 —which now also contains the file a.txt .

How to copy the entire directory vs the contents of the directory

There is an interesting edge case when you copy a directory: if the destination directory already exists, you can choose whether to copy the contents of the directory or the entire directory by adding or removing a trailing / from your input.

Here’s the description from the -R option of the man page:

If you want to copy just the contents of the directory into another directory, add a trailing / to your input.

If you want to copy the contents of the directory and the directory folder itself into another directory, don’t add a trailing / :

Here you can see that because directory-2 already exists—and the input source didn’t have a trailing / —both the contents of directory-1 and the directory itself was copied into the destination.

How to prevent overwriting files with cp

By default, the cp command will overwrite existing files:

There are two ways to prevent this.

The interactive flag

To be prompted when an overwrite is about to occur, you can add the -i or –interactive flag:

The no-clobber flag

Or, to prevent overwrites without being prompted, you can add the -n or –no-clobber flag:

Here you can see that thanks to the -n flag the contents of directory-1/a.txt were not overwritten.

Other options

There are many other useful options to pass to the cp command: like -v for “verbose” output or -f for “force.”

I highly encourage you to read through the man page for all of the other useful options.

If you liked this tutorial, I also talk about topics like this on Twitter, and write about them on my site.

A simple web developer who likes helping others learn how to program.

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons – all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives and help pay for servers, services, and staff.

How to toggle between two directories in linux’s command lineandy.brandon50 (CC BY-SA 2.0)

There are many ways to change text on the Linux command line from lowercase to uppercase and vice versa. In fact, you have an impressive set of commands to choose from. This post examines some of the best commands for the job and how you can get them to do just what you want.

Using tr

The tr (translate) command is one of the easiest to use on the command line or within a script. If you have a string that you want to be sure is in uppercase, you just pass it through a tr command like this:

Below is an example of using this kind of command in a script when you want to be sure that all of the text that is added to a file is in uppercase for consistency:

Switching the order to [:upper:] [:lower:] would have the opposite effect, putting all the department names in lowercase:

Similarly, you could use the sed command’s A-Z and a-z strings to accomplish the same thing:

As you undoubtedly suspect, reversing the order of the a-z and A-Z strings will have the opposite effect, turning the text to all lowercase.

Using awk

The awk command lets you do the same thing with its toupper and tolower options. The command in the script shown in the previous example could be done this way instead:

The reverse (switching to lowercase) would look like this:

Using sed

The sed (stream editor) command also does a great job of switching between upper- and lowercase. This command would have the same effect as the first of the two shown above.

Switching from uppercase to lowercase would simply involve replacing the U near the end of the line with an L.

Manipulating text in a file

Both awk and sed also allow you to change the case of text for entire files. So, you just found out your boss wanted those department names in all lowercase? No problem. Just run a command like this with the file name provided:

If you want to overwrite the depts file, instead of just displaying its contents in lowercase, you would need to do something like this:

Making the change with sed, however, you can avoid that last step because sed can edit a file “in place” as shown here, leaving the file intact, but the text in all lowercase:

Capitalizing first letters only

To capitalize only the first letters of words in a string, you can do something like this:

That command will ensure that first letters are capitalized, but won’t change the rest of the letters.

Making sure only first letters are uppercase

It’s a little more challenging when you want to change text so that only first letters are in uppercase. Say you’re manipulating a list of staff members’ names and you want them to be formatted in the normal Firstname Lastname manner.

with sed

You could use a considerably more complex sed command to ensure this result:

with python

If you have python loaded, you can run a command like this that also formats text so that only the first letters of each word are capitalized and the command may be a little easier to parse than the sed command shown above:

There are many ways to change the formatting of text between upper- and lowercase. Which works best depends in part of whether you’re manipulating a single string or an entire file and how you want the end result to look.

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.