Page 1 of 1

Listing the command lines that I have written

Posted: Thu Mar 14, 2019 9:03 am
by doubleudee1
Hi
I've set up several lines of code to do different things on my PI. e.g. move files that are older than 7 days from one folder to another.

How can I list all that I have written ?
and can I alter them ?
or will I have to re-write them over again?


Thanks

Re: Listing the command lines that I have written

Posted: Thu Mar 14, 2019 9:18 am
by B.Goode
Assuming you are working at a shell (command line) prompt in the Raspbian Operating System:

Code: Select all

 cat ~/.bash_history
will show the history of what you have typed.

And the Up_Arrow key on the keyboard will recall the preceding command. And steps back repeatedly. Commands recalled in this way can be edited interactively, as though they had just been typed, before being executed by pressing [Return].

Re: Listing the command lines that I have written

Posted: Thu Mar 14, 2019 9:20 am
by DirkS
Assuming you're running a standard setup with Raspbian as the OS the answer is yes.

There are many ways of recycling commands you have used. Bash keeps a history of (almost) every command you issue
* arrow keys up and down; you can edit them before running them
* search the history with <Control+R>
* several other commands, command modifiers, keys
See e.g. https://www.digitalocean.com/community/ ... -linux-vps for more information

Re: Listing the command lines that I have written

Posted: Thu Mar 14, 2019 9:23 am
by joan
Some shells store the previous commands in a history file. You can use up arrow to go back through the history and use return to repeat the command.

Perhaps a better way is to create a script file containing the command.

E.g. nano myscript will let you enter text.

Enter the following

Code: Select all

#!/bin/bash
echo "Your name is $LOGNAME"
Press ctrl-o, ctrl-x to write the file and exit.

Make the file executable.
chmod +x myscript
Run the script
./myscript

Re: Listing the command lines that I have written

Posted: Thu Mar 14, 2019 9:33 am
by doubleudee1
Thanks all, I'm getting there (slowly) :D
Is there a way of altering a pice of code I have entered into the shell window? Or a way of deleting it and then entering a new updated piece?

Re: Listing the command lines that I have written

Posted: Thu Mar 14, 2019 9:46 am
by joan
You can edit the line when it is on screen using the left/right cursor keys and delete etc.

You can highlight the text in a terminal window then copy with shift ctrl c and paste with shift ctrl v.

Re: Listing the command lines that I have written

Posted: Thu Mar 14, 2019 10:10 am
by doubleudee1
joan wrote:
Thu Mar 14, 2019 9:46 am
You can edit the line when it is on screen using the left/right cursor keys and delete etc.

You can highlight the text in a terminal window then copy with shift ctrl c and paste with shift ctrl v.
Ah!
so i can just alter it on the screen and then re-run it and it will overwrite the initial command?

Re: Listing the command lines that I have written

Posted: Thu Mar 14, 2019 10:18 am
by B.Goode
doubleudee1 wrote:
Thu Mar 14, 2019 10:10 am
joan wrote:
Thu Mar 14, 2019 9:46 am
You can edit the line when it is on screen using the left/right cursor keys and delete etc.

You can highlight the text in a terminal window then copy with shift ctrl c and paste with shift ctrl v.
Ah!
so i can just alter it on the screen and then re-run it and it will overwrite the initial command?


Yes. That's what @joan wrote.

And similar to a previous post that said:
Commands recalled in this way can be edited interactively, as though they had just been typed, before being executed by pressing [Return].

Re: Listing the command lines that I have written

Posted: Thu Mar 14, 2019 10:27 am
by doubleudee1
Thanks all.

WD

Re: Listing the command lines that I have written

Posted: Thu Mar 14, 2019 3:42 pm
by W. H. Heydt
If you find yourself using the same commands a lot, even with minor variations, wrap the sequence up into a shell script, create a "bin" directory under /home/pi (the next time you reboot, it will be part of the default PATH), and run them that way. No need to rely on history being intact and the commands you want being easy to get back to.

Re: Listing the command lines that I have written

Posted: Thu Mar 14, 2019 7:54 pm
by k-pi
It might be easier to

Code: Select all

cat ~/.bash_history >> myfile
then edit myfile.

Run regularly used command lines from scripts, (as above), saves time & possible mistakes. 8-)

Re: Listing the command lines that I have written

Posted: Fri Mar 15, 2019 12:44 am
by jbudd
And the Up_Arrow key on the keyboard will recall the preceding command. And steps back repeatedly. Commands recalled in this way can be edited interactively, as though they had just been typed, before being executed by pressing [Return].
In the Korn shell, now out of fashion, you could scroll back up through recent commands and then hit 'v' to edit the chosen command with vi, or your chosen alternative editor. It was especially useful with complex multi line commands.
Does anyone know if this feature is available in Bash?

Re: Listing the command lines that I have written

Posted: Fri Mar 15, 2019 11:09 am
by k-pi
Doesn't appear to, nor ctrl-v.

However, bash will redirect to a file if you want to save it.

Code: Select all

echo "commands" >> file