How do I search my command-line history for commands I used before?

1

Why should we clear Command line history?

There are chances that you don’t want to expose the Command line history in your Linux system. Say for example, you are a Linux trainer teaching Linux to students. You are testing some critical and deadly commands in the Lab computer. The commands you testing might cause a serious damage to the system. But the new Linux users and students might not aware of those critical commands. A curious student may search the command line history, wonder what does those commands do, and start to test them one by one. The result? He/she might break the system. Do you want to allow that? Of course, we can re-install or repair the system in couple minutes. But, I think it is completely unnecessary if you be bit careful. So, clearing Command line history from time to time, especially in a shared computer, is a good practice. It is just an example. There might be many other reasons to clear Linux command line history.

In this brief tutorial,...

0 0
2

If you’re trying to remember an exact command you executed via the Terminal but can’t quite come up with it, you can query your command line history to discover old commands by using the following syntax:

history |grep "search string"

This will look for “search string” in your command history and only print back instances that include the search text. If you’re unfamiliar with the Terminal and you’re wondering why this might be useful, read on for an example.

Example: Searching Past “defaults” Commands
I was trying to recall the exact syntax of a defaults write command that I recently used. The defaults commands are often long strings of text that modify behavior of Mac OS X or certain applications, because of their length and obscurity, trying to remember one of these off the top of your head is challenging to say the least. Instead of hitting the up arrow to scroll through past executions for an eternity, I used the following to narrow my command...

0 0
3

CommandLineIntro: CommandHistory

The shell lets you bring back old commands and re-enter them, making changes if you want. This is one of the easiest and most efficient ways to cut down on typing, because repeated sequences of commands are very common. For instance, in the following sequence we're going through various directories, listing what's there, deleting files we don't want, and saving certain files under different names:

cd Pictures/ ls -l status.log.* rm status.log.[3-5] mv status.log.1 status.log.bak cd ../Documents/ ls -l status.log* rm status.log.[2-4] mv status.log.1 status.log.bak cd ../Videos/ ls -l status.log* rm status.log.[2-5] mv status.log.1 status.log.bak

Eventually, if you had to do this kind of clean-up regularly, you would write a script to automate it and perhaps use a cron job to run it at regular intervals. But for now, we'll just see how to drastically reduce the amount of typing you need while...

0 0
4
...
0 0
5

Let me teach you how to work efficiently with command line history in bash.

This tutorial comes with a downloadable cheat sheet that summarizes (and expands on) topics covered in this guide.

Download PDF cheat sheet: bash history cheat sheet (.pdf) (downloaded: 227528 times)
Download ASCII cheat sheet: bash history cheat sheet (.txt) (downloaded: 23324 times)
Download TEX cheat sheet: bash history cheat sheet (.tex) (downloaded: 11593 times)

In case you are a first time reader, this is the 3rd part of the article series on working efficiently in bourne again shell. Previously I have written on how to work efficiently in vi and emacs command editing modes by using predefined keyboard shortcuts (both articles come with cheat sheets of predefined shortcuts).

First, lets review some basic keyboard shortcuts for navigating around previously typed commands.

As you remember, bash offers two modes for command editing - emacs mode and vi mode. In...

0 0
6

If you get started on Linux (like I, a Mac user, did after getting curious), getting used to the platforms quirks can be challenging. Linux doesn’t work like other platforms. It requires rethinking how a computer “should” work and understanding a whole different paradigm with its own strengths and weaknesses.

One of the biggest changes is the command line. Windows has Command Prompt (which any good sysadmin will spend a good bit of time in) and Mac has Terminal, but both prefer to keep command line stuff as a last resort. Mac and Windows (and some Linux brands, such as Ubuntu) would first present a GUI for managing programs and updating your OS.

Linux, however, runs mostly on the command line. Doing all the cool (and useful) things in the operating system requires understanding how some funny-sounding commands can operate your system. However, the command line can be intimidating. To help you understand it and work in it better, here are some things you should...

0 0
7

Advertisement

Updated by Tina Sieber on January 21, 2017.

Microsoft has slowly but surely pushed the command line 10 Windows Command Line Tips You Should Check Out aside in the Windows interface. This is not without reason. It’s an antiquated and mostly unnecessary tool from an era of text-based input.

But many commands remain useful, and Windows 8 and 10 even added new features. Here we present the 15 commands every Windows user needs to know.

In case you’re not sure how to access the command prompt, forgot basic commands, or would like to know how to see a list of switches for each command, you can refer to our beginners guide to the Windows command line A Beginner's Guide To The Windows Command Line for instructions.

Prefer this tutorial in video form? We’ve got you covered:

Our 15 Favorites

1. ASSOC

Most files in...

0 0
8

Advertisement

If you want to become a true Linux master, having some knowledge of terminal commands is a good idea. Here are four different methods you can use to start teaching yourself.

Tip of the Day


A great way to gradually learn more about terminal commands is to have a “Tip of the Day” style message appear each time you open up the terminal. These messages can tell you about useful commands, as well as advanced tricks for certain commands you may already know. You can easily set this up by going into your .bashrc file (located at /home//.bashrc) and add the following to the end of the file on a new line:

echo "Did you know that:"; whatis $(ls /bin | shuf -n 1)

That’s all you have to do! If you’d like to make it slightly more entertaining, you can make a cow say all of these tips. To do so, run the command sudo apt-get install cowsay for Ubuntu/Debian or sudo yum install cowsay for Fedora. Then, instead of the code above, add the...

0 0
9

These are just a few of the most common GNU / Linux commands that you may find yourself looking for, along with some answers to common questions about running Linux. On most systems more information about a command can be found by typing [ man command ] ; man being the word man and command being the particular command you are checking.

You will need to be root to use some of these commands ; also be sure to check your

path

. If there is no path to the command then you will likely get a

" command not found "

error. Check the command to be sure you have typed it correctly. Be

extremely

careful as root , you can make your system unusable. This is very important to understand. If you are using a dual boot system you may not be able to access either system if you make a mistake as root and your system is not bootable. Before you type any command as root be absolutely certain of what you are doing. Any comments are encouraged and welcome at

Update...

0 0
10

This article provides practical examples for 50 most frequently used commands in Linux / UNIX.

This is not a comprehensive list by any means, but this should give you a jumpstart on some of the common Linux commands. Bookmark this article for your future reference.

Did I miss any frequently used Linux commands? Leave a comment and let me know.

1. tar command examples

Create a new tar archive.

$ tar cvf archive_name.tar dirname/

Extract from an existing tar archive.

$ tar xvf archive_name.tar

View an existing tar archive.

$ tar tvf archive_name.tar

More tar examples: The Ultimate Tar Command Tutorial with 10 Practical Examples

2. grep command examples

Search for a given string in a file (case in-sensitive search).

$ grep -i "the" demo_file

Print the matched line, along with the 3 lines after it.

$ grep -A 3 -i "example" demo_text

Search for a given string in all files recursively

$ grep -r...
0 0
11

Check out my other tutorials on the Unix Page, and my

Check my blog

Copyright 1994, 1995 Bruce Barnett and General Electric Company

Copyright 2001, 2013 Bruce Barnett

All rights reserved

You are allowed to print copies of this tutorial for your personal use, and link to this page, but you are not allowed to make electronic copies, or redistribute this tutorial in any form without permission.

Original version written in 1994 and published in the Sun Observer

This section describes C Shell (CSH/TCSH) programming. It covers conditional testing, control loops, and other advanced techniques.

This month begins a tutorial on the bad-boy of UNIX, lowest of the low, the shell of last resort. Yes, I am talking about the C shell. FAQ's flame it. Experts have criticized it. Unfortunately, this puts UNIX novices in an awkward situation. Many people are given the C shell as their default shell. They aren't familiar with it, but they have...

0 0