Top 10 tricks for the terminal

1. Execute the last command with !!

The title says it all, just enter the following in a terminal ...

!!

… To re-execute the last command entered. This trick is very useful when we forget to enter sudo at the beginning. In that case, you would have to enter:

sweat !!

To discover more complex ways to get the most out of this trick, I recommend reading this old post.

2. Execute the last command but correcting a typing error

This trick is really useful when we enter a command with a simple typo. For example, if we run:

threw out "desdelinuxz"

You can correct it by entering the following:

^z

3. Invoke a text editor to enter a long command

Sometimes you need to enter endless commands in the terminal. In that case, the help of a simple text editor, such as nano or emacs, can be helpful.

To open the editor, just press Ctrl + x + e in a terminal. The text that you have entered so far will be copied into the text editor once it is opened.

For its part, the editor used will be the one specified in the $ EDITOR variable. To change this configuration, it is possible to execute ...

export EDITOR = nano

… Replacing nano with your preferred editor.

4. Execute a command without saving it to history

A true hacker cannot ignore this trick. As everyone knows, bash saves the history of executed commands, which is very easy to access using the keyboard arrows or Ctrl + R (to perform a reverse search in the history).

In this case, what you have to do so that the entered command is not saved in the history is to put a space in front:

command

5. Automatically place the last parameter of a command

Suppose the command executed earlier was

cp file.txt / var / www / wp-content / uploads / 2009/03 /

To access the directory in question, you can enter the command cd followed by Alt +. o Esc +. :

cd 'ALT +.'

This is just a shortened way to enter:

cd / var / www / wp-content / uploads / 2009/03 /
By continuing to press the mentioned keyboard shortcut, it is possible to navigate through the history of the last commands executed.

6. Execute a command at a specified time

Yes, yes, that's what it exists for cron. However, sometimes we want to run a command to run at a certain time but only once.

Suppose we want to execute the command Ls -l at midnight. In that case, we must execute it in the following way:

echo "ls -l" | at midnight

7. Get your external IP

Thanks to the service provided by http://ifconfig.me/ It is possible to know various data of your Internet connection, directly from the terminal:

curl ifconfig.me/ip // IP address curl ifconfig.me/host // Remote Server curl ifconfig.me/ua // User Agent curl ifconfig.me/port // Port

8. Learn how to use Ctrl + u and Ctrl + y

How many times did it happen to you that you started typing a command and remembered that before it was necessary to do something else? Well, it is possible to do a sort of cut-paste so that later it is easier to re-enter the command that was left half.

Suppose you started writing ...

cd / home / user

and you remembered that before you had to check something in the current directory. In that case press Ctrl + u (It would be something like a "cut").

Enter the command that needed to be run first. Suppose ...

Ls -l

... and then press Ctrl + y (It works like a "paste").

9. Clean the terminal easily

With Ctrl + l the terminal can be cleaned in a jiffy.

10. Go to a directory, run a command, and return to the current directory

This trick is a delight. To execute a series of commands without leaving the current directory, just group them in parentheses. As almost everyone knows, to execute a series of commands, you have to concatenate them using &&. Using these concepts, we can execute something like the following:

(cd / tmp && ls)

The result of this command (notice the parentheses) will be a list of the files and directories within the folder / Tmp. All without leaving our current directory. A jewel!

Yapa. List the most used commands

To see them most used commands just execute the following:

history | awk '{a [$ 2] ++} END {for (i in a) {print a [i] "" i}}' | sort -rn | head

In my case, the winners were:

450 yaourt 415 sudo 132 git 99 cd 70 leafpad 70 killall 68 ls 52 pacman 50 xrandr 45 top

For terminal fans, like this server, I recommend visiting commandlinefu. Also, why not, don't stop looking at the archive from our blog.

Are there any tricks missing? Don't forget to share and comment below. 🙂

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   fox said

    There was one that I used that omitted capital letters in my inputrc, because honestly many files that started like this would slip into me.

  2.   Jesus Perales said

    I think one of the most important is the history command in which it returns a numbered list
    of the commands you have used and if you want to use any of them just use the following line
    ! 22

    that will execute the command that is in your history at number 22 😀

    To search for a command with auto-completion is
    ctrl + r

    1.    Xurxo said

      I also always use Ctrl + r to repeat the last command; once the last one appears, you can scroll back through the history by pressing Up Pag (up arrow).

      Greetings.

  3.   Rodrigo bravo said

    Good article. Thanks for sharing those Tips.
    I want to share one that I use daily which is 'fg', which allows you to open the applications that you stopped earlier with 'Ctrl + z'.

    1.    giskard said

      And with 'bg' you send them to background 🙂

    2.    let's use linux said

      Look at you ... good trick! Thanks for sharing it.
      Hug! Paul.

  4.   Gabriel said

    The post is excellent, they always remind you of things that you knew and eventually forgot, but that are really useful.

  5.   Qvic said

    Terminal cheats are always welcome.
    Incredibly I did not know that adding a space to the command was omitted from the History.
    Thank you very much, Greetings !!

  6.   ezequiel said

    to use auto complete filtering history add in $ HOME / .inputrc file
    "\ E [5 ~": history-search-backward
    "\ E [6 ~": history-search-forward

    and for example at some point you had used:
    $ cd / one / path / longer / than / the / shit

    and now you want to use it again, you have to do:
    $ cd
    and then press the "Page Up" or "Page Down" keys to navigate through all the commands in the history that started with "cd".

    1.    giskard said

      OR…. Without editing anything, press Ctrl + R and then cd (if you want to search for something with 'cd') and by pressing Ctrl + R repeatedly you will see all the commands that contain it until you get to the one you want. Obviously, if it is the immediately preceding one, you will get one.

    2.    let's use linux said

      Very good!

  7.   antc said

    Actually ls / tmp should be enough to list the directory without leaving the current one.

  8.   slayerkorn said

    You can also concatenate the command using ";" as in example n ° 10 it would be like this:

    (cd / tmp; ls)

    regards

    1.    RudaMale said

      He ";" It works differently than the "&&" (and), when we do command-1 && command-2 the second command is only executed if the output of the first is "0", that is, without error. In the case of the semicolon, the second command is executed regardless of the output of the first. To extend, there is the operator «||» (or), in this case the second command is executed only if the first one throws an error on the output. Regards.

  9.   anonymous said

    In order for each line that remembers history to have date and time, there must be an environment variable in the ~ .bashrc of the normal user or root.

    # nano .bashrc
    export HISTTIMEFORMAT = »% F% T»

    #history
    492 2014-09-02 14:25:57 revdep-rebuild -i -v
    493 2014-09-02 14:31:14 eclean-dist -d
    494 2014-09-02 14:31:23 localepurge -v
    495 2014-09-02 14:31:29 etc-update
    496 2014-09-02 14:31:54 emerge –depclean –pretend
    497 2014-09-02 14:39:08 updatedb

    There are more options, which are for the amount of lines it saves, I am testing them.

  10.   chuckT said

    Very good post. Really very useful. Trick number 4, that of executing a command without it being saved in the history, has only worked for me in Ubuntu, not in Debian, nor in Centos.

    1.    Azureus said

      It worked for me on raspbian and not on arch.

  11.   Chaparral said

    Merveilleux!!

  12.   erjuak said

    Hello,
    what to put a space before executing a command so that it is not registered in the history, as it does not work ...
    [user @ host /] $ ls -l
    Total 104
    dr-xr-xr-x. 2 root root 4096 Aug 21 03:55 bin
    dr-xr-xr-x. 5 root root 3072 Aug 20 17:26 boot
    drwxr-xr-x. 2 root root 4096 Dec 9 2013 cgroup

    [user @ host /] $ history
    1024 ls
    1025 ls
    1026 history
    1027 ls -l
    1028 history

    interesting article..

    All the best

  13.   erjuak said

    Sorry, for not reading all the posts,
    The test is also done in centos.

  14.   eliotime3000 said

    Good with Ctrl + U and Ctrl + Y.

  15.   NauTiluS said

    On my system, the list of most used commands worked for me by changing the variable to $ 5.

    Output example:

    1122 sudo
    362 ls
    279 clear
    214 cd
    142 dig
    141 yaourt
    130 vnstat
    122mv
    112 saw
    112 history

    This is because in my .bashrc file I have the following line:
    # Show the date to the history command
    export HISTTIMEFORMAT = '% F% T:'

    I am almost certain that many around here have used several terminals at the same time.
    Now, I don't know whether to tell you that it's a terminal trick, but as I usually have many windows or using screen to have several terminals open, this enables any command entered in one of those terminals, as it will be reflected in all .

    This goes in the .bashrc file:

    HISTSIZE = 90000
    HISTFILESIZE = $ HISTSIZE
    HISTCONTROL = ignorespace: ignoredups

    history () {
    _bash_history_sync
    builtin history "$ @"
    }

    _bash_history_sync () {
    builtin history -a # 1
    HISTFILESIZE = $ HISTSIZE # 2
    builtin history -c # 3
    builtin history -r # 4
    }

    PROMPT_COMMAND = _bash_history_sync

    1.    let's use linux said

      Very good contribution! Thanks for passing.
      Hug! Paul.

  16.   Roy said

    Congratulations on the article.
    Very practical and I have learned a few commands.

  17.   linuXgirl said

    Although I am already somewhat "experienced" in these matters, these tricks are always appreciated, so… for the trunk of tricks !!!

  18.   Nonamed said

    great, thanks for the tricks, they are really useful 😀

  19.   vinsuk said

    The paths of the console are inscrutable 😉

  20.   desikoder said

    The trick of pressing control + u and control + y does not depend on the shell, but on the configuration that the tty have. To this day under Linux the tty are very complex entities, in fact I have been investigating their operation and I can assure that they have many more ins and outs than it seems. The tty have some control characters that change their status, for example it has happened to me sometimes that I do cat a binary file, and after all the "garbage" appears on the screen, the prompt is strange or with another coding. This is because in a random file there is a high probability of encountering tty control characters.

    For example, run this in a shell:

    print '33c'

    That will clear the screen (as if you run clear).

    Other combinations, such as control + L to clear the screen come from the shell itself that is being used, the classic / bin / sh shells do not usually bring this feature.

    In addition, today there are a lot of complex options for handling files and sockets under Linux. For example, if you open a tty device using open (), in AIO (Asynchronous Input / Output) mode, the process will receive a SIGIO every time there is data available in the input buffer.

    If, for example, bash works simply by reading the lines and executing commands (as it does when executing a script), when pressing control + L, this sequence of characters would simply be included as part of the commands, but since bash has control of the buffer of input can detect it.

    For example, sometimes when programming I have come to use fcntl to get a non-blocking input buffer, which I have used even with ncurses libraries (manipulate the control tty before starting ncurses in the program).

    Greetings!

  21.   Lautaro said

    For those who did not work the space at the beginning, and perhaps to add it to the note, there is a variable called HISTIGNORE where the character that is added before a command is configured so that it is ignored in the history.
    In some installations this variable does not come from
    Greetings and good post! direct to favorites!

    Note that in the comments I also saw several interesting things .. !!

  22.   ramon hidalgo said

    All really very useful. Thank you.