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?

3
  • Commands in a file? Isn't that a shell script for? – Darius Nov 14 '13 at 9:42
  • Yes, I would like to create shell script based on commands (quite long) that I ve already used, and wonder if there is a better way to type them again to create the sh file, or if there is some way to put them in a shell script – DrakaSAN Nov 14 '13 at 9:45
  • This comment is on a different subject, but I am writing here because this seems to be the only way to talk to a user except chat. You gave me a short answer to one of my questions (on playing DVD with external subs) but then you have deleted it. I investigated your answer and found that VLC worked if the video output is changed to OpenGL. I want to give you credit and show you the solution: here. – user162573 Feb 20 '14 at 11:24
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

2
  • 1
    also >> will append the output to an existing file. > will overwrite any existing file. – suspectus Nov 14 '13 at 9:54
  • If your command contains wildcards (e.g., cat *.txt), the wildcard will be expanded when you do the echo and so you will capture cat laundry.txt shopping.txt (for example) in your file.sh. You might do better with the history command. – Scott Nov 14 '13 at 17:59
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.

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.