0

This question is not about the cp command.

I don't have a mouse nor a GUI installed on an Ubuntu server, but I would like to save some commands in a file so I can reuse them later.

Is my only choice to retype them in a file (using Vi/Nano/whatever), or is there a way to copy them?

Jawa
  • 3,679
DrakaSAN
  • 867

3 Answers3

1

There is of course also bash's history mechanism. If enabled, bash will keep a file ~/.bash_history which contains all command lines that you entered, up to a maximum number of entries.

There's also the fc command to browse the history without looking through the file, for instance fc -l 1 | fgrep echo to list all history lines containing echo anywhere.

All of this of course can be configured:

  • HISTFILE sets the name of the history file, instead of ~/bash_history
  • HISTSIZE sets the maximum number of entries that are kept in the history (defaults to 500).
  • HISTCONTROL allows some fine tuning about what is kept in the history and what not. By setting HISTCONTROL=ignoreboth duplicate entries are kept only once, and you can prevent single command lines from showing up in the history by prepending a space (e.g.  ls instead of ls).

I like to keep HISTSIZE as large as I can without slowing down my machine, that's typically around 50000 or so before it gets noticeable. This way I can go back for months if I don't remember that one difficult pipeline or whatever and I need it again.

( I'm not using bash myself, only zsh, but from what I gather from the manpage the mechanism is similar. Someone please correct me if I got the details wrong. )

0

Found it!

Let s say you have some big command, add echo before the command, and redirect stdout to a file.

Example for the command

someapp -wich have a lot of arguments and -is boring -to type

Type

echo someapp -wich have a lot of arguments and -is boring -to type > file.sh

It will create file.sh which contain the command

DrakaSAN
  • 867
0

What you want, should/could be done with the command alias.

You could create separate files for each complex command but this is just what alias is for.

You do

alias sa='someapp -wich have a lot of arguments and -is boring -to type'

and when you type sa + enter on the prompt it will run your program with arguments.

To survive a restart of bash you need to add them to ~/.bashrc (or ~/.kshrc or whatever shell you use).

Mine looks like this:

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

So you could add your alias-line there.

Rik
  • 13,565