Thursday, March 1, 2018

Terminal Commands : Some Random One-liner Linux Commands

Some Random One-liner Linux Commands


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.
1. Let us start this part with Emacs. If you have Emacs installed on your system, press CTRL+x and CTRL+e to open the Emacs editor containing whatever you just typed in your Terminal. For example, type “ls” on your Terminal and press CTRL+x followed by CTRL+e. Now the Emacs editor will automatically open with “ls” on it.
2. To list files and directories in the current working directory, type:
$ du -sxh * | sort -h
The smallest items will be showed on top.
3. To go back to the previous working directory from any location, type:
$ cd -
Please note the space between cd and – (dash).
To go back to your $HOME directory from any location, the command is:
$ cd
It doesn’t matter which is your previous working directory. The “cd” command will take you from any location to your $HOME directory.
4. Some times I forget to add “sudo” when I am editing Apache configuration files using vim editor. The result? I couldn’t save the file after making some changes. It will throw the permission denied error if I try to save. This cool trick helped me to save a file edited in vim without the needed permissions.
:w !sudo tee %
You don’t need to quit the vim and re-edit the file with sudo permission. After making the changes, just type above line to save it with sudo permissions. It is really cool trick when you forget to add “sudo” when editing files.
5. To recall a command with a specific prefix from the BASH history without executing the command, type:
$ !su:p
The above command will recall the last command with prefix “su”, but it will not run it. As you see in the above picture, my last command with prefix “su” was – sudo netctl restart wlp9s0sktab.
6. To continuously monitor the output of a file, use this command:
$ tail -f /var/log/pacman.log
This command will be useful to monitor a file that produces output a lot.
7. To upgrade a single package using “APT” package manager, use:
$ sudo apt-get install --only-upgrade <package-name>
8. If you accidentally modified or overwrote your .bashrc file, you can restore to its default settings by copying the default copy from skel folder.
$ cp /etc/skel/.bashrc ~/
Be aware that this command will overwrite all changes in your existing .bashrc file.
9. To automatically “ls” when changing directory, add the following lines in your .bashrc file.
cd() {        
     builtin cd "$@" && ls -lA
}
To update the changes made in your .bashrc file, run:
$ source ~/.bashrc
Now cd to any directory. You will see the list of files and folders including the hidden items in it. Please be mindful that it will be annoying sometimes you change to directory that contains hundreds of files/folders.
10. To display the total numbers of files and folders in the current directory, run:
$ echo $(($(ls -l | wc -l) - 1))
To display total number files/folders including hidden files, type:
$ echo $(($(ls -lA | wc -l) - 1))
11.To download a .deb package with all required dependencies without installing it, use this command:
$ sudo apt-get install --download-only vim
This command will download “vim” package with all necessary dependencies, but it will not install it.
For more details, refer this link.
  • Download Packages With Dependencies Locally In Ubuntu

Download Packages With Dependencies Locally In Ubuntu

We can do this in two methods. For the purpose of this tutorial, I will be using Ubuntu 16.04 LTS desktop.

Method 1:

This is the simplest and straight forward method than everything else.
To download a package with all dependencies without installing them, run:
sudo apt-get install --download-only <package_name>
All downloaded files will be saved in /var/cache/apt/archives directory. Just copy the entire cache folder on any USB or transfer them via network to any system where you wanted to install the packages.
To install the downloaded packages, go to the cache folder and install them as shown below.
sudo dpkg -i *
See? It is easier than you ever thought, isn’t? Well, let us see the another method.

Method 2:

First, download the dependencies of the package you wanted to download.
To display list of all dependencies of a package, for example Python, run:
sudo apt-cache depends python
Sample output:
python
PreDepends: python-minimal
Depends: python2.7
Depends: libpython-stdlib
Conflicts: <python-central>
Breaks: update-manager-core
Suggests: python-doc
Suggests: python-tk
Replaces: python-dev
Let us download python package with its dependencies to our local disk.
To do so, first create a directory to save the packages.
mkdir python
Go to the directory:
cd python
And then run:
for i in $(apt-cache depends python | grep -E 'Depends|Recommends|Suggests' | cut -d ':' -f 2,3 | sed -e s/'<'/''/ -e s/'>'/''/); do sudo apt-get download $i 2>>errors.txt; done
The above command will download Python package along with all required dependencies and saves them in the current working directory. This command will also save any errors in the errors.txt file.
Let us view the downloaded files using ‘ls’ command:
ls
Sample output:
errors.txt
libpython-stdlib_2.7.11-1_amd64.deb
python2.7_2.7.11-7ubuntu1_amd64.deb
python-doc_2.7.11-1_all.deb
python-minimal_2.7.11-1_amd64.deb
python-tk_2.7.11-2_amd64.deb
Download packages locally in Ubuntu
As you see in the above output, python package with all its dependencies has been downloaded.
Just copy them to your USB drive and install the python packages on any offline system as shown below.
Mount the USB drive, go to the location where you have mounted the drive, and run the following command to install Python.
sudo dpkg -i *

You can also download a .rpm package with all dependencies without installing it. Check this below link to know how.
  • How To Download A RPM Package With All Dependencies In CentOS

Download A RPM Package With All Dependencies Using “Downloadonly” plugin

We can easily download any RPM package with all dependencies using “Downloadonly” plugin for yum command.
To install Downloadonly plugin, run the following command as root user.
yum install yum-plugin-downloadonly
Now, run the following command to download a RPM package.
yum install --downloadonly <package-name>
By default, this command will download and save the packages in /var/cache/yum/ in rhel-{arch}-channel/packages location. However, you can download and save the packages in any location of your choice using “–downloaddir” option.
yum install --downloadonly --downloaddir=<directory> <package-name>
Example:
yum install --downloadonly --downloaddir=/root/mypackages/ httpd
Sample output:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * epel: epel.mirror.angkasa.id
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos.4 for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package Arch Version Repository Size
=======================================================================================================================================
Installing:
 httpd x86_64 2.4.6-40.el7.centos.4 updates 2.7 M
Installing for dependencies:
 apr x86_64 1.4.8-3.el7 base 103 k
 apr-util x86_64 1.5.2-6.el7 base 92 k
 httpd-tools x86_64 2.4.6-40.el7.centos.4 updates 83 k
 mailcap noarch 2.1.41-2.el7 base 31 k

Transaction Summary
=======================================================================================================================================
Install 1 Package (+4 Dependent packages)

Total download size: 3.0 M
Installed size: 10 M
Background downloading packages, then exiting:
(1/5): apr-1.4.8-3.el7.x86_64.rpm | 103 kB 00:00:01 
(2/5): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:01 
(3/5): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:01 
(4/5): httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm | 83 kB 00:00:01 
(5/5): httpd-2.4.6-40.el7.centos.4.x86_64.rpm | 2.7 MB 00:00:09 
---------------------------------------------------------------------------------------------------------------------------------------
Total 331 kB/s | 3.0 MB 00:00:09 
exiting because "Download Only" specified
rootserver1_001
Now go the location that you specified in the above command. You will see there the downloaded package with all dependencies. In my case, I have downloaded the packages in /root/mypackages/ directory.
Let us verify the contents.
ls /root/mypackages/
Sample output:
apr-1.4.8-3.el7.x86_64.rpm
apr-util-1.5.2-6.el7.x86_64.rpm
httpd-2.4.6-40.el7.centos.4.x86_64.rpm
httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm
mailcap-2.1.41-2.el7.noarch.rpm
rootserver1_003
As you see in the above output, the package httpd has been downloaded with all dependencies.
Please note that this plugin is applicable for “yum install/yum update” and not for “yum groupinstall”. By default this plugin will download the latest available packages in the repository. You can however download a particular version by specifying the version.
Example:
yum install --downloadonly --downloaddir=/root/mypackages/ httpd-2.2.6-40.el7
Also, you can download multiple packages at once as shown below.
yum install --downloadonly --downloaddir=/root/mypackages/ httpd vsftpd
Method 2 – Download A RPM Package With All Dependencies Using “Yumdownloader” utility
Yumdownloader is a simple, yet useful command-line utility that downloads any RPM package along with all required dependencies in one go.
Install Yumdownloader using the following command as root user.
yum install yum-utils
Once installed, run the following command to download a package, for example httpd.
yumdownloader httpd
To download packages with all dependencies, use –resolve option:
yumdownloader --resolve httpd
By default, Yumdownloader will download the packages in the current working directory.
To download packages along with all dependencies to a specific location, use –destdir option:
yumdownloader --resolve --destdir=/root/mypackages/ httpd
Or
yumdownloader --resolve --destdir /root/mypackages/ httpd
Sample output:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * epel: epel.mirror.angkasa.id
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos.4 for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution
(1/5): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:01 
(2/5): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:02 
(3/5): apr-1.4.8-3.el7.x86_64.rpm | 103 kB 00:00:02 
(4/5): httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm | 83 kB 00:00:03 
(5/5): httpd-2.4.6-40.el7.centos.4.x86_64.rpm | 2.7 MB 00:00:19
rootserver1_004
Let us verify whether packages have been downloaded in the specified location.
ls /root/mypackages/
Sample output:
apr-1.4.8-3.el7.x86_64.rpm
apr-util-1.5.2-6.el7.x86_64.rpm
httpd-2.4.6-40.el7.centos.4.x86_64.rpm
httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm
mailcap-2.1.41-2.el7.noarch.rpm
rootserver1_005
Unlike “Downloadonly” plugin, Yumdownload can download the packages related to a particular group.
yumdownloader "@Development Tools" --resolve --destdir /root/mypackages/
Personally, I prefer Yumdownloader over “Downloadonly” plugin for yum. But, both are extremely easy and handy and does the same job.


12. To list all installed packages on a Debian based system, run:
$ dpkg -l
To display the files installed and path details of a given package, use:
$ dpkg -L <package-name>

One-liner Linux Commands

1. To find when a package was installed on Fedora, RHEL, CentOS, run:
$ rpm -q --last <package-name>
Example:
$ rpm -q --last nano
nano-2.3.1-10.el7.x86_64 Wed 28 Feb 2018 05:17:35 PM IST
2. To list all packages associated with a particular language, for example Spanish, in RPM-based systems like RHEL, Fedora, CentOS, run:
$ yum langinfo es
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.iitm.ac.in
 * epel: epel.mirror.angkasa.id
 * extras: ftp.iitm.ac.in
 * updates: mirrors.nhanhoa.com
Language-Id=es
 autocorr-es
 gimp-help-es
 gnome-getting-started-docs-es
 hunspell-es
 hyphen-es
 kde-l10n-Spanish
 libreoffice-langpack-es
 man-pages-es
 mythes-es
3. At times, you might need to copy a file to multiple directories. Here is the one-liner command example to copy a file to multiple directories:
$ find dir1/ dir2/ -type d -exec cp file.txt {} \;
In the above example, we copy file.txt to dir1 and dir2 at once.

Suggested read:

4. List the contents of a directory, sorted by access time:
$ ls -ltu

Suggested read:

5. To quickly create a file with some contents:
$ cat > file.txt <<< 'Welcome To OSTechNix'
Here, I created a file named file.txt with contents “Welcome To OSTechNix”.
$ cat file.txt 
Welcome To OSTechNixTHis
6. To shutdown your Linux box at a specific time, for example 9PM, run:
# shutdown -h 21:00
To reboot at a specific time, for example 9PM, run:
# shutdown -r 21:00
7. Normally, we do the following to remove package in YUM-based systems:
$ sudo yum remove <package-name>
To install a package, we do:
$ sudo yum install <package>
How about combining these two commands into one?
To remove one package and install another package at the same time in RHEL, CentOS, run:
$ sudo yum swap nano emacs
This command will first remove “nano” package and then install “emacs”. This is one of the best option I found in YUM package manager.
8. Looking for a CLI utility to crop your media files between a specific time intervals. I suggest you to use “ffmepg”. If you haven’t installed it already, refer the following guide.
Once ffmpeg installed, you can a audio/video file using start and stop times using as shown below.
$ ffmpeg -i input.mp3 -ss 00:01:54 -to 00:06:53 -c copy output.mp3
The above command will crop “input.mp3” file starting from 1:54 minutes to ending time 6:53 and save the final output in a separate file named “output.mp3”.

Suggested read:

9. We used to use “cat” command to displaying file’s output. Did you know we can also use “awk” command to display the contents of a file like below?
$ awk '{print}' file.txt
The above command is equivalent to “cat file.txt” command.
10. A one liner Linux command to assign multiple permissions to a file/directory at once:
$ chmod g+w,o-rw,a+x <path-to-file-or-directory>
This command assigns write permission to “group” members, removes read/write permissions from “other” users, and assigns the execute permission to “all” to the given file or directory.
11. Print detailed usage of each sub-directory in a directory in human-readable format:
$ du ostechnix/ -bh | more
This command displays the disk usage of all sub-directories in the directory named “ostechnix”.
12. Split files at a particular line:
$ csplit file.txt 3
This command splits file.txt at line number 3 and saves the output in two separate files namely xx00 and xx01.
13. Display file’s output in reverse order:
I have a file named file.txt with following contents:
$ cat file.txt 
Welcome To OSTechNix
Daily Linux Tips
Now let me reverse the above file’s output:
$ rev file.txt 
xiNhceTSO oT emocleW
spiT xuniL yliaD
See? The “rev” command reverses the order of the characters in every line.
14. Feel bored at work or want to impress your female/male colleague, here is an interesting command that I came across a few days ago.
Simulate on-screen typing like in the movies:
$ echo "Welcome to OSTechNix" | pv -qL 5
This command will print the characters in the given sentence (i.e Welcome to OSTechNix) in your Terminal at 5 per second. You can change the time interval as per your wish.
Please note that “pv” command should be installed in your system. PV is available in the default repositories of Arch-based systems and DEB-based systems. On RHEL/CentOS, you need to enable EPEL repository and then install pv command.
15. The following command will delete all files that doesn’t match the specific extensions. In other words, we delete all files in a current working directory except the given file types. Take a look at the following command:
$ rm !(*.txt|*.mp3|*.zip)
This command will delete all files that doesn’t match .txt, .mp3, .zip extensions in the current working directory. To put this simply, it will keep .txt, .mp3, .zip type files and delete all other files. Here ! operator specifies not.
Please be very careful while using this command. You may unknowingly delete the files in a wrong directory. Make sure you’re in the right directory and make sure you have specified the correct file extensions.
16. Create a directory and cd into it with a single command:
$ mkdir /home/sk/ostechnix && cd $_
This will command will create directory named “ostechnix” inside “/home/sk/” location and and cd into it immediately.

No comments: