Home FAQ Some Random One-liner Linux Commands [Part 1]
Random One-liner Linux Commands Part 1

Some Random One-liner Linux Commands [Part 1]

By sk
555 Views

The following one-liner Linux commands have already been shared in image templates in our social and professional networks. These commands are just single line commands that makes your command line life easier and better. I have decided to gather all commands that we shared in our social network pages, put them all together in a single article and publish it at the end of every month. Additionally, I have included some more tips & tricks and related resources to learn Linux stuffs. Some of the commands given below are collected from Arch wiki, /r/linux, Askubuntu, and Stack Overflow. All credit goes to the community. And some are my own findings from day-to-day experience. This is the first part in the series. We will be publishing the subsequent parts at every month-end.

Some Random One-liner Linux Commands

These commands are mostly for beginners. All commands are given in no order. If there are any typos, mistakes in commands, let me know in the comment section below. I will check and update them ASAP.

1. Open random man pages

Feel bored at work? Open any random man pages and start reading it. It's good for killing your boring time.

$ apropos . | shuf -n 1 | awk '{print$1}' | xargs man

To know more about Apropos, check the following link.

2. Display information about a Linux distribution

To show all the available information about your current distribution, package management and base details, run:

$ echo /etc/*_ver* /etc/*-rel*; cat /etc/*_ver* /etc/*-rel*

Sample output from Ubuntu 18.04 desktop:

/etc/debian_version /etc/lsb-release /etc/os-release
buster/sid
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.3 LTS"
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

3. Get notification when a command is completed

To get notified when a command is completed, add the following line at end of the command. It is good for monitoring commands that takes long time to complete.

;notify-send done

Example:

$ ls -l ;notify-send done

Similar tools:


4. Find files bigger than X size

To find files bigger than X size, for example 10 MB, and sort them by size, run:

$ find . -size +10M -type f -print0 | xargs -0 ls -Ssh | sort -z

We can also find files smaller than X size as described in the below link.

5. Run Linux commands non-interactively

To run Linux commands non-interactively, use "yes" command like below.

$ yes | sudo apt install vim

It doesn't require user intervention. To put this simply, you don't have to type "yes" or "y" to complete the given command. It might be useful in scripts. It's also dangerous. You might accidentally do some damages when deleting files or directories. Be cautious when using "yes" command.

6. Recall Nth command from history

We can recall "N"th command from your BASH history without executing it. For example, the following command will display 12th command from the history, but it won't execute it.

$ !12:p

Related read:


7. Learn Unix/Linux file system hierarchy

To learn about Unix/Linux file system hierarchy, run:

$ man hier

8. Know what a command will do

If you don't know what a particular command will do, you can use Explainshell web service.

Explainshell breaks down the long/confusing commands and instantly display what each command part will exactly do. This is recommended site to newbies.

9. How to use Terminal if ENTER key is not working

To use the Terminal on a system where the ENTER key doesn't work, use the following keyboard shortcuts:

  • CTRL+j or CTRL+m

10. Find broken symbolic links

To find all broken sym links in your system, run:

$ find . -type l ! -exec test -e {} \; -print

Suggested read:


11. Monitoring CPU speed

To monitor CPU speed in real time, run:

$ watch grep \"cpu MHz\" /proc/cpuinfo

Press CTRL+c to stop monitoring.


Related resources:


12. Find installation date

To find the exact installation and date of your Linux OS, use the following commands:

Arch Linux:

$ head -n1 /var/log/pacman.log

If the logs are already deleted, use the following commands as root user.

# fs=$(df / | tail -1 | cut -f1 -d' ') && tune2fs -l $fs | grep created

Or,

# tune2fs -l /dev/sda1 | grep 'Filesystem created:'

On RPM based systems such as Fedora, RHEL and its clones such as CentOS, Scientific Linux, Oracle Linux:

$ sudo rpm -qi basesystem

Or,

$ sudo rpm -qi basesystem | grep Install

13. Find most used commands

To find most used commands on your Linux command, run:

$ history | awk '{print $2}' | sort|uniq -c|sort -nr|head -15

This command will display the top 15 most used commands.

More examples in the below link.

14. Find last sleep time

Find when was the last time your system went to sleep:

$ journalctl -u sleep.target

Related resources:


15. Enable and start a service

To enable and start a service, for example docker, with a single command:

# systemctl enable --now docker

Usually, I enable and start a service like below until I came to know this one-liner.

# systemctl enable docker
# systemctl start docker

16. Difference between "&&" and ";" operators between commands

The "&&" operator executes the second command only if the first command was successful.

Example:

$ sudo sh -c 'apt-get update && apt-get upgrade'

In the above case, the second command (apt-get upgrade) will execute only if the first command was successful. Otherwise, it won't run.

The ";" operator executes the second command whether the first command was successful or failed.

Example:

$ sudo sh -c 'apt-get update ; apt-get upgrade'

In the above case, the second command (apt-get upgrade) will execute even if the first command is failed.

16. Monitoring Kernel messages

To monitor Kernel messages in live, run:

$ dmesg -wx

To stop monitoring press CTRL+c.

Check Netdata tool to monitor everything in a Linux system.

17. Copy everything except one file or directory

$ rsync -avz --exclude 'ostechnix' dir1/ dir2/

The above command will copy everything from dir1 to dir2, except "ostechnix". The "ostechnix" can be either file or folder.


Similar resources:


18. Check service status

To check if a particular service is enabled or not at startup, use:

$ systemctl is-enabled bluetooth-service

19. Delete duplicate lines in files

We can delete all consecutive duplicate lines in a file, for example ostechnix.txt, using command:

$ sed '$!N; /^\(.*\)\n\1$/!P; D' ostechnix.txt

This command will delete all consecutive duplicate lines from the ostechnix.txt file.


Related read:


20. List screen resolution

To list all resolutions supported by your X, use xrandr command like below:

$ xrandr

To change X's resolution on the fly:

$ xrandr -s 1024x760

We can also adjust monitor brightness using xrandr command. More details are in the following link.

21. Display crypto currency exchange rates

To display all cryptocurrency exchange rates in Terminal, run:

$ curl rate.sx

To display a specific currency rate, for example BTC, run:

$ curl rate.sx/btc

22. Check your CPU compatibility

To check your CPU compatibility i.e 32 bit  or 64 bit, run:

$ lscpu | grep mode

Do you want to know whether your system is 32 bit or 64 bit? Refer the following guide.

23. Quickly copy or backup files

To quickly copy or backup a file, use this command:

$ cp ostechnix.txt{,.bak}

This command will copy the file named "ostechnix.txt" to a file named "ostechnix.txt.bak". This can be useful for making backups of configuration files before editing them.

24. Create files of specific permissions

To create files with specific permission on the fly, run:

$ install -b -m 777 /dev/null file.txt

Here, -b flag is used to take backup of the file if it already exists.


Related read:


25. Playing multiplayer Tron game in your Terminal

$ ssh sshtron.zachlatta.com

Use W, A, S, D keys for movement. It is useful to kill your boring time.

26. Display a sequence of numbers in Terminal

$ echo {01..10}

This command will display the numbers from 01 to 10.

27. Arch Linux news on Terminal

To display the latest Arch Linux news in your Terminal, use w3m text browser like below:

$ w3m https://www.archlinux.org/ | sed -n "/Latest News/,/Older News/p" | head -n -1

Make sure you have installed w3m text browser. w3m is available in the default repositories of most Linux distributions.

28. Create encrypted (password-protected) file using Vim

$ vim -x ostechnix.txt

Enter the encryption key twice.

To remove the password, open the file using vim:

$ vim ostechnix.txt

And type:

:set key=

Finally type :wq to save and close the file.

Also use CryptoGo utility to password-protect files.

29. Watch ASCII episode of Star Wars IV in Terminal

$ telnet towel.blinkenlights.nl

Please be mindful that you can't pause, rewind once the movie starts. Be prepared to watch the entire episode in a single sitting.

Here is another link to Watch Star wars:

$ nc towel.blinkenlights.nl 23

30. List hidden files and directories first

$ ls -alv

31. Find and delete specific type of files

To find and delete all files of certain type, for example "PDF", run:

$ find . -name '*.pdf' | xargs rm -v

Double check before you running this command. You may accidentally run it in wrong directory and delete all data.

32. Display disk usage in human readable format

Display disk usage of all files and directories in human readable format:

$ du -ah

Display only the total disk usage (summary) of current directory:

$ du -sh

33. How to use Vim editor if ESC key is broken

To use vim editor on a system where ESC key doesn't work, use the following keyboard shortcut:

  • CTRL+[

34. Reset and erase all characters in Terminal at once

To reset and erase all characters entered at  Unix password prompt, press:

  • CTRL+ALT+u

Before I know this tip, I hit BACKSPACE key repeatedly to erase the characters.

35. List upgradble packages on DEB-based systems

To view the list of packages to be upgradable on Debian based systems, use:

$ apt-get list --upgradable

36. Find "ext" filesystem mount time

To find when was an "ext" filesystem last mounted, run:

$ sudo tune2fs -l /dev/sdaX

Where "x" is the partition number like sda1, sda2

Example:

$ sudo tune2fs -l /dev/sda1

Or,

$ sudo tune2fs -l /dev/sda1 | grep "Last mount time"

You can also use this command to check how many times the file system has been mounted and when was the file system created .

37. Useful BASH shortcuts

Here are some useful BASH shortcut keys.

  • CTRL+r : Search command history
  • CTRL+l : Clears the Terminal screen. (Here l is the letter L)
  • CTRL+c : Cancels the running command.
  • CTRL+z : Suspends the running command.
  • CTRL+u : Delete the entire line before the cursor.
  • CTRL+k : Delete the entire line after the cursor.
  • CTRL+t : Interchange the last two characters before the cursor. useful to correct mistyped commands.
  • CTRL+d : Close the Terminal.

More Bash shortcuts are given in the following guide.

That's all for the first part. Read the second part of this series in the link given below.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

3 comments

Todd January 4, 2018 - 6:34 pm

This is a good summary, some of the commands I forgot, thank you for sharing, I have updated some of my command history, lol. I appreciate all of the items provided, I have provided a few for your review as well, always good to share:

-> Present information about the disks running on the system with filesystems
* for i in $(fdisk -l | awk ‘/^/dev/ {print $1}’); do tune2fs -l $i; done

-> https://stackoverflow.com/questions/14768609/remove-duplicates-entries-from-multiple-text-file-in-perl
* sort –unique Remove_duplicate.txt
* perl -lne ‘$seen{$_}++ and next or print;’ Remove_duplicate.txt

-> Replace items – https://www.garron.me/en/bits/pearl-pie-search-replace-substitute-text-all-files-terminal.html
* perl -pi -e ‘s///’

-> Create ticker from rate.sx, if watch is run, it presents unreadable symbols
* while true ; do curl rate.sx; done
* while sleep 1; do curl rate.sx; done

I thought this discussion was good as well, this may be good for your next iteration of your webpage.

Please keep them coming, wonderful place to share thoughts.

Todd

Reply
SK January 5, 2018 - 12:24 pm

Thank you Todd.

Reply
elswerky January 5, 2019 - 4:02 pm

number 33
for me it worked by only ctrl+u without alt

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More