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

Some Random One-liner Linux Commands [Part 2]

By sk
Published: Last Updated on 1.2K views

This is 2nd part of one-liner Linux commands series to make your command line life easier, better and faster. 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?

Random One-liner Linux Commands

  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.

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

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

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

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

  1. To upgrade a single package using "APT" package manager, use:
$ sudo apt-get install --only-upgrade <package-name>
  1. 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.

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

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

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

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

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

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

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

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

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

  1. 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 
$ cat file2.txt

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

$ cat file1.txt file2.txt 

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

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

  1. To find the type of a file, use:
$ file <file-name>


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

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

The third part in this series is ready and live now. Click the link below to read it.

You May Also Like

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