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
-
- Posts: 164
- Joined: Fri Nov 30, 2018 2:39 pm
- Location: Wales
Listing the command lines that I have written
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. 

Re: Listing the command lines that I have written
Assuming you are working at a shell (command line) prompt in the Raspbian Operating System:
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].
Code: Select all
cat ~/.bash_history
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].
Last edited by B.Goode on Thu Mar 14, 2019 9:20 am, edited 1 time in total.
Re: Listing the command lines that I have written
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
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
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
Press ctrl-o, ctrl-x to write the file and exit.
Make the file executable.
chmod +x myscript
Run the script
./myscript
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"
Make the file executable.
chmod +x myscript
Run the script
./myscript
-
- Posts: 164
- Joined: Fri Nov 30, 2018 2:39 pm
- Location: Wales
Re: Listing the command lines that I have written
Thanks all, I'm getting there (slowly) 
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?

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?
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. 

Re: Listing the command lines that I have written
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.
You can highlight the text in a terminal window then copy with shift ctrl c and paste with shift ctrl v.
-
- Posts: 164
- Joined: Fri Nov 30, 2018 2:39 pm
- Location: Wales
Re: Listing the command lines that I have written
Ah!
so i can just alter it on the screen and then re-run it and it will overwrite the initial command?
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. 

Re: Listing the command lines that I have written
doubleudee1 wrote: ↑Thu Mar 14, 2019 10:10 amAh!
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].
-
- Posts: 164
- Joined: Fri Nov 30, 2018 2:39 pm
- Location: Wales
Re: Listing the command lines that I have written
Thanks all.
WD
WD
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. 

-
- Posts: 14020
- Joined: Fri Mar 09, 2012 7:36 pm
- Location: Vallejo, CA (US)
Re: Listing the command lines that I have written
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
It might be easier to
then edit myfile.
Run regularly used command lines from scripts, (as above), saves time & possible mistakes.
Code: Select all
cat ~/.bash_history >> myfile
Run regularly used command lines from scripts, (as above), saves time & possible mistakes.

Re: Listing the command lines that I have written
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.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].
Does anyone know if this feature is available in Bash?
Re: Listing the command lines that I have written
Doesn't appear to, nor ctrl-v.
However, bash will redirect to a file if you want to save it.
However, bash will redirect to a file if you want to save it.
Code: Select all
echo "commands" >> file