0

To explain my question, here is the dumb way of how I'm doing things:

cp fileFromMyFriend.txt ~/my_first_subfolder/my_second_subfolder/more_subfolders
cd ~/my_first_subfolder/my_second_subfolder/more_subfolders

What is a shortcut to doing those two lines in one? I'm not looking for a concatenation symbol such as & or ;

Rather I'm trying to avoid retyping my very long folder path.

(And I already looked through the cp man page and Googled this question with different wording before coming here)

FriskySaga
  • 111
  • 4
  • See [Supply the same filename as argument to two commands](https://unix.stackexchange.com/questions/184235/supply-the-same-filename-as-argument-to-two-commands) – steeldriver Sep 15 '19 at 23:56
  • if the only issue is typing the very long folder path then you can save it in a variable. – Pacifist Sep 16 '19 at 00:13
  • See also [How to use arguments from previous command?](https://stackoverflow.com/questions/4009412/how-to-use-arguments-from-previous-command). I **never** remember to do any of this, and just retype, or use the mouse to copy-paste, or use a variable (and `basename` or `dirname` if required). – cas Sep 16 '19 at 05:58
  • @steeldriver Ah I didn't realize something like that existed, thanks. I guess I was narrowing my thinking too much. – FriskySaga Sep 16 '19 at 16:07

3 Answers3

1

If this is for interactive use: type and run the first command normally. Then type cd Space M-. (you can type M-. either as Esc . or as Alt+.). The keyboard shortcut M-. (yank-last-arg) inserts the last word of the previous command.

If this is for scripting: put the directory name in a variable. Don't forget double quotes when you use the variable.

target_dir=~/my_first_subfolder/my_second_subfolder/more_subfolders
cp fileFromMyFriend.txt "$target_dir"
cd "$target_dir"

If you find that you use the same sequence of commands a lot, define a function. The bash function below calls cp and treats the last argument as a directory to change to. If the last argument is not a directory or a symbolic link to a directory, it changes to the directory containing the last argument. This works for most ways to use cp, but not with the -t option to GNU cp.

cp_cd () {
  cp "$@"
  if [ -d "${!#}/." ]; then
    cd -- "${!#}"
  else
    cd -- "$(dirname -- "${!#}")"
  fi
}
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
0

I'm going to just answer this question of yours: Rather I'm trying to avoid retyping my very long folder path.

If that's what you are trying to do, you can just do this:

cp fileFromMyFriend.txt ~/my_first_subfolder/my_second_subfolder/more_subfolders
cd !$
SparedWhisle
  • 3,588
  • 4
  • 22
  • 35
  • Marking this as the solution since this is the simplest answer for my case. I only need the last argument. I didn't know "!$" is a shortcut to using the last argument from the last command. If I had known something like that existed, I would've found this answer when searching on Google: https://unix.stackexchange.com/questions/88642/what-does-mean/88643 – FriskySaga Sep 16 '19 at 16:05
0

In order to work with the latest file you have used, you can run the following

cd $_ # directory case
vim $_ # file case

alessiosavi
  • 347
  • 2
  • 9