Some Random One-liner Linux Commands [Part 1]

One-liner Linux Commands

If you are following our social networks, you might have noticed that we share some one-liner Linux commands via image templates everyday. Those commands are just single line commands that makes your command line life easier and better. We have decided to gather all commands that we published each day in the month, put them all together in a single article and publish it at the end of every month. Additionally, I have included some other tips and tricks 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 any typos, mistakes in commands, let me know in the comment section. I will update them ASAP.

1. 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

2. 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*

3. 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

4. Find files bigger than X size, for example 10 MB, and sort them by size:

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

5. To run Linux commands non-interactively, use “yes” command like below.

$ yes | rm -r <directory-or-file>

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 useful in scripts. It’s also dangerous. You might accidentally do some damages. Be cautious when using “yes” command.

6. Recall “N”th command from your BASH history without executing it:

$ !12:p

The above command will display 12th command from the history, but it won’t execute it.

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

$ man hier

8. If you don’t know what a particular command will do, copy/paste that command in the following site.

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

9. To use the Terminal on a system where the ENTER key doesn’t work, use:

  • CTRL+j or CTRL+m

10. Find all broken sym links in your system:

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

11. Monitor CPU speed in real time:

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

Press CTRL+c to stop monitoring.

12. Find the exact installation and date a Linux OS, run the following command:

Arch Linux:

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

If the logs are already delete, use the following commands instead. Run these 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:

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

This command will display the top 15 most used commands.

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

$ journalctl -u sleep.target

15. 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 apt-get update && sudo apt-get upgrade

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

The “;” operator executes the second command even if the first command was successful or fail.

Example:

sudo apt-get update ; sudo apt-get upgrade

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

16. To monitor Kernel messages in live:

$ dmesg -wx

To stop monitoring press CTRL+c.

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.

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

$ systemctl is-enabled bluetooth-service

19. Delete all consecutive duplicate lines in a file:

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

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

20. List all resolutions supported by your X:

$ xrandr

To change X’s resolution on the fly:

$ xrandr -s 1024x760

21. Display crypto currency exchange rates from command line:

$ curl rate.sx

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

$ lscpu | grep mode

23. 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. To create files with specific permission on the fly:

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

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

25. Play 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. Display the latest Arch Linux news in your Terminal:

$ 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.

28. Create a 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.

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.

30. List hidden files and directories first:

$ ls -alv

31. Find and delete all files of certain type. for example “.PDF”:

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

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

32. 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. To use vim editor on a system where ESC key doesn’t work, use:

  • CTRL+[

34. 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. To view the list of packages to be upgradable on Debian based systems, use:

$ apt-get list --upgradable

36. 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. 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.

Read the second part of this series in the link given below.

And, that’s all for now. More good stuffs to come. Stay tuned!

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

2 Responses

  1. Todd says:

    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

Leave a Reply

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