Lesser known Bash Shortcuts

What are the Lesser known Bash Shortcuts?

Lesser Known Bash Shortcuts – Introduction

Bash Shortcuts, often referred to as keyboard shortcuts or key bindings, are a collection of key combinations or sequences that you may use when working in the Bash shell, which is the standard command-line interface in many Unix-like operating systems, including Linux, to do a variety of activities more quickly.

Here are the Lesser known Bash Shortcuts shown below.

  1. Xargs

To run commands with arguments taken from standard input, use the xargs command. When you need to run a task on a lot of files or folders, this command can be beneficial. Let’s take the scenario where you must relocate a directory containing thousands of files to a different place. Use the xargs command to execute the command for all files at once rather than spelling it out for each one individually. Here is an illustration.

$ find . -type f -name '*.txt' | xargs -I{} mv {} /tmp

  1. nl

Line numbers can be added to a file using the nl command. When you need to refer to certain lines in a huge file, this command can be quite helpful. The example is shown below.

$ nl myfile.txt

This command adds line numbers to the text file myfile.txt in this example, and the output is shown on the terminal screen.

  1. Tee

Both a file and the standard output (stdout) stream can be used as the destination for output when using the tee command. This function might be very helpful when you wish to both store and view the result of a program on your terminal screen.

$ ls | tee output.txt

  1. watch

Using the watch command, a command may be executed periodically at predetermined intervals. This command might be very helpful when you wish to track a command’s output in real time.

$ watch -n 1 'ls -l'

  1. sort

To arrange the lines in a file in a certain order, use the sort command. When you wish to sort the contents of a big file, this command can be quite helpful.

$ sort -n myfile.txt

  1. timeout

To run a task with a time restriction, use the timeout command. When you wish to create a time restriction for a command that would otherwise continue forever, this command can be quite helpful.

$ timeout 10s sleep 20s

  1. rev

Reversing the order of the characters on each line of a file is done with the rev command. This command might be very helpful if you wish to reverse a file’s contents.

$ rev myfile.txt

  1. tac

The tac command is used to show a file’s contents backward, with the final line appearing first and the first line following. When you wish to see a file in reverse order or examine the contents of a log file, this command might be quite helpful.

$ tac access.log

  1. pgrep

The pgrep command is used to determine a running process’s process ID (PID) from its name. When you need to monitor or terminate a process and want to determine its PID, this command might be quite helpful.

$ pgrep firefox

  1. Tab

It is the most practical shortcut and time-saving device ever created. Commands, file names, or directory names are autocompleted for you. Simply hit the Tab key once you’ve begun entering a command, filename, or directory name. Either the machine will finish the string for you or it will show you all of your possibilities.

  1. Ctrl+R/O/G

When looking over command history, find the reverse-i-search combination to be helpful. You can go through previously used commands, bring them up, and repeat the ones you need. When using lengthy and chained commands, this is useful.

  • Ctrl+R: Recall the most recent command that included the given characters.
  • Ctrl+R (again): Navigate using the corresponding instructions.
  • Ctrl+O: Return the command to your terminal by pressing Enter, or run it directly from the search mode by pressing Enter.
  • Ctrl+G: Without executing a command, exit the history search mode.
  1. Ctrl+C

A well-known shortcut is Ctrl+C. By delivering the SIGINT signal to the program you wish to interrupt and terminate, it cleanly aborts the majority of programs. Most programs accurately detect it and gracefully terminate it. The underlying processes then end themselves in the absence of any SIGINT handling instructions from the program. Yes, using it is safe.

  1. Ctrl+L

The clear command has an analogous shortcut. Your terminal’s screen is cleared.

  1. Ctrl+D

When used after the su or sudo commands, this shortcut will essentially log you out of any terminal and shut it down or return you to the default user. An EOF (end-of-file) marker is sent to bash. When Bash receives this marker, it terminates. Running the exit command is comparable to using this shortcut.

  1. Ctrl+Alt+D

Repeat the shortcut to restore your windows to their original positions after minimizing all terminals or windows.

  1. Ctrl+U

This shortcut clears anything between the cursor’s current location and the start of the line. When you accidentally type a command or discover a syntactic problem and would rather start again, you find this beneficial. It’s a really useful shortcut.

  1. Ctrl+Z

If you need to return to the terminal while working on anything, this shortcut is helpful. It signals the foreground process using SIGTSTP. For instance, you could be using a text editor and need to go away from it to find some information. This shortcut allows you to pause and send the editor into the background. Once you’ve finished your other work, simply run fg to return to your program.

  1. Ctrl+E

The Ctrl+A shortcut is reversed by this one. The cursor is moved to the end of the line. When entering lengthy or chained instructions, I always utilize the Ctrl+A and Ctrl+E keyboard keys to swiftly move my mouse around the current line.

  1. Undo (C-/)

There is an undo function in Bash. It is nevertheless useful despite not being as robust as those in Emacs and Vim (for example, it can only revert actions and there is no redo command). It is curiously activated by the keybindings C-/, C-_, or C-x C-u. C-/ is the most practical key on the US layout since it only requires two keys.

  1. Revert line (M-r)

Revert-line is a unique variation of the undo command. It is useful every time you make a mistake with a line pulled from the command history and want to restore it to its original value.

  1. Open the line in the editor
ShortcutCommandDescription
C-x C-e(edit-and-execute-command)Edit the line in your editor of choice

Although Bash’s macros are nice, there is some room for improvement. They are unable to capture character-search, for instance. Additionally, although Bash’s Vim emulation is impressive, your NeoVim and Doom are on another level.

Bash must thus go to the mountain if the mountain would not come to him.

To rapidly open the current line in Emacs, your favorite editor, use C-x C-e.

To choose the editor to use, define VISUAL=your-editor> in the.bashrc file.

  1. String Inline Search
ShortcutCommandsDescription

 

C-r(reverse-search-history)Jump to the previous occurrence of a string
M-C-r Jump to the next occurrence of a string

 

M– C-r As above

 

Few people are aware that the same shortcut, when used in a not empty line, searches in that line. C-r is frequently used to search the command history. C-r runs a backward search.

Of course, if you add a Negative Argument before it, a forward search will now be used.

Wrapping Up

You ought to have some ideas for your aliases and Lesser known Bash Shortcuts after reading this article. Your stay in the shell can be made simpler and more enjoyable by routinely using these.

Redefining existing commands with potentially harmful behaviour should be avoided, always. When you first use a system without it after being acclimated to it, even going the other direction, and aliasing a command to a safer variant can go wrong.

Don’t forget to share your valuable Suggestions.

For more Click the link Below

https://linuxiron.com/blog/

Q. What is the tr command?

The tr command replaces or removes characters from standard input, and then outputs the result. Depending on the flags supplied and the strings specified by the String1 and String2 variables, the tr command does one of three types of operations.

Q. What does bash’s Ctrl V key do?

Commands are assigned to control keys by terminal-based systems. For instance, the command Ctrl – V in Bash means to “take the next character literally”. In the command line, you can now embed a control character. Such commands are unavailable if the terminal snatches control keys for its use.

Q. What does the bash less command do?

A Linux tool called Less can be used to read a text file’s contents one page (or one screen) at a time. Because it accesses files page by page rather than completely if they are enormous, it has faster access.

Q. In bash, how can I find less text?

Using the less command, open the file to view. Then, after pressing the / key and the search pattern you want to use, press the enter key. From your present position, a forward search will be initiated, and you will be moved to the first match. The highlighted patterns are those that match.

Q. What functions does the bash’s F option perform?

The -f option’s main purpose is to delete superuser-level protection from files. An error notice may display if we try to remove a read-only file, for instance, saying that we are unable to delete the file.

Leave a Comment

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