doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Listing the command lines that I have written

Thu Mar 14, 2019 9:03 am

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
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. :)

User avatar
B.Goode
Posts: 10964
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Listing the command lines that I have written

Thu Mar 14, 2019 9:18 am

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].
Last edited by B.Goode on Thu Mar 14, 2019 9:20 am, edited 1 time in total.

DirkS
Posts: 10517
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Listing the command lines that I have written

Thu Mar 14, 2019 9:20 am

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

User avatar
joan
Posts: 15284
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Listing the command lines that I have written

Thu Mar 14, 2019 9:23 am

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

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: Listing the command lines that I have written

Thu Mar 14, 2019 9:33 am

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?
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. :)

User avatar
joan
Posts: 15284
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Listing the command lines that I have written

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.

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: Listing the command lines that I have written

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?
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. :)

User avatar
B.Goode
Posts: 10964
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Listing the command lines that I have written

Thu Mar 14, 2019 10:18 am

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].

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: Listing the command lines that I have written

Thu Mar 14, 2019 10:27 am

Thanks all.

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. :)

W. H. Heydt
Posts: 14096
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Listing the command lines that I have written

Thu Mar 14, 2019 3:42 pm

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.

k-pi
Posts: 929
Joined: Sun Feb 12, 2017 1:46 pm
Location: Upper Hale, Surrey, UK.

Re: Listing the command lines that I have written

Thu Mar 14, 2019 7:54 pm

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-)

jbudd
Posts: 1614
Joined: Mon Dec 16, 2013 10:23 am

Re: Listing the command lines that I have written

Fri Mar 15, 2019 12:44 am

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?

k-pi
Posts: 929
Joined: Sun Feb 12, 2017 1:46 pm
Location: Upper Hale, Surrey, UK.

Re: Listing the command lines that I have written

Fri Mar 15, 2019 11:09 am

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

Return to “Beginners”