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
Try:
The command
pwd
prints the current directory; with the commandX=$(pwd)
you are storing the output of the command in the variable(1)X
(no spaces around the=
!)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
(1) in the
bash
(and other) shells they are called parameters. See for example here for a simple tutorial, or here.Assuming your shell is bash, you can use
$OLDPWD is set by bash when you change directories.
Some time ago I've tried to create a more elaborate version of
X=$(pwd)
andmv * $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:
and allow you to do everything you would expect from a "portal gun", applied to the context of the bash-shell:
In my experience, it makes the usage of
mv
andcd
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.
Suppose you are now in the directory with files you want to move, use
to store your target_dir in
$OLDPWD
variable, and move back to your original directory.Now, you may use
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.
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.
Why do not use variables for src and dest-Path? Here a little script:
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)