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.
Table of Contents
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:
- Get Notification When A Terminal Task Is Done
- Undistract-me : Get Notification When Long Running Terminal Commands Complete
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:
- 5 Ways To Repeat Your Last Command In Linux
- Apply Tags To Linux Commands To Easily Retrieve Them From History
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:
- How To Display CPU Usage From Commandline
- How To View CPU Temperature On Linux
- Hegemon – A Modular System Monitor Application Written In Rust
- CPU Power Manager – Control And Manage CPU Frequency In Linux
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.
17. 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.
18. 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:
- How To Exclude Specific Directories From Copying In Linux
- How To Remove All Files In A Folder Except One Specific File In Linux
- How To Exclude Certain Size Files From Copying In Linux
- How To Find and Copy Certain Type Of Files From One Directory To Another In Linux
19. Check service status
To check if a particular service is enabled or not at startup, use:
$ systemctl is-enabled bluetooth-service
20. 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:
21. 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.
22. 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
23. 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.
24. 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.
25. 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:
26. 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.
27. Display a sequence of numbers in Terminal
$ echo {01..10}
This command will display the numbers from 01 to 10.
28. 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.
29. 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.
30. 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
31. List hidden files and directories first
$ ls -alv
32. 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.
33. 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
34. 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+[
35. 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.
36. 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
37. 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 .
38. 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 other parts of this series in the links given below.
5 comments
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
Thank you Todd.
number 33
for me it worked by only ctrl+u without alt
Hi SK,
Thanks for the excellent insight shared in https://ostechnix.com/copy-files-change-ownership-permissions-time/ on the one liner command to copy and change ownership.
I tried to post a question on that page. But got an error as it was trying to redirect to 127.0.0.1
My scenario and questions are as below.
—————comment below————
I am replacing the hard drive in an old linux laptop with a new SSD. I am also doing a fresh install of a Linux distro on the new SSD (with new user ids etc). After removing the old hard drive, I am planning to connect it to the laptop using a “USB 3.0 to SATA” cable and copy the data files (home folders and a ton of other data from different partitions) to the SSD. Can I use this command to copy all the data files and manage permissions based on the user ids in the new linux instance installed on the SSD?
Thanks for your help.
Yes, you can. Just make sure the source and destination paths are correct. But why would you want to do that? Just do a regular copy/paste. The ownership of old files will be changed to new user.