2

I have to move files from one folder to another but they reside in totally different paths. The destination path is so long and complicated that I am not able to remember it. Is there any way to go first to destination directory then store the path in some variable lets suppose X and then go to source directory and write a command like that:

 mv * $X
  • 2
    you know you can simply drag a directory over the terminal window to use it? – Jacob Vlijm Nov 11 '14 at 19:15
  • No I did not know about that, thank you :). – Al Bundy Nov 11 '14 at 19:20
7

Try:

cd /your/long/path
X=$(pwd) 

The command pwd prints the current directory; with the command X=$(pwd) you are storing the output of the command in the variable(1) X (no spaces around the =!)

cd /the/other/dir/
mv * "$X"

Now you are using the value of the variable X, using $X (read $ as "the value of"). The " that are around the $X are needed if the directory name contains spaces or other special characters.

If you are unsure about what is in X, you can check its content with

echo "$X" 

(1) in the bash (and other) shells they are called parameters. See for example here for a simple tutorial, or here.

| improve this answer | |
  • Is there any restriction on number of variables and their names ? – Al Bundy Nov 11 '14 at 19:09
  • No restriction on (reasonable) number, and few on the name. Stick with letters, numbers (but don't start with a number) and underscores and you are safe. Note that x and X are different --- the shell is case sensitive. – Rmano Nov 11 '14 at 19:14
4

Assuming your shell is bash, you can use

pushd /your/long/directory
popd
mv ./* "$OLDPWD"

$OLDPWD is set by bash when you change directories.

| improve this answer | |
2

Suppose you are now in the directory with files you want to move, use

cd target_dir
cd -

to store your target_dir in $OLDPWD variable, and move back to your original directory.

Now, you may use

mv * "$OLDPWD"

to move your files to your target directory.


Notes:

$PWD(Print Working Directory) always remebers current working directory.

And $OLDPWD always remebers last working directory.

By the way, use TAB key to auto-complete your path when typing your path.

| improve this answer | |
1

Some time ago I've tried to create a more elaborate version of X=$(pwd) and mv * $X. The result was something that I call a "bashPortalGun". Here it is:

https://github.com/tyukiand/bashPortalGun

These scripts transform your shell prompt into something like this:

me@host [orange -> there] here$ 

and allow you to do everything you would expect from a "portal gun", applied to the context of the bash-shell:

me@host ~$ cd /very/long/path
me@host /very/long/path$ pOrange # create orange portal
me@host [orange -> .portal.closed] /very/long/path$ cd /some/other/directory
me@host /some/other/directory$ pBlue # create blue portal
me@host [blue -> path] /some/other/directory$ mv file.txt [orange] # move stuff through portal

In my experience, it makes the usage of mv and cd more comfortable, especially when working with multiple shells. Maybe you find it useful, maybe you just consider it cool to have a portal gun in your bash :)

Motivated test subjects are welcome. Constructive feedback will be highly appreciated.

| improve this answer | |
0

It's perhaps easier to just create temporary symbolic links instead of variables.

It's also not all that "un-handy" to use "tab" auto-completion/suggestions. But may depend on one's individual .bashrc, I'm not sure.

| improve this answer | |
-1

Why do not use variables for src and dest-Path? Here a little script:

IFS='
'
SRC="/PATH/TO/SOURCE-PATH"
DST="/PATH/TO/DEST-PATH"
for i in `ls $SRC` 
do
mv $SRC/$i $DST/.
done

the IFS is important, if you have filenames with spaces. Without IFS the for-loop do in some environments a line-break on this spaces. (SOURCE)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.