5 Ways to Keep Remote SSH Sessions and Processes Running After Disconnection

SSH or Secure Shell in simple terms is a way by which a person can remotely access another user on other system but only in command line i.e. non-GUI mode. In more technical terms, when we ssh on to other user on some other system and run commands on that machine, it actually creates a pseudo-terminal and attaches it to the login shell of the user logged in.

Keep SSH Sessions Running After Disconnection

5 Ways to Keep SSH Sessions Running After Disconnection

When we log out of the session or the session times out after being idle for quite some time, the SIGHUP signal is send to the pseudo-terminal and all the jobs that have been run on that terminal, even the jobs that have their parent jobs being initiated on the pseudo-terminal are also sent the SIGHUP signal and are forced to terminate.

Don’t Miss: 5 Useful Practices to Keep SSH Server Secure and Protected

Only the jobs that have been configured to ignore this signal are the ones that survive the session termination. On Linux systems, we can have many ways to make these jobs running on the remote server or any machine even after user logout and session termination.

Understand Processes on Linux

Normal Process

Normal processes are those which have life span of a session. They are started during the session as foreground processes and end up in certain time span or when the session gets logged out. These processes have their owner as any of the valid user of the system, including root.

Orphan Process

Orphan processes are those which initially had a parent which created the process but after some time, the parent process unintentionally died or crashed, making init to be the parent of that process. Such processes have init as their immediate parent which waits on these processes until they die or end up.

Daemon Process

These are some intentionally orphaned processes, such processes which are intentionally left running on the system are termed as daemon or intentionally orphaned processes. They are usually long-running processes which are once initiated and then detached from any controlling terminal so that they can run in background till they do not get completed, or end up throwing an error. Parent of such processes intentionally dies making child execute in background.

Techniques to Keep SSH Session Running After Disconnection

There can be various ways to leave ssh sessions running after disconnection as described below:

1. Using screen Command to Keep SSH Sessions Running

screen is a text Window Manager for Linux which allows user to manage multiple terminal sessions at same time, switching between sessions, session logging for the running sessions on screen, and even resuming the session at any time we desire without worrying about the session being logged out or terminal being closed.

screen sessions can be started and then detached from the controlling terminal leaving them running in background and then be resumed at any time and even at any place. Just you need to start your session on the screen and when you want, detach it from pseudo-terminal (or the controlling terminal) and logout. When you feel, you can re-login and resume the session.

Starting a screen Session

After typing ‘screen’ command, you will be in a new screen session, within this session you can create new windows, traverse between windows, lock the screen, and do many more stuff which you can do on a normal terminal.

$ screen

Starting Screen Session in Linux

Starting Screen Session in Linux

Once screen session started, you can run any command and keep the session running by detaching the session.

Run Commands in Screen Session

Run Commands in Screen Session

Detaching a Screen

Just when you want to log out of the remote session, but you want to keep the session you created on that machine alive, then just what you need to do is detach the screen from the terminal so that it has no controlling terminal left. After doing this, you can safely logout.

To detach a screen from the remote terminal, just press “Ctrl+a” immediately followed by “d” and you will be back to the terminal seeing the message that the Screen is detached. Now you can safely logout and your session will be left alive.

Detaching Linux Screen Session

Detaching Linux Screen Session

Resuming Detached Screen Session

If you want to Resume a detached screen session which you left before logging out, just re-login to remote terminal again and type “screen -r” in case if only one screen is opened, and if multiple screen sessions are opened run “screen -r <pid.tty.host>”.

$ screen -r
$ screen -r <pid.tty.host>

Resume Detached Screen Session

Resume Detached Screen Session

To Learn more about screen command and how to use it just follow the link: Use screen Command to Manage Linux Terminal Sessions

2. Using Tmux (Terminal Multiplexer) to Keep SSH Sessions Running

Tmux is another software which is created to be a replacement for screen. It has most of the capabilities of screen, with few additional capabilities which make it more powerful than screen.

It allows, apart from all options offered by screen, splitting panes horizontally or vertically between multiple windows, resizing window panes, session activity monitoring, scripting using command line mode etc. Due to these features of tmux, it has been enjoying wide adoption by nearly all Unix distributions and even it has been included in the base system of OpenBSD.

Start a Tmux Session

After doing ssh on the remote host and typing tmux, you will enter into a new session with a new window opening in front of you, wherein you can do anything you do on a normal terminal.

$ tmux

Start tmux Terminal Session

Start tmux Terminal Session

After performing your operations on the terminal, you can detach that session from the controlling terminal so that it goes into background and you can safely logout.

Perform Linux Commands in Tmux Session

Perform Linux Commands in Tmux Session

Detach Tmux Session from Terminal

Either you can run “tmux detach” on running tmux session or you can use the shortcut (Ctrl+b then d). After this your current session will be detached and you will come back to your terminal from where you can log out safely.

$ tmux detach

Detach Tmux Session in Linux

Detach Tmux Session in Linux

Resuming the Closed Tmux Session

To re-open the session which you detached and left as is when you logged out of the system, just re-login to the remote machine and type “tmux attach” to reattach to the closed session and it will be still be there and running.

$ tmux attach

Resume Tmux Closed Session

Resume Tmux Closed Session

To Learn more about tmux and how to use it just follow the link: Use Tmux Terminal Multiplexer to Manage Multiple Linux Terminals.

3. Using nohup command to Keep Running SSH Sessions

If you are not that familiar with screen or tmux, you can use nohup and send your long running command to background so that you can continue while the command will keep on executing in background. After that you can safely log out.

With nohup command we tell the process to ignore the SIGHUP signal which is sent by ssh session on termination, thus making the command persist even after session logout. On session logout the command is detched from controlling terminal and keeps on running in background as daemon process.

Executing command using nohup in background

Here, is a simple scenario wherein, we have run find command to search for files in background on ssh session using nohup, after which the task was sent to background with prompt returning immediately giving PID and job ID of the process ([JOBID] PID).

# nohup find / -type f $gt; files_in_system.out 2>1 &

Run Linux Command in Background

Run Linux Command in Background

Resuming the session to view if job is still running

When you re-login again, you can check the status of command, bring it back to foreground using 'fg %JOBID' to monitor its progress and so on. Below, the output shows that the job was completed as it doesn’t show on re-login, and has given the output which is displayed.

# fg %JOBID

Run Linux Command in Background

Run Linux Command in Background

4. Using disown Command to Keep SSH Sessions Running

Another elegant way of letting your command or a single task run in background and remain alive even after session logout or disconnection is by using disown.

Disown, removes the job from the process job list of the system, so the process is shielded from being killed during session disconnection as it won’t receive SIGHUP by the shell when you logout.

Disadvantage of this method is that, it should be used only for the jobs that do not need any input from the stdinand neither need to write to stdout, unless you specifically redirect jobs input and output, because when job will try to interact with stdin or stdout, it will halt.

Executing command using disown in background

Below, we sent ping command to background so that ut keeps on running and gets removed from job list. As seen, the job was first suspended, after which it was still in the job list as Process ID: 15368.

$ ping tecmint.com > pingout &
$ jobs -l
$ diswon -h %1
$ ps -ef | grep ping

Run Linux Commands Using Disown

Run Linux Commands Using Disown

After that disown signal was passed to the job, and it was removed from job list, though was still running in background. The job would still be running when you would re-login to the remote server as seen below.

$ ps -ef | grep ping

Verify Job Status

Verify Job Status

5. Using setsid Command to Put SSH Sessions Running

Another utility to achieve the required behavior is setsidNohup has a disadvantage in the sense that the process group of the process remains the same so the process running with nohup is vulnerable to any signal sent to the whole process group (like Ctrl + C).

setsid on other hand allocates a new process group to the process being executed and hence, the process created is totally in a newly allocated process group and can execute safely without fear of being killed even after session logout.

Execute any command using setsid

Here, it shows that the process ‘sleep 10m’ has been detached from the controlling terminal, since the time it has been created.

$ setsid sleep 10m
$ ps -ef | grep sleep

Run Linux Command Using Setsid

Run Linux Command Using Setsid

Now, when you would re-login the session, you will still find this process running.

$ ps -ef | grep [s]leep

Linux Process Running Background

Linux Process Running Background

Conclusion

What ways you could think of to keep your process running even after you logout from SSH session? If there is any other and efficient way you can think of, do mention in your comments.

Source

PacVim – A Game That Teaches You Vim Commands

Although Vim (short for VI Improved) is a popular text editor on Linux systems, people still find it hard to learn, it has a steep learning curve especially the advanced features; a lot of Linux newbies are literally afraid of learning this powerful and highly recommended text editor.

On the other hand, so much effort has been directed by the Tecmint and Linux community towards making Vimeasy to learn; from creating Vim tutorials, sharing useful Vim usage tricks and tips, to developing interactive learning web-apps and command-line games such as PacVim.

PacVim is a free open source, text-based game that teaches you vim commands in a simple and fun manner. It is inspired by the popular and classic PacMan game, and runs on Linux and MacOSX. It helps you to comprehensively learn vim commands in an enjoyable way. Its objective is more or less like that of PacMan – you must move the pacman (the green cursor) over all the characters on the screen while avoiding the ghosts (red G).

How to Install PacVim Game in Linux

To install PacVim game, you need to first install required Curses (graphics library) package on your Linux distribution using default package manager as shown.

$ sudo apt install libncurses5-dev libncursesw5-dev  [On Ubuntu/Debian]
# yum install ncurses-devel                          [On CentOS/RHEL]
# dnf install ncurses-devel                          [On Fedora]

Next, download PacVim source files by cloning its repository and install it as shown.

$ cd ~/Downloads
$ git clone https://github.com/jmoon018/PacVim.git
$ cd PacVim
$ sudo make install

After installing PacVim, you can start learning vim commands by running it from level 0 and the default mode is hard.

$ pacvim

PacVIM Game for Learning Vim Commands

PacVIM Game for Learning Vim Commands

Here are a few keys to move the cursor:

  • h – move left
  • i – move right
  • j – move down
  • k – move up
  • q – quit the game

You can launch it in a specific level and mode (n and h for normal/hard respectively), for example.

$ pacvim n
OR
$ pacvim 2
OR
$ pacvim 2 n

You can find more information including key-usage combinations and how to create your custom maps from the PacVim Github repository.

Read AlsonSnake – Play Old Classic Snake Game in Linux Terminal

That’s all! PacVim is a useful game that teaches you vim commands while having fun with Linux terminal. Use the comment form below to share your thoughts or ask questions about it.

Source

7 Ways to Determine the File System Type in Linux (Ext2, Ext3 or Ext4)

A file system is the way in which files are named, stored, retrieved as well as updated on a storage disk or partition; the way files are organized on the disk.

A file system is divided in two segments called: User Data and Metadata (file name, time it was created, modified time, it’s size and location in the directory hierarchy etc).

In this guide, we will explain seven ways to identify your Linux file system type such as Ext2, Ext3, Ext4, BtrFS, GlusterFS plus many more.

1. Using df Command

df command reports file system disk space usage, to include the file system type on a particular disk partition, use the -T flag as below:

$ df -Th
OR
$ df -Th | grep "^/dev"

df Command - Find Filesystem Type

df Command – Find Filesystem Type

For a comprehensive guide for df command usage go through our articles:

  1. 12 Useful “df” Commands to Check Disk Space in Linux
  2. Pydf – An Alternative ‘df’ Command That Shows Disk Usage in Colours

2. Using fsck Command

fsck is used to check and optionally repair Linux file systems, it can also print the file system type on specified disk partitions.

The flag -N disables checking of file system for errors, it just shows what would be done (but all we need is the file system type):

$ fsck -N /dev/sda3
$ fsck -N /dev/sdb1

fsck - Print Linux Filesystem Type

fsck – Print Linux Filesystem Type

3. Using lsblk Command

lsblk displays block devices, when used with the -f option, it prints file system type on partitions as well:

$ lsblk -f

lsblk - Shows Linux Filesystem Type

lsblk – Shows Linux Filesystem Type

4. Using mount Command

mount command is used to mount a file system in Linux, it can also be used to mount an ISO imagemount remote Linux filesystem and so much more.

When run without any arguments, it prints info about disk partitions including the file system type as below:

$ mount | grep "^/dev"

Mount - Show Filesystem Type in Linux

Mount – Show Filesystem Type in Linux

5. Using blkid Command

blkid command is used to find or print block device properties, simply specify the disk partition as an argument like so:

$ blkid /dev/sda3

blkid - Find Filesystem Type

blkid – Find Filesystem Type

6. Using file Command

file command identifies file type, the -s flag enables reading of block or character files and -L enables following of symlinks:

$ sudo file -sL /dev/sda3

file - Identifies Filesystem Type

file – Identifies Filesystem Type

7. Using fstab File

The /etc/fstab is a static file system info (such as mount point, file system type, mount options etc) file:

$ cat /etc/fstab

Fstab - Shows Linux Filesystem Type

Fstab – Shows Linux Filesystem Type

That’s it! In this guide, we explained seven ways to identify your Linux file system type. Do you know of any method not mentioned here? Share it with us in the comments.

Source

The Complete Guide to “useradd” Command in Linux – 15 Practical Examples

We all are aware about the most popular command called ‘useradd‘ or ‘adduser‘ in Linux. There are times when a Linux System Administrator asked to create user accounts on Linux  with some specific properties, limitations or comments.

In Linux, a ‘useradd‘ command is a low-level utility that is used for adding/creating user accounts in Linux and other Unix-like operating systems. The ‘adduser‘ is much similar to useradd command, because it is just a symbolic link to it.

add users in linux

useradd command examples

In some other Linux distributions, useradd command may comes with lightly difference version. I suggest you to read your documentation, before using our instructions to create new user accounts in Linux.

When we run ‘useradd‘ command in Linux terminal, it performs following major things:

  1. It edits /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files for the newly created User account.
  2. Creates and populate a home directory for the new user.
  3. Sets permissions and ownerships to home directory.

Basic syntax of command is:

useradd [options] username

In this article we will show you the most used 15 useradd commands with their practical examples in Linux. We have divided the section into two parts from Basic to Advance usage of command.

  1. Part I: Basic usage with 10 examples
  2. Part II: Advance usage with 5 examples
Part I – 10 Basic Usage of useradd Commands

1. How to Add a New User in Linux

To add/create a new user, all you’ve to follow the command ‘useradd‘ or ‘adduser‘ with ‘username’. The ‘username’ is a user login name, that is used by user to login into the system.

Only one user can be added and that username must be unique (different from other username already exists on the system).

For example, to add a new user called ‘tecmint‘, use the following command.

[root@tecmint ~]# useradd tecmint

When we add a new user in Linux with ‘useradd‘ command it gets created in locked state and to unlock that user account, we need to set a password for that account with ‘passwd‘ command.

[root@tecmint ~]# passwd tecmint
Changing password for user tecmint.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

Once a new user created, it’s entry automatically added to the ‘/etc/passwd‘ file. The file is used to store users information and the entry should be.

tecmint:x:504:504:tecmint:/home/tecmint:/bin/bash

The above entry contains a set of seven colon-separated fields, each field has it’s own meaning. Let’s see what are these fields:

  1. Username: User login name used to login into system. It should be between 1 to 32 charcters long.
  2. Password: User password (or x character) stored in /etc/shadow file in encrypted format.
  3. User ID (UID): Every user must have a User ID (UID) User Identification Number. By default UID 0 is reserved for root user and UID’s ranging from 1-99 are reserved for other predefined accounts. Further UID’s ranging from 100-999 are reserved for system accounts and groups.
  4. Group ID (GID): The primary Group ID (GID) Group Identification Number stored in /etc/group file.
  5. User Info: This field is optional and allow you to define extra information about the user. For example, user full name. This field is filled by ‘finger’ command.
  6. Home Directory: The absolute location of user’s home directory.
  7. Shell: The absolute location of a user’s shell i.e. /bin/bash.

2. Create a User with Different Home Directory

By default ‘useradd‘ command creates a user’s home directory under /home directory with username. Thus, for example, we’ve seen above the default home directory for the user ‘tecmint‘ is ‘/home/tecmint‘.

However, this action can be changed by using ‘-d‘ option along with the location of new home directory (i.e./data/projects). For example, the following command will create a user ‘anusha‘ with a home directory ‘/data/projects‘.

[root@tecmint ~]# useradd -d /data/projects anusha

You can see the user home directory and other user related information like user id, group id, shell and comments.

[root@tecmint ~]# cat /etc/passwd | grep anusha

anusha:x:505:505::/data/projects:/bin/bash

3. Create a User with Specific User ID

In Linux, every user has its own UID (Unique Identification Number). By default, whenever we create a new user accounts in Linux, it assigns userid 500501502 and so on…

But, we can create user’s with custom userid with ‘-u‘ option. For example, the following command will create a user ‘navin‘ with custom userid ‘999‘.

[root@tecmint ~]# useradd -u 999 navin

Now, let’s verify that the user created with a defined userid (999) using following command.

[root@tecmint ~]# cat /etc/passwd | grep tecmint

navin:x:999:999::/home/tecmint:/bin/bash

NOTE: Make sure the value of a user ID must be unique from any other already created users on the system.

4. Create a User with Specific Group ID

Similarly, every user has its own GID (Group Identification Number). We can create users with specific group ID’s as well with -g option.

Here in this example, we will add a user ‘tarunika‘ with a specific UID and GID simultaneously with the help of ‘-u‘ and ‘-g‘ options.

[root@tecmint ~]# useradd -u 1000 -g 500 tarunika

Now, see the assigned user id and group id in ‘/etc/passwd‘ file.

[root@tecmint ~]# cat /etc/passwd | grep tarunika

tarunika:x:1000:500::/home/tarunika:/bin/bash

5. Add a User to Multiple Groups

The ‘-G‘ option is used to add a user to additional groups. Each group name is separated by a comma, with no intervening spaces.

Here in this example, we are adding a user ‘tecmint‘ into multiple groups like adminswebadmin and developer.

[root@tecmint ~]# useradd -G admins,webadmin,developers tecmint

Next, verify that the multiple groups assigned to the user with id command.

[root@tecmint ~]# id tecmint

uid=1001(tecmint) gid=1001(tecmint)
groups=1001(tecmint),500(admins),501(webadmin),502(developers)
context=root:system_r:unconfined_t:SystemLow-SystemHigh

6. Add a User without Home Directory

In some situations, where we don’t want to assign a home directories for a user’s, due to some security reasons. In such situation, when a user logs into a system that has just restarted, its home directory will be root. When such user uses su command, its login directory will be the previous user home directory.

To create user’s without their home directories, ‘-M‘ is used. For example, the following command will create a user ‘shilpi‘ without a home directory.

[root@tecmint ~]# useradd -M shilpi

Now, let’s verify that the user is created without home directory, using ls command.

[root@tecmint ~]# ls -l /home/shilpi

ls: cannot access /home/shilpi: No such file or directory

7. Create a User with Account Expiry Date

By default, when we add user’s with ‘useradd‘ command user account never get expires i.e their expiry date is set to 0 (means never expired).

However, we can set the expiry date using ‘-e‘ option, that sets date in YYYY-MM-DD format. This is helpful for creating temporary accounts for a specific period of time.

Here in this example, we create a user ‘aparna‘ with account expiry date i.e. 27th April 2014 in YYYY-MM-DDformat.

[root@tecmint ~]# useradd -e 2014-03-27 aparna

Next, verify the age of account and password with ‘chage‘ command for user ‘aparna‘ after setting account expiry date.

[root@tecmint ~]# chage -l aparna

Last password change						: Mar 28, 2014
Password expires						: never
Password inactive						: never
Account expires							: Mar 27, 2014
Minimum number of days between password change		        : 0
Maximum number of days between password change		        : 99999
Number of days of warning before password expires		: 7

8. Create a User with Password Expiry Date

The ‘-f‘ argument is used to define the number of days after a password expires. A value of 0 inactive the user account as soon as the password has expired. By default, the password expiry value set to -1 means never expire.

Here in this example, we will set a account password expiry date i.e. 45 days on a user ‘tecmint’ using ‘-e‘ and ‘-f‘ options.

[root@tecmint ~]# useradd -e 2014-04-27 -f 45 tecmint

9. Add a User with Custom Comments

The ‘-c‘ option allows you to add custom comments, such as user’s full namephone number, etc to /etc/passwd file. The comment can be added as a single line without any spaces.

For example, the following command will add a user ‘mansi‘ and would insert that user’s full name, Manis Khurana, into the comment field.

[root@tecmint ~]# useradd -c "Manis Khurana" mansi

You can see your comments in ‘/etc/passwd‘ file in comments section.

[root@tecmint ~]# tail -1 /etc/passwd

mansi:x:1006:1008:Manis Khurana:/home/mansi:/bin/sh

10. Change User Login Shell:

Sometimes, we add users which has nothing to do with login shell or sometimes we require to assign different shells to our users. We can assign different login shells to a each user with ‘-s‘ option.

Here in this example, will add a user ‘tecmint‘ without login shell i.e. ‘/sbin/nologin‘ shell.

[root@tecmint ~]# useradd -s /sbin/nologin tecmint

You can check assigned shell to the user in ‘/etc/passwd‘ file.

[root@tecmint ~]# tail -1 /etc/passwd

tecmint:x:1002:1002::/home/tecmint:/sbin/nologin
Part II – 5 Advance Usage of useradd Commands

11. Add a User with Specific Home Directory, Default Shell and Custom Comment

The following command will create a user ‘ravi‘ with home directory ‘/var/www/tecmint‘, default shell /bin/bashand adds extra information about user.

[root@tecmint ~]# useradd -m -d /var/www/ravi -s /bin/bash -c "TecMint Owner" -U ravi

In the above command ‘-m -d‘ option creates a user with specified home directory and the ‘-s‘ option set the user’s default shell i.e. /bin/bash. The ‘-c‘ option adds the extra information about user and ‘-U‘ argument create/adds a group with the same name as the user.

12. Add a User with Home Directory, Custom Shell, Custom Comment and UID/GID

The command is very similar to above, but here we defining shell as ‘/bin/zsh‘ and custom UID and GID to a user ‘tarunika‘. Where ‘-u‘ defines new user’s UID (i.e. 1000) and whereas ‘-g‘ defines GID (i.e. 1000).

[root@tecmint ~]# useradd -m -d /var/www/tarunika -s /bin/zsh -c "TecMint Technical Writer" -u 1000 -g 1000 tarunika

13. Add a User with Home Directory, No Shell, Custom Comment and User ID

The following command is very much similar to above two commands, the only difference is here, that we disabling login shell to a user called ‘avishek‘ with custom User ID (i.e. 1019).

Here ‘-s‘ option adds the default shell /bin/bash, but in this case we set login to ‘/usr/sbin/nologin‘. That means user ‘avishek‘ will not able to login into the system.

[root@tecmint ~]# useradd -m -d /var/www/avishek -s /usr/sbin/nologin -c "TecMint Sr. Technical Writer" -u 1019 avishek

14. Add a User with Home Directory, Shell, Custom Skell/Comment and User ID

The only change in this command is, we used ‘-k‘ option to set custom skeleton directory i.e. /etc/custom.skell, not the default one /etc/skel. We also used ‘-s‘ option to define different shell i.e. /bin/tcsh to user ‘navin‘.

[root@tecmint ~]# useradd -m -d /var/www/navin -k /etc/custom.skell -s /bin/tcsh -c "No Active Member of TecMint" -u 1027 navin

15. Add a User without Home Directory, No Shell, No Group and Custom Comment

This following command is very different than the other commands explained above. Here we used ‘-M‘ option to create user without user’s home directory and ‘-N‘ argument is used that tells the system to only create username (without group). The ‘-r‘ arguments is for creating a system user.

[root@tecmint ~]# useradd -M -N -r -s /bin/false -c "Disabled TecMint Member" clayton

For more information and options about useradd, run ‘useradd‘ command on the terminal to see available options.

Read Also15 usermod Command Examples

Source

Add Rainbow Colors to Linux Command Output in Slow Motion

In this article, we will review a cool and simple way of giving a command screen output a rainbow colors and slowing down its output speed as well for one reason or the other.

The lolcat program is used for the above purpose. It basically functions by concatenating files, or standard input, to standard output in a similar way as the cat command, overrides the default screen output color of a particular command and adds rainbow coloring to it.

How to Install Lolcat Program in Linux

Lolcat program is available on almost all modern Linux distributions from its default repository, but the available version bit older. Alternatively you can install latest version of lolcat from git repository using this following guide.

  1. Install Lolcat to Output Rainbow Of Colors in Linux Terminal

Once lolcat installed, the basic syntax for running lolcat is:

$ lolcat [options] [files] ...

It comes with several options that control its behavior, below are a few of the most considerable flags we will emphasis for the scope of this guide:

  1. -a – passes every input line through an animation effect.
  2. -d – specifies the duration (number of steps before displaying next line) of the animation effect, the default value is 12.
  3. -s – it specifies the speed (frame rate- number of steps per second) of the animation effect, default value is 20.
  4. -f – enables forcing of coloring in case standard output is not a tty.

You can find more options in the lolcat man page:

$ man lolcat 

How to Use Lolcat in Linux

To use lolcat, simply pipe the output of any relevant command to it and watch the magic.

For example:

$ ls -l | lolcat -as 25

colorful Linux Terminal Output

Besides you can alter the default speed, in the following command, we will use a relatively slow speed of 10steps per second:

$ ls -l | lolcat -as 10

You can use any command with lolcat display output in colorful fashion on Linux terminal, few commands say ps, date and cal as:

$ ps | lolcat
$ date | lolcat
$ cal | lolcat

In this article, we have looked at how to significantly reduce the speed of a command screen output and give it a rainbow coloring effect.

As usual, you can address to us any questions or comments concerning this article via the feedback section below. Lastly, you can mention to us any useful Linux commands you have discovered there.

Source

Newsroom – A Modern CLI to Get Your Favorite News in Linux

If you are a command-line addict like me, then you would always want to do everything such as controlling your Linux systems (local or remote), programming, searching Google using Googler, playing text-based games, reading your favorite news and much more from within a terminal window.

Okay, Linux newbies (or possibly any other Linux users out there) are probably asking, “how can i get latest news from the command-line?” In this article, we are going to show you how to do this using Newsroom (similar to Newsboat – a RSS/Atom Feed reader for Linux console).

Read AlsoCricket-CLI – Watch Live Cricket Scores in Linux Terminal

Newsroom is a simple, free open-source modern command-line tool to get your favorite news in Linux. It is developed using JavaScript (NodeJS to be specific), so it is cross-platform and runs on Linux systems, Mac OSX as well as Windows.

The default newsroom sources are: hackernews, techcrunch, inside, bnext, ithome, wanqu, nodeweekly, codetengu and gankio. You can configure your own sources via OPML (Outline Processor Markup Language) – an XML-based format designed for exchange of outline-structured information between applications running on different operating systems and environments.

Linux Terminal Newsreader

Requirements:

  1. NPM – Default NodeJS package manager; you can install NodeJS and NPM at once on your Linux system.

How to Install Newsroom in Linux Systems

Once you have NPM installed on your system, you install newsroom with root privileges using the sudo command, as follows (the -g switch means install globally: to be used by all users on the system):

$ sudo npm install -g newsroom-cli

Once you have successfully installed newsroom, the CLI will register the newsroom and nr commands in your shell. You can start using it as follows, it will take you to an interactive command line interface where you can choose your news source:

$ newsroom 

News Sources

News Sources

Use the Up and Down arrows to select a news source from a list of predefined sources, as shown below.

Select News Source

Select News Source

After choosing a news source, all news titles will be shown as in the following screen shot, then you can select an item by pressing the Space bar, after making a selection, the item will be indicated by a green colored bullet, as shown in the screen shot below. You can press Enter to read it in detail from a web browser.

Select News Topic

Select News Topic

To terminate the command-line, type [Ctrl+C].

You can also provide the source you want to get news from and the number of news items to be displayed as shown.

$ newsroom [news_source] [number_of_news_items]

For example:

$ newsroom hackernews 3

Last but not least, you can also use your own awesome OPML file, as follows. This way, you can add your own news sources such as tecmint.comfossmint.com, etc.

$ newsroom -o <your-awesome-list.opml>

To view the newsroom help message, use the command below.

$ newsroom --help

For more information check out Newsroom Github repository and learn how to create OPML file.

Newsroom is a great way to get your favorite news in Linux on the command-line. Try it out and share your thoughts about it, with us via the feedback form below.

Source

Rename All Files and Directory Names to Lowercase in Linux

This guide will show you how to rename all files and directories names to lowercase in Linux.

Read AlsoHow to Find Out Top Directories and Files (Disk Space) in Linux

There are several ways to achieve this, but we’ll explain two of the most efficient and reliable methods. For the purpose of this guide, we have used a directory named Files which has the following structure:

# find Files -depth

List Directory Structure

List Directory Structure

1. Using find, xargs and rename Commands Together

rename is a simple command line utility for renaming several files at once in Linux. You can use it together with find utility to rename all files or subdirectories in a particular directory to lowercase as follows:

$ find Files -depth | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;

Explanation of options used in the above command.

  • -depth – lists each directory’s contents before the directory itself.
  • -n 1 – instructs xargs to use at most one argument per command line from find output.

Sample output after renaming files and subdirectories to lowercase in Files directory.

Rename Files and Directory Names to Lowercase

Rename Files and Directory Names to Lowercase

Another alternative way using the find and mv commands in a script as explained below.

2. Using find and mv Commands in Shell Script

First create your script (you can name it anything you prefer):

$ cd ~/bin
$ vi rename-files.sh

Then add the code below in it.

#!/bin/bash
#print usage 
if [ -z $1 ];then
        echo "Usage :$(basename $0) parent-directory"
        exit 1
fi

#process all subdirectories and files in parent directory
all="$(find $1 -depth)"



for name in ${all}; do
        #set new name in lower case for files and directories
        new_name="$(dirname "${name}")/$(basename "${name}" | tr '[A-Z]' '[a-z]')"

        #check if new name already exists
        if [ "${name}" != "${new_name}" ]; then
                [ ! -e "${new_name}" ] && mv -T "${name}" "${new_name}"; echo "${name} was renamed to ${new_name}" || echo "${name} wasn't renamed!"
        fi
done

echo
echo
#list directories and file new names in lowercase
echo "Directories and files with new names in lowercase letters"
find $(echo $1 | tr 'A-Z' 'a-z') -depth

exit 0

Save and close the file, then make the script executable and run it:

$ chmod +x rename-files.sh
$ rename-files.sh Files     #Specify Directory Name

Lowercase File Names Using Script

Lowercase File Names Using Script

You may also like to read these following related articles.

  1. Explanation of “Everything is a File” and Types of Files in Linux
  2. fswatch – Monitors Files and Directory Changes or Modifications in Linux
  3. Fasd – A Commandline Tool That Offers Quick Access to Files and Directories
  4. FSlint – How to Find and Remove Duplicate Files in Linux

In this guide, we expalined you how to rename all files and directories to lowercase in Linux. If get any errors, please hit us up via the feedback form below. You can as well offer us any other methods of doing the same.

Source

Linux zcat Command Examples for Newbies

Normally, files compressed using gzip can be restored to their original form using gzip -d or gunzip commands. What if you want to view the contents of a compressed file without uncompressing it? For this purpose, you need the zcat command utility.

Read Also18 tar Command Examples in Linux

Zcat is a command line utility for viewing the contents of a compressed file without literally uncompressing it. It expands a compressed file to standard output allowing you to have a look at its contents. In addition, zcat is identical to running gunzip -c command. In this guide, we will explain zcat command examples for beginners.

1. The first example shows how to view contents of a normal file using cat command, compress it using gzip command and view the contents of the zipped file using zcat as shown.

$ cat users.list 
$ gzip users.list
$ zcat users.list.gz

View Compressed File Contents in Linux

View Compressed File Contents in Linux

2. To view multiple compressed files, use the following command with filenames as shown.

$ zcat users.list.gz apps.list.gz

View Multiple Compressed Files Content in Linux

View Multiple Compressed Files Content in Linux

3. To view contents of normal files use the -f flag, similar to cat command, for example.

$ zcat -f users.list

View Linux File Contents

View Linux File Contents

4. To enable pagination, you can use the more and less commands as shown (Also read: Why ‘less’ is Faster Than ‘more’ Command in Linux).

$ zcat users.list.gz | more
$ zcat users.list.gz | less

5. To get the properties (compressed size, uncompressed size, ratio – compression ratio (0.0% if unknown), uncompressed_name (name of the uncompressed file) of a compressed file, use the -l flag.

$ zcat -l users.list.gz  

View Compressed File Properties in Linux

View Compressed File Properties in Linux

6. To suppress all warnings, use the -q flag as shown.

$ zcat -q users.list.gz

For more information, see the zcat man page.

$ man zcat

You might also like to read these following related articles.

  1. ccat – Show ‘cat Command’ Output with Syntax Highlighting or Colorizing
  2. How to Use ‘cat’ and ‘tac’ Commands with Examples in Linux
  3. Manage Files Effectively using head, tail and cat Commands in Linux
  4. How to Backup or Clone Linux Partitions Using ‘cat’ Command

That’s all! In this short article, we’ve explained zcat command examples for beginners. Share your thoughts with us in the comments section below.

Source

How to Permanently Disable Swap in Linux

Swapping or swap space represents a physical memory page that lives on top of disk partition or a special disk file used for extending the RAM memory of a system when the physical memory fills up.

Using this method of extending RAM resources, inactive memory pages are frequently dumped into the swap area when no RAM is available. However, do to the spinning speed of classical hard disks, swap space is way lower in transfer speeds and access time compared to RAM.

On newer machines with fast SSD hard disks, reserving a small partition for swapping can greatly improve access time and speed transfer compared to classical HDD, but the speed is still more magnitudes lower than RAM memory. Some suggest that the swap space should be set as twice the amount of machine RAM. However, on systems with more than 4 GB or RAM, swap space should be set between 2 or 4 GB.

In case your server has sufficient RAM memory or does not require the use of swap space or the swapping greatly decreases your system performance, you should consider disabling the swap area.

Before actually disabling swap space, first you need to visualize your memory load degree and then identify the partition that holds the swap area, by issuing the below commands.

# free -h 

Look for Swap space used size. If the used size is 0B or close to 0 bytes, it can be assumed that swap space is not used intensively and can be safety disabled.

Check Swap Space

Check Swap Space

Next, issue following blkid command, look for TYPE=”swap” line in order to identify the swap partition, as shown in the below screenshot.

# blkid 

Check Swap Partition Type

Check Swap Partition Type

Again, issue the following lsblk command to search and identify the [SWAP] partition as shown in the below screenshot.

# lsblk

Search Confirm Swap Partition

Search Confirm Swap Partition

After you’ve identified the swap partition or file, execute the below command to deactivate the swap area.

# swapoff /dev/mapper/centos-swap  

Or disable all swaps from /proc/swaps

# swapoff -a 

Run free command in order to check if the swap area has been disabled.

# free -h

Disable Swap Partition

Disable Swap Partition

In order to permanently disable swap space in Linux, open /etc/fstab file, search for the swap line and comment the entire line by adding a # (hashtag) sign in front of the line, as shown in the below screenshot.

# vi /etc/fstab

Disable Swap Partition Permanently

Disable Swap Partition Permanently

Afterwards, reboot the system in order to apply the new swap setting or issuing mount -a command in some cases might do the trick.

# mount -a

After system reboot, issuing the commands presented in the beginning of this tutorial should reflect that the swap area has been completely and permanently disabled in your system.

# free -h
# blkid 
# lsblk

Source

8 Practical Examples of Linux “Touch” Command

In Linux every single file is associated with timestamps, and every file stores the information of last access time, last modification time and last change time. So, whenever we create new file, access or modify an existing file, the timestamps of that file automatically updated.

Linux Touch Command

Linux Touch Command Examples

In this article we will cover some useful practical examples of Linux touch command. The touch command is a standard program for Unix/Linux operating systems, that is used to create, change and modify timestamps of a file. Before heading up for touch command examples, please check out the following options.

Touch Command Options

  1. -a, change the access time only
  2. -c, if the file does not exist, do not create it
  3. -d, update the access and modification times
  4. -m, change the modification time only
  5. -r, use the access and modification times of file
  6. -t, creates a file using a specified time

1. How to Create an Empty File

The following touch command creates an empty (zero byte) new file called sheena.

# touch sheena

2. How to Create Multiple Files

By using touch command, you can also create more than one single file. For example the following command will create 3 files named, sheenameena and leena.

# touch sheena meena leena

3. How to Change File Access and Modification Time

To change or update the last access and modification times of a file called leena, use the -a option as follows. The following command sets the current time and date on a file. If the leena file does not exist, it will create the new empty file with the name.

# touch -a leena

The most popular Linux commands such as find command and ls command uses timestamps for listing and finding files.

4. How to Avoid Creating New File

Using -c option with touch command avoids creating new files. For example the following command will not create a file called leena if it does not exists.

# touch -c leena

5. How to Change File Modification Time

If you like to change the only modification time of a file called leena, then use the -m option with touch command. Please note it will only updates the last modification times (not the access times) of the file.

# touch -m leena

6. Explicitly Set the Access and Modification times

You can explicitly set the time using -c and -t option with touch command. The format would be as follows.

# touch -c -t YYDDHHMM leena

For example the following command sets the access and modification date and time to a file leena as 17:30(17:30 p.m.) December 10 of the current year (2012).

# touch -c -t 12101730 leena

Next verify the access and modification time of file leena, with ls -l command.

# ls -l

total 2
-rw-r--r--.  1 root    root   0 Dec 10 17:30 leena

7. How to Use the time stamp of another File

The following touch command with -r option, will update the time-stamp of file meena with the time-stamp of leena file. So, both the file holds the same time stamp.

# touch -r leena meena

8. Create a File using a specified time

If you would like to create a file with specified time other than the current time, then the format should be.

# touch -t YYMMDDHHMM.SS tecmint

For example the below command touch command with -t option will gives the tecmint file a time stamp of 18:30:55 p.m. on December 102012.

# touch -t 201212101830.55 tecmint

We’ve almost covered all the options available in the touch command for more options use “man touch“. If we’ve still missed any options and you would like to include in this list, please update us via comment box.

Source

WP2Social Auto Publish Powered By : XYZScripts.com