Some Random One-liner Linux Commands [Part 3]

One-liner Linux Commands - part 3

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

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.

And, that’s all for now. I hope this part has brought some useful Linux commands that you may not aware of. The fourth part of this series is live now. Go to the following link to learn more Linux one-liners.

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

Leave a Reply

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