Improve performance on the command line with One-Liners Command

Tram Ho


Linux is becoming more and more popular all over the world, it crept into almost every computer system as well as common technology devices today. The first thing that people mention about Linux is how they use it. Other with computer systems running the Windows operating system used primarily in the graphical interface, or MacOS use a combination of graphical and command line with Linux, most of the time they work will be on the system command line.

All the work that needs to be handled on Linux is possible via the command line system to complete, from difficult to easy, from simple to complex. Over time, to avoid having to type lengthy and difficult commands to remember, people began to switch to Shell Script files to speed up their work. Often these files will be in size from several lines to several hundred lines. However, with simple tasks, it is not necessary to write to the file to execute. And nowadays people will often write short syntaxes and combine with alias to form a simple effective command. These syntaxes are often referred to as One-Liner .

Here I will introduce some collection and compilation syntax to help work on the command line system of Linux users become simpler.

Useful commands

Filter processes that use a lot of memory

Command: ps aux | awk '{if ($5 != 0 ) print $2,$4,$6,$11}' | sort -k2nr | head -n10

  • ps aux will retrieve all processes created by the current user
  • awk will print out the corresponding values ​​in the PID ($ 2, process id) column,% MEM ($ 4, the actual memory ratio used), RSS ($ 6, the number of actual memory bytes used) and COMMAND ($ 11, main executable statement of process)
  • sort will rearrange the data rows, -k2 means sorting by the 2% column MEM, -n is arithmetic sort, -r is reversed to get descending results
  • head -n10 to get the first 10 lines, also the 10 processes that consume the most memory.

Syntax retrieves most frequently used commands (with Bash Shell)

Command: cat ~/.bash_history | tr "|;" "n" | sed -e "s/^ //g" | cut -d " " -f 1 | sort | uniq -c | sort -nr | head -n 15

  • The typed commands will be stored in ~/.bash_history file
  • uniq -c to count the number of duplicate commands
  • cut to limit the columns to display
  • sort to sort the statements by number of uses

Syntax used to delete blank lines in the file

Command: sed -i '/^$/d'

Before deleting files there are a lot of blank lines

And after deleting, there are no more blank lines

Rename all the files in the directory to convert spaces into underscore

Usually when working with files whose names have spaces, it will be inconvenient for users, need to insert backsplash to use. Command: for i in *; do mv "$i" ${i// /_};done

Before changing the name, very inconvenient to manipulate

After renaming has manipulated easier

The syntax for quickly shutting down an active web server

Command: pgrep -f tcp://[IP]:[PORT] | xargs kill -9

Server is running

Combine xargs and kill to close the server

Get the name of the project based on the github repository

Command: git remote get-url framgia | grep -o "/[a-zA-Z0-9_-]+.git" | sed -E "s/^/|.git$//g"

  • The command git remote get-url to retrieve the url of the repository
  • grep -o will retrieve the text starting with splash / and ending with .git
  • sed will remove the / and .git .

Here I have 2 remote repository, I will get the name of the project based on the repository framgia

The following results.

Use git to delete all files with a given name in the current directory tree from cached

Command: find . -name [file_name] -exec git rm --ignore-unmatch --cached {} +

Suppose you have files with the name .keep used when you want to push empty directories to github

After processing the above command, all .keep files will be moved to untracked state


Above are some of the One-liner commands I have collected and compiled that are used to improve the performance on the Linux command line. Thank you everyone for reading.


Share the news now

Source : Viblo