Recently I had some folks watching my shared screen and they were constantly saying “oh, that was cool, I didn’t know you could do that in the shell”.
If you’ve been working on the shell (here I’m talking about bash) day in and day out for years, you might know some of these. But a lot of people are occasional shell users, or adjacent shell users. They’re not grizzled Unix vets but rather developers, DBAs, or other people who use the shell as a user but have never really read a book about it.
Here’s some cool tricks you can use to make your interactive shell experience more pleasant. I’m not talking about shell programming (writing shell scripts) but rather working at the prompt.
Go Into the Directory You Just Made
mkdir /some/long/path/that/is/a/pain/to/type cd $_
This is faster than typing “cd /some/long/path…”. The $_ variable picks up the last item in your previous command.
Jumping Around the Line
control-A will jump your cursor to the start of the line
control-E will jump your cursor to the end of the line
So if you start to type…
kdir /some/long/path/that/is/a/pain/to/type
And then realize you forgot the “m” in “mkdir”, you can hit control-A, add it, and then either hit ENTER to submit the command or control-E to jump back to the end of the line and continue typing. If you’d hit ENTER already and got an error, then the up arrow would move back in history. Speaking of which…
History
The
history
command will show you your history. Let’s say it looks like this:
551 ssh user@server 552 vi /tmp/file 553 grep Prod /some/other/file
If you want to rerun that vi command, you can type
!552
You can also do
!vi
Be careful with this last form! Rerunning with the command number always gives you the exact command, but something like
!r
will rerun the last command that starts with r. Might be “rm /some/file”. Might be “rm -rf *”.
You type control-R to search through your history.
When You’ve Typed a Long Command But Don’t Want to Run it
So you’ve typed something like this:
cp /some/long/path/to/files/*.csv /some/other/long/directory/that/is/a/pain/to/type
And just as you get to the end you realize that you haven’t created /some/other/long/directory/that/is/a/pain/to/type yet. Rather than cancel the command and go create the directory and retype the command, you can do this: hit control-A, type # to insert the comment character at the start of the line, and then hit return.
Now this long command is in your history. You can go create the directory, and then scroll back (up arrow) to this command, hit control-A, remove the #, and hit ENTER.
Conditional Execution
You can execute multiple commands on one line by separating them with semi-colons:
command1 ; command2
But what if you don’t want command2 to run unless command1 succeeds? For example:
mkdir /tmp/blah ; cd /tmp/blah ; cp /somewhere/*.csv .
If the mkdir and cd fails, you don’t want the cp to run. Instead of semi-colons, use the logical operator for “and”:
mkdir /tmp/blah && cd /tmp/blah && cp /somewhere/*.csv .
This will stop execution once any command fails.
Watch a Command
Let’s say you have a long copy, rsync, etc. process going. You want to watch how it’s going. What you want is to run du -sh (“disk usage, summary, human-readable output”) every 60 seconds. In ages past you’d do something like:
while [ 1 ] ; do echo ; date ; du -sh /dest/dir ; sleep 60; done
That still works, but this is simpler:
watch -n 60 /dest/dir
What’s your favorite Linux shell trick? Drop it in the comments below!
Related Posts:
- LowEndBoxTV: Are All Hetzner Locations Alike?No!And Piotr Has the Proof: “This is Clearly Not the Same Processor” - November 22, 2024
- One Week From Tomorrow…THE WORLD WILL LOSE THEIR MINDS!Lines Are Already Forming! - November 21, 2024
- Crunchbits Discontinuing Popular Annual Plans – The Community Mourns! - November 20, 2024
Is there any way to filter out Low End Guides from RSS feed and keep Low End Box offers only? If yes – how?
I like the guides, even if this one didn’t tell me anything new. :-)
Anyway, just go to a category at the top and subscribe to said category.