Home Linux Commands Some Random One-liner Linux Commands [Part 3]

Some Random One-liner Linux Commands [Part 3]

By sk
Published: Updated: 374 views

This is the third part of "some random one-liner Linux commands" article series. As you might already noticed, we collect all commands that we share daily via image templates in our social and professional networks and put them all together in a single article and publish it at the end of each month. Some of the one-liner commands provided here are the ones that I use everyday at work and some are collected from various Linux forums and websites such as Askubuntu, Reddit, and Stack Exchange.

If you haven't read the previous parts yet, check the following links.

Now let us see this month's one-liner Linux commands.

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


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

Suggested read:


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

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


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

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

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

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

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

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

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

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

Go to the following link to learn more Linux one-liners.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

1 comment

Mahmoud Elswerky January 5, 2019 - 9:05 pm

thank you

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. By using this site, we will assume that you're OK with it. Accept Read More