0

When I move files with a GUI file manager, I navigate to the source and destination directory in 2 different windows, and then drag and drop. Is there a way to do this in BASH (move a file to a directory, but be able to navigate to the new directory rather than needing to know the full path right away to put in the argument)

I know how to use commands like mv, and cp, but both of these take one full path as the argument, and I want to be able to navigate to the new destination separate from navigating to the source file... if that makes sense

3
  • I'm not sure if I understood correctly what you want to achieve. You can use two different terminal windows(or tabs) , or VTs if you are in console mode. Then you can move and/or copy files with mv and cp as you said, and connect to a directory with cd and list the files with ls.
    – NickTux
    Sep 18, 2013 at 22:42
  • 1
    You have a current directory and you navigate using .. as previous directory and tab to complete. Eg mv test* ../../otherdir/private/ will move all files whose names start with test to the private directory of the otherdir directory of the second parent of the source.
    – Sylwester
    Sep 18, 2013 at 23:31
  • You can use the Tab key to complete paths and "navigate" directories while building the arguments to cp and mv. For example: type cp /ho and press the Tab key. Sep 19, 2013 at 17:47

7 Answers 7

2

I'm not sure if I understood, but you want a console file manager so you could navigate your folders in two panels. You could try with Midnight commander.

Install it with:

sudo apt-get install mc
1
  • Great answer. this is what i do now, but i really want to try to move into just using the terminal. I feel like knowledge like this will be especially handy when im ssh'd onto machines
    – chasemc67
    Oct 2, 2013 at 15:38
2

There are two commands to allow you to easily move back and forth between directories:

pushd
popd

For an example, navigate to the first directory with cd:

cd /home/aperson/more/path/to/a/very/long/and/annoying/to/type/

Then use pushd to move to the other directory:

pushd /home/aperson/more/path/to/a/different/very/long/and/annoying/to/type/

Now when you call popd again you will be back in the first directory.

This just helps with the navigation though, you will still need to do your mv or cp separately.

Depending on how much you need to do it may be worth assigning your paths to variables so you don't have to type the full path out:

DIR1="/home/aperson/a/very/long/and/annoying/to/type/"
DIR2="/home/aperson/a/different/very/long/and/annoying/to/type/"

Now you can use $DIR1 and $DIR2 to refer to the directories i.e.

cp somestuff.stuff $DIR1
cd $DIR1
mv somestuff.stuff $DIR2
cd $DIR2
0
1

This is not exactly navigation, but I am guessing what you're looking for is TAB AUTO COMPLETION.

This will allow you to get the full part of any directory without having to know it.

Here's a great tutorial for this.

1

I don't understand why nobody mentioned two easy ways to achieve what op asks.

First way:

cd -  #goes back to previous directory

Second way:

cd $OLDPWD

For more info check out following link https://superuser.com/questions/113219/go-back-to-previous-directory-in-shell

EDIT: I found really a nice .bashrc function in following link by Adam Katz which is a must for easy navigation in bash shell: https://unix.stackexchange.com/questions/31161/quick-directory-navigation-in-the-terminal

I tried it, it works well.

3
  • +1 for $OLDPWD in particular Oct 12, 2017 at 7:33
  • 'cd --' does NOT take you back two levels. '--' is a generic GNU convention to indicate that there are no further options, so 'cd --' is interpreted exactly as 'cd' (without any options), which takes you to your home directory.
    – Popup
    May 18, 2018 at 10:52
  • Thank you for clarification. I don't know how I reached that misconception. I should have been cautious before posting an anwer. I fixed it now.
    – kenn
    May 18, 2018 at 12:14
0

You could use the tab feature that comes with ubuntu's terminal (ctrl + shift + t). This way you could search for your destination directory in the second tab using the cd and ls commands. then once in the desired directory you could use the pwd command (print working directory) to get the full path to said directory. Once you have that, copy and paste the path over to your first terminal tab where you are executing your cp or mv command.

enter image description here

If you are not familiar with cd and ls, here is a great little tutorial for some of the basic bash commands.

The Command Line Crash Course

I hope this is of some use to you.

0

Another option, that's more akin to the GUI 'cut and paste' idea.

cd /long/path/to/wherever/the/file/is/now
mv file_to_move /tmp/
cd /destination/directory/somewhere/else/
mv /tmp/file_to_move ./

But really, it involves much more typing than a simple single cp <source> <destination>

0

Autojump with Tab completions

This doesn't exactly answer your question, BUT if you've been to that particular folder before, you can simply use the autojump command/program to allow you to quickly jump between folders.

j Pictures and i'm in the pictures folder

j AnotherFolder and then instantly, I am transported to that folder.

Best part of about autojump is that it respects tab completions, and cycles through folders with the similar names.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .