Some Random One-liner Linux Commands [Part 2]

One-liner Linux Commands

If you are following our social network pages, 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. At the end of every month, We gather all commands that we published each day in that month, put them all together in a single article and publish it. Additionally, we include some other tips and tricks to learn Linux stuffs. The first part in this series is already live. You may want to read it from the link below.

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 second part in the series. We will be publishing the subsequent parts at every month-end. Let’s get started, shall we?

One-liner Linux Commands [Part 2]

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 "[email protected]" && 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.

You can also download a .rpm package with all dependencies without installing it. Check this below link to know how.

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>

Recommended read:


13. To display current system status, time, duration, list of currently logged-in users and other user details, run:

$ w

14. To display the disk usage of all files and directories (including the hidden files) in the current working directory in human readable format, use this command:

$ du -sch .[!.]* *

For more “du” command examples, refer this link.

15. Some times you may want to write the output of a particular command to multiple files and send it to your colleagues, friends. If so, you can do it like below.

$ uname -r | tee file1 file2 file3

This command writes the output of “uname -r” command to file1, file2 and file3. If the files doesn’t exist, it will create them and write the output. One disadvantage of using this command is it will overwrite the contents of the files. If you don’t want that, you can append the output using “-a” flag as shown below.

$ uname -r | tee -a file1 file2 file3

16. To alphabetically sort a file and display the output in a single line:

$ cat <file-name> | tr '|' '\n' | sort | tr '\n' '|' | sed "s/.$/\\n/g"

For example, I have a file named “example.txt” with following contents:

$ cat example.txt 
abc
cab
bac
bca
cba

Now let us sort the contents of this file alphabetically and display the result in a single line. To do so, run:

$ cat example.txt | tr '|' '\n' | sort | tr '\n' '|' | sed "s/.$/\\n/g" 
abc|bac|bca|cab|cba

17. Are you willing to learn JavaScript, then head over to the following site.

This is a community supported website where people compose 140 characters JavaScript programs that produce interesting visuals.

18. To delete the specific characters in a given command, do:

$ echo 'Hell1o, Welcome1 2to OSTechNix4' | tr -d '1-9'

The above command will delete the numbers from 1 to 9 and display the final output.

19. Is the “BACKSPACE” key not working in your keyboard? No worries! You still can delete the characters on the Terminal in a system where BACKSPACE key doesn’t work by pressing “CTRL+h” keys.

20. To view all users who recently logged in in a system, type:

$ last

To know when was the last time an user logged in, type:

$ last <username>

To view all bad login attempts, type:

$ sudo lastb

21. Are you setting up a testing webserver using Nginx? You can use this free online Nginx configuration generator for general purposes:

Just enter the details such as domain, doucumentroot etc., in the respective column and it will automatically generate Nginx configuration based on your input as you type. You can just copy/paste it in your /etc/nginx/nginx.conf file. This is really useful for learning and testing purposes.

22. To merge two files side by side, each in its column, using TAB as delimiter, you can use this command:

$ paste file1 file2

Let us say you have two files namely file1.txt and file2.txt with following contents.

$ cat file1.txt 
Apple
Orange
Mango
$ cat file2.txt
30
25
40

If you use cat command to view the output of both files, the output will be:

$ cat file1.txt file2.txt 
Apple
Orange
Mango
30
25
40

But if you use paste command, you will see fields have properly been organized in a neat column like below.

$ paste file1.txt file2.txt

23. To find the last access time of a file, run:

$ stat -c %x file

To find the last modification time of a file, use:

$ stat -c %y file

Also read:


24. To find the type of a file, use:

$ file <file-name>

Example:

$ file ostechnix 
ostechnix: ASCII text
$ file image 
image: PNG image data, 330 x 146, 8-bit colormap, non-interlaced

This could be useful if the given file has no extension.

25. To save the output of multiple commands as a single line in a text file, the command would be:

$ echo "$(uname -r) $(hostname -i)" >> output.txt

The above command writes the output of “uname -r” and “hostname -i” commands to a file file named “output.txt”.

And, that’s all for this month. This month I came across 23 one-liner commands and 2 websites that I just shared here. For more details of the each command, refer its man pages.

I hope you find this useful. I will meet you all with the third part of this series at next month-end. Until then, stay tuned with OSTechNix!

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 *