Gogo – Create Shortcuts to Long and Complicated Paths in Linux

Gogo is an impressive way to bookmark directories inside your shell. It allows you to create shortcuts to long and complicated paths in Linux. This way, you don’t have to type or remember long and complicated paths anymore in Linux.

For example, if you have a directory ~/Documents/Phone-Backup/Linux-Docs/Ubuntu/, using gogo, you can create an alias (a shortcut name), for instance Ubuntu to access it without typing the whole path anymore. No matter your current working directory, you can move into ~/cd Documents/Phone-Backup/Linux-Docs/Ubuntu/by simply using the alias Ubuntu.

Read Alsobd – Quickly Go Back to a Parent Directory Instead of Typing “cd ../../..” Redundantly

In addition, it also allows you to create aliases for connecting directly into directories on remote Linux servers.

How to Install Gogo in Linux Systems

To install Gogo, first clone the gogo repository from Github and then copy the gogo.py to any directory in your PATH environmental variable (if you already have the ~/bin/ directory, you can place it here, otherwise create it).

$ git clone https://github.com/mgoral/gogo.git
$ cd gogo/
$ mkdir -p ~/bin        #run this if you do not have ~/bin directory
$ cp gogo.py ~/bin/

Install Gogo in Linux

Install Gogo in Linux

Then add a function from gogo.sh to your ~/.bashrc (for Bash) or ~/.zshrc (for Zsh) file and verity it as shown.

$ cat gogo.sh >> ~/.bashrc
$ tail  ~/.bashrc
OR
$ cat gogo.sh >> ~/.zshrc 

Add Gogo Function to Bashrc

Add Gogo Function to Bashrc

How to Use Gogo in Linux Systems

To start using gogo, you need to logout and login back to use it. Gogo stores its configuration in ~/.config/gogo/gogo.conf file (which should be auto created if it doesn’t exist) and has the following syntax.

# Comments are lines that start from '#' character.
default = ~/something
alias = /desired/path
alias2 = /desired/path with space
alias3 = "/this/also/works"
zażółć = "unicode/is/also/supported/zażółć gęślą jaźń"

If you run gogo run without any arguments, it will go to the directory specified in default; this alias is always available, even if it’s not in the configuration file, and points to $HOME directory.

To display the current aliases, use the -l switch. From the following screenshot, you can see that default points to ~/home/tecmint which is user tecmint’s home directory on the system.

$ gogo -l   

List Gogo Aliases

List Gogo Aliases

Below is an example of running gogo without any arguments.

$ cd Documents/Phone-Backup/Linux-Docs/
$ gogo
$ pwd

Running Gogo Without Options

Running Gogo Without Options

To create a shortcut to a long path, move into the directory you want and use the -a flag to add an alias for that directory in gogo, as shown.

$ cd Documents/Phone-Backup/Linux-Docs/Ubuntu/
$ gogo -a Ubuntu
$ gogo
$ gogo -l
$ gogo -a Ubuntu
$ pwd

Create Long Directory Shortcut

Create Long Directory Shortcut

You can also create aliases for connecting directly into directories on a remote Linux servers. To do this, simple add the following lines to gogo configuration file, which can be accessed using -e flag, this will use the editor specified in the $EDITOR env variable.

$ gogo -e

One configuration file opens, add these following lines to it.

sshroot = ssh://root@192.168.56.5:/bin/bash  /root/
sshtdocs = ssh://tecmint@server3  ~/tecmint/docs/

Gogo Configuration File

Gogo Configuration File

To display the gogo help message, use the -h option.

$ gogo -h

One notable limitation of gogo is its lack of support for auto-completion – when accessing subdirectories/child directories under an aliased long path.

Gogo github repositoryhttps://github.com/mgoral/gogo

Gogo is a remarkable way that comes in handy, for creating shortcuts to long and complicated paths in Linux. Try it out and share your thoughts about it or ask any questions via the comment form below.

Source

Goto – Quickly Navigate to Aliased Directories with Auto-Completion Support

In a recent article, we talked about Gogo – a tool to create shortcuts for long paths in a Linux shell. Although gogo is a great way to bookmark your favorite directories inside a shell, however, it has one major limitation; it lacks an auto-completion feature.

Because of the above reason, we went all out to find a similar utility with auto-completion support – where the shell can prompt with suggestions of the available aliases (shortcuts to long and complicated paths) and luckily, after crawling through Github, we discovered Goto.

Goto is a shell utility to quickly navigate to aliased directories, with support for auto-completion. It comes with a nice auto-completion script so that once you press the tab key after the goto command or after typing a few charters of an existing alias, bash or zsh prompts with suggestions of the aliases or auto complete the name, respectively.

Goto also has additional options for unregistering an alias, expanding an alias’s value as well as cleaning up aliases of deleted directories. Note that goto’s auto-completion only works for aliases; it is separate from shell auto-completion for commands or filenames.

Goto - Navigate Quickly to Aliased Directories

How to Install and Use Goto in Linux Systems

To install Goto, start by cloning the goto repository from Github and move into the local repository directory, then run the install shell script with root user privileges using the sudo command as shown.

$ cd Downloads/
$ git clone https://github.com/iridakos/goto.git
$ cd goto
$ ls
$ sudo ./install

This will install goto in /usr/local/share/goto.sh, and it will add a line in your ~/.bashrc (for Bash) or ~/.zshrc (for Zsh) shell startup file, to source it.

Now restart your terminal to start using goto. To create an alias for a directory, register the alias with the -rflag as follows.

$ goto -r march ~/Documents/Tecmint.com-Articles/March/

Create Alias for Directory in Linux

Create Alias for Directory in Linux

To alias your current directory, use this syntax which will automatically be aliased to the whole path.

$ goto -r home . 

When you type goto and press the tab key, it will show all registered aliases and when you type a few letters of a registered alias, goto will auto complete the name. However, to view a list of your currently registered aliases, use the -l flag.

$ goto -l

Check Available Aliases in Linux

Check Available Aliases in Linux

To expand an alias to its value using the following command.

$ goto -x scripts
$ goto -x march

Expand an Alias Value

Expand an Alias Value

Goto also allows you to unregister an alias, using the -u option.

$ goto -l
$ goto -u march
$ goto -l

Remove Directory Alias in Linux

Remove Directory Alias in Linux

If you have removed aliased directories (for example if you have deleted the directories ~/Documents/Tecmint.com-Articles/March and ~/bin/shellscripts/recon from the filesystem), yet they still have aliases in goto, you can cleanup all these aliases from goto with the -c flag.

$ goto -c

A significant limitation of goto is that it doesn’t allow for accessing a sub-directory under an aliased directory, which is a feature present in Gogo.

For more information, consult the goto help message with the -h option.

$ goto -h

Goto Github repositoryhttps://github.com/iridakos/goto

Goto is a powerful way to bookmark your favorite directories inside a shell, with auto-completion support, in Linux. It has more useful features compared to Gogo, as explained above. Give it a try and share with us, your thoughts about it via the feedback form below.

Source

4 Useful Way to Know Plugged USB Device Name in Linux

As a newbie, one of the many things you should master in Linux is identification of devices attached to your system. It may be your computer’s hard disk, an external hard drive or removable media such USB drive or SD Memory card.

Using USB drives for file transfer is so common today, and for those (new Linux users) who prefer to use the command line, learning the different ways to identify a USB device name is very important, when you need to format it.

Suggested Read: 3 Ways to Delete All Files in a Directory

Once you attach a device to your system such as a USB, especially on a desktop, it is automatically mounted to a given directory, normally under /media/username/device-label and you can then access the files in it from that directory. However, this is not the case with a server where you have to manually mount a device and specify its mount point.

Linux identifies devices using special device files stored in /dev directory. Some of the files you will find in this directory include /dev/sda or /dev/hda which represents your first master drive, each partition will be represented by a number such as /dev/sda1 or /dev/hda1 for the first partition and so on.

$ ls /dev/sda* 

List All Linux Device Names

List All Linux Device Names

 

Now let’s find out device names using some different command-line tools as shown:

Find Out Plugged USB Device Name Using df Command

To view each device attached to your system as well as its mount point, you can use the df command (checks Linux disk space utilization) as shown in the image below:

$ df -h

Find USB Device Name Using df Command

Find USB Device Name Using df Command

Use lsblk Command to Find USB Device Name

You can also use the lsblk command (list block devices) which lists all block devices attached to your system like so:

$ lsblk

List Linux Block Devices

List Linux Block Devices

Identify USB Device Name with fdisk Utility

fdisk is a powerful utility which prints out the partition table on all your block devices, a USB drive inclusive, you can run it will root privileges as follows:

$ sudo fdisk -l

List Partition Table of Block Devices

List Partition Table of Block Devices

Determine USB Device Name with dmesg Command

dmesg is an important command that prints or controls the kernel ring buffer, a data structure which stores information about the kernel’s operations.

Run the command below to view kernel operation messages which will as well print information about your USB device:

$ dmesg

dmesg - Prints USB Device Name

dmesg – Prints USB Device Name

That is all for now, in this article, we have covered different approaches of how to find out a USB device name from the command line. You can also share with us any other methods for the same purpose or perhaps offer us your thoughts about the article via the response section below.

Source

15 Tips On How to Use ‘Curl’ Command in Linux

Back in the mid-1990’s when the Internet was still in its infancy, a Swedish programmer named Daniel Stenbergstarted a project that eventually grew into what we know as curl today.

Initially, he aimed at developing a bot that would download currency exchange rates from a web page periodically and would provide Swedish Kronor equivalents in US dollars to IRC users.

Long story short, the project thrived, adding several protocols and features along the way – and the rest is history. Now let’s dive in with both feet and learn how to use curl to transfer data and more in Linux!

We have put together the following list of 15 curl commands for you.

1. View curl Version

The -V or --version options will not only return the version, but also the supported protocols and features in your current version.

$ curl --version

curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets 

2. Download a File

If you want to download a file, you can use curl with the -O or -o options. The former will save the file in the current working directory with the same name as in the remote location, whereas the latter allows you to specify a different filename and/or location.

$ curl -O http://yourdomain.com/yourfile.tar.gz # Save as yourfile.tar.gz
$ curl -o newfile.tar.gz http://yourdomain.com/yourfile.tar.gz # Save as newfile.tar.gz

3. Resume an Interrupted Download

If a download was interrupted for some reason (for example, using Ctrl + c), you can resume it very easily. The use of -C – (dash C, space dash) tells curl to resume the download beginning where it left off.

$ curl -C - -O http://yourdomain.com/yourfile.tar.gz

Download File Using Curl Command

Download File Using Curl Command

4. Download Multiple Files

With the following command you will download info.html and about.html from http://yoursite.com and http://mysite.com, respectively, in one go.

$ curl -O http://yoursite.com/info.html -O http://mysite.com/about.html 

5. Download URLs From a File

If you combine curl with xargs, you can download files from a list of URLs in a file.

$ xargs -n 1 curl -O < listurls.txt

Download Multiple Files with Curl

Download Multiple Files with Curl

6. Use a Proxy with or without Authentication

If you are behind a proxy server listening on port 8080 at proxy.yourdomain.com, do.

$ curl -x proxy.yourdomain.com:8080 -U user:password -O http://yourdomain.com/yourfile.tar.gz

where you can skip -U user:password if your proxy does not require authentication.

7. Query HTTP Headers

HTTP headers allow the remote web server to send additional information about itself along with the actual request. This provides the client with details on how the request is being handled.

To query the HTTP headers from a website, do:

$ curl -I www.tecmint.com

Curl Query HTTP Headers

Curl Query HTTP Headers

This information is also available in your browser’s developer tools.

8. Make a POST request with Parameters

The following command will send the firstName and lastName parameters, along with their corresponding values, to https://yourdomain.com/info.php.

$ curl --data "firstName=John&lastName=Doe" https://yourdomain.com/info.php

You can use this tip to simulate the behavior of a regular HTML form.

9. Download Files from an FTP Server with or without Authentication

If a remote FTP server is expecting connections at ftp://yourftpserver, the following command will download yourfile.tar.gz in the current working directory.

$ curl -u username:password -O ftp://yourftpserver/yourfile.tar.gz 

where you can skip -u username:password if the FTP server allows anonymous logins.

10. Upload Files to an FTP server with or without Authentication

To upload a local file named mylocalfile.tar.gz to ftp://yourftpserver using curl, do:

$ curl -u username:password -T mylocalfile.tar.gz ftp://yourftpserver

11. Specify User Agent

The user agent is part of the information that is sent along with an HTTP request. This indicates which browser the client used to make the request. Let’s see what our current curl version uses as default, and let’s change it later to “I am a new web browser”:

$ curl -I http://localhost --user-agent "I am a new web browser"

Curl Check User Agent

Curl Check User Agent

12. Store Website Cookies

Want to see which cookies are downloaded to your computer when you browse to https://www.cnn.com? Use the following command to save them to cnncookies.txt. You can then use cat command to view the file.

$ curl --cookie-jar cnncookies.txt https://www.cnn.com/index.html -O

Curl Store Website Cookies

Curl Store Website Cookies

13. Send Website Cookies

You can use the cookies retrieved in the last tip in subsequent requests to the same site.

$ curl --cookie cnncookies.txt https://www.cnn.com

14. Modify Name Resolution

If you’re a web developer and want to test a local version of yourdomain.com before pushing it live, you can make curl resolve http://www.yourdomain.com to your localhost like so:

$ curl --resolve www.yourdomain.com:80:localhost http://www.yourdomain.com/

Thus, the query to http://www.yourdomain.com will tell curl to request the site from localhost instead of using DNS or the /etc/hosts file.

15. Limit Download Rate

To prevent curl from hosing your bandwidth, you can limit the download rate to 100 KB/s as follows.

$ curl --limit-rate 100K http://yourdomain.com/yourfile.tar.gz -O
Summary

In this article we have shared a brief history of the origins of curl and explained how to use it through 15 practical examples.

Do you know of any other curl commands that we may have missed in this article? Feel free to share them with our community in the comments! Also, if you have questions feel free to let us know. We look forward to hearing from you!

Source

Easily Correct a Typo of Previous Command Using Carat (^) Symbol

Have you ever typed a command and rushed to hit Enter, only to find that you had a typo in it? While you can use the up and down arrows to navigate the command history and edit the typo, there’s an easier and faster way.

Don’t Miss: The Power of Linux “History Command” in Bash Shell

In this tip, we shall cover a simple and handy method of dealing with a command line typo, let’s suppose you wanted to see if there’s a service listening on port 22, but accidentally typed nestat instead of netstat.

You can easily replace the typo with the correct command and execute it like so:

# nestat -npltu | grep 22
# ^nestat^netstat

That’s right. Using two carat signs (they should be followed by the typo and the right word, respectively) you can correct the typo and run the command automatically afterwards.

Correct Command Typos or Mistakes with Carat (^) Symbol

Correct Command Typos or Mistakes with Carat (^) Symbol

You must take note that this method only works for the previous command (most recent command executed), when you try to correct a typo for a command executed earlier on, the shell will print out an error.

Summary

This is a great tip that can help you eliminate time wasting tendencies, as you have seen, it is much easier and faster than scrolling through commands history to locate and correct a typo.

All you have to do is correct the typo using the carat signs, hit Enter button and the correct command is executed automatically.

Source

Agedu – A Useful Tool for Tracking Down Wasted Disk Space in Linux

Assume you are running short on disk space and you wanted to free up, by searching something that’s a waste of space and removing it or moving it to an archive medium. How do you track down right stuff to delete, that saves maximum space?

Linux provides a standard du command, which scans entire disk and shows you which directories hold the huge amount of data. That can assist you narrow your search to the things most useful deleting.

However, that only shows you what’s huge. What you actually want to know is what’s too huge. By default, du command will not let you differentiate between data that’s huge because you are doing something that needs it to be huge, and data that’s huge because you unpacked it once and ignored about it.

Most Linux file systems, by default only shows when a file was last accessed, but not shown when it was written, modified or even read. So if you created a huge amount of data years ago, forgot to delete it and have never used it since, then it is important to use those last-access time stamps to know the difference between used and unused data.

Agedu pronounced as (age dee you) is an open source and free utility (much like du command) that helps system administrators to track down wasted disk space used by old files and delete them to free up some space.

Agedu does a complete scan and produces reports that shows how much disk space is utilized by each directory and sub directory along with the last-access times of files. In simple words, it simply helps you to free up disk space.

Agedu Features

  1. Creates graphical reports.
  2. Produces data output in HTML format.
  3. Generates HTML reports with hyperlinks to other directories for easy navigation to gather reports.
  4. Provides more configurable options.

How Does Agedu Works?

From the man page:

agedu is a program which does this. It does basically the same sort of disk scan as du, but it also records the last-access times of everything it scans. Then it builds an index that lets it efficiently generate reports giving a summary of the results for each subdirectory, and then it produces those reports on demand.

How to Install Agedu in Linux Systems

On Debian/Ubuntuagedu is available to install from the default system repositories using following apt-get command as shown.

$ sudo apt-get install agedu

On RHEL/CentOS, you need to turn on EPEL repository to install Agedu using following yum command as shown.

# yum install epel-release
# yum install agedu

Fedora and Arch Linux users, simply type the following command to install Agedu.

$ sudo dnf install agedu  [On Fedora]
$ sudo yaourt -S agedu    [On Arch Linux]

On other Linux distributions, you can compile Agedu from source as shown.

$ wget https://www.chiark.greenend.org.uk/~sgtatham/agedu/agedu-20180329.af641e6.tar.gz
$ tar -xvf agedu-20180329.af641e6.tar.gz
$ cd agedu-20180329.af641e6
$ ./configure
$ make
$ sudo make install

How to Track Wasted Disk Space Using Agedu

The following command will do a complete scan of /home/tecmint directory and its sub-directories and creates a special index file containing its data structure.

# agedu -s /home/tecmint/
Scan Linux System for Disk Space
Built pathname index, 232578 entries, 22842517 bytes of index                                                                                                                
Faking directory atimes
Building index
Final index file size = 97485984 bytes

Next, type the following command to query newly created index file.

# agedu -w
Sample Output :
Using Linux /proc/net magic authentication
URL: http://localhost:34895/

Now, type the following command to open the URL using any web browser.

# http://localhost:34895/
Graphical Output :

The below screen shows the graphical representation of disk usage of /home/tecmint along with its sub directories using various colors to show the difference between disused and recently accessed data.

Click on any sub directory to see the reports of its sub-directories. To terminate this mode, just press [CTRL+D] on command line.

Linux Disk Space Usage

Linux Disk Space Usage

To create and set custom port number for agedu, type the following command.

# agedu -w --address 127.0.0.1:8081
Sample Output :
Using Linux /proc/net magic authentication
URL: http://127.0.0.1:8081/

Enable password protection for Agedu using the following command.

# agedu -w --address 127.0.0.1:8081 --auth basic
Agedu Password Protect Reports
Username: agedu
Password: n2tx16jejnbzmuur
URL: http://127.0.0.1:8081/

Password Protect Agedu

Password Protect Agedu

Linux Directory Disk Space Usage

Linux Directory Disk Space Usage

Access Agedu reports using terminal mode.

# agedu -t /home/tecmint
Show Disk Space Usage in Terminal
8612        /home/tecmint/.AndroidStudio3.1
3684        /home/tecmint/.PlayOnLinux
604         /home/tecmint/.ScreamingFrogSEOSpider
2416        /home/tecmint/.TelegramDesktop
61960       /home/tecmint/.Write
1508        /home/tecmint/.adobe
20          /home/tecmint/.aptitude
48          /home/tecmint/.byobu
1215948     /home/tecmint/.cache
3096        /home/tecmint/.cinnamon
1421828     /home/tecmint/.config
12          /home/tecmint/.dbus
8           /home/tecmint/.emacs.d
780         /home/tecmint/.fonts
...

You see the output similar to du command. Let’s see the old files which are not accessed for long time. For example, to see only old files that are not accessed in last 12 months or more.

# agedu -t /home/tecmint -a 12m
Show Files Not Accessed in Last 12 Months
2416        /home/tecmint/.TelegramDesktop
1500        /home/tecmint/.adobe
46776       /home/tecmint/.cache
1840        /home/tecmint/.cinnamon
142796      /home/tecmint/.config
636         /home/tecmint/.gconf
88          /home/tecmint/.gimp-2.8
12          /home/tecmint/.gnome
112         /home/tecmint/.java
108         /home/tecmint/.kde
8           /home/tecmint/.links2
16          /home/tecmint/.linuxmint
6804        /home/tecmint/.local
12          /home/tecmint/.mindterm
40920       /home/tecmint/.mozilla
4           /home/tecmint/.oracle_jre_usage
12          /home/tecmint/.parallel
24          /home/tecmint/.shutter
6840        /home/tecmint/.softmaker
336         /home/tecmint/.themes
....

Let’s find out how much disk space taken by MP3 files by using following command.

# agedu -s . --exclude '*' --include '*.mp3'

Again to see reports run the following command.

# agedu -w

To deleted files and free up disk space, use the following command.

# rm -rf /downloads/*.mp3

How to remove agedu index file? First see the size of the index file with the following command.

# ls agedu.dat -lh
Sample output :
-rw------- 1 tecmint tecmint 35M Apr 10 12:05 agedu.dat

To remove index file, just enter.

# agedu -R

For more information on agedu command options and usage, please read the man pages or visit agedu home page.

# man agedu

If you know any tool that we haven’t mentioned in this site. Please let us know about it via comment box below.

Source

How to Set and Unset Local, User and System Wide Environment Variables in Linux

Environment Variables are some special variables that are defined in shell and are needed by programs while execution. They can be system defined or user defined. System defined variables are those which are set by system and are used by system level programs.

Set and Unset Linux Environment Variables

Set and Unset Linux Environment Variables

For e.g. PWD command is a very common system variable which is used to store the present working directory. User defined variables are typically set by user, either temporarily for the current shell or permanently. The whole concept of setting and un-setting environment variables revolves around some set of files and few commands and different shells.

In Broader terms, an environment variable can be in three types:

1. Local Environment Variable

One defined for the current session. These environment variables last only till the current session, be it remote login session, or local terminal session. These variables are not specified in any configuration files and are created, and removed by using a special set of commands.

2. User Environment Variable

These are the variables which are defined for a particular user and are loaded every time a user logs in using a local terminal session or that user is logged in using remote login session. These variables are typically set in and loaded from following configuration files: .bashrc.bash_profile.bash_login.profile files which are present in user’s home directory.

3. System wide Environment Variables

These are the environment variables which are available system-wide, i.e. for all the users present on that system. These variables are present in system-wide configuration files present in following directories and files: /etc/environment/etc/profile/etc/profile.d//etc/bash.bashrc. These variables are loaded every time system is powered on and logged in either locally or remotely by any user.

Understanding User-Wide and System-wide Configuration files

Here, we briefly describe various configuration files listed above that hold Environment Variables, either system wide or user specific.

.bashrc

This file is user specific file that gets loaded each time user creates a new local session i.e. in simple words, opens a new terminal. All environment variables created in this file would take effect every time a new local session is started.

.bash_profile

This file is user specific remote login file. Environment variables listed in this file are invoked every time the user is logged in remotely i.e. using ssh session. If this file is not present, system looks for either .bash_login or .profile files.

/etc/environment

This file is system wide file for creating, editing or removing any environment variables. Environment variables created in this file are accessible all throughout the system, by each and every user, both locally and remotely.

/etc/bash.bashrc

System wide bashrc file. This file is loaded once for every user, each time that user opens a local terminal session. Environment variables created in this file are accessible for all users but only through local terminal session. When any user on that machine is accessed remotely via a remote login session, these variables would not be visible.

/etc/profile

System wide profile file. All the variables created in this file are accessible by every user on the system, but only if that user’s session is invoked remotely, i.e. via remote login. Any variable in this file will not be accessible for local login session i.e. when user opens a new terminal on his local system.

Note: Environment variables created using system-wide or user-wide configuration files can be removed by removing them from these files only. Just that after each change in these files, either log out and log in again or just type following command on the terminal for changes to take effect:

$ source <file-name>

Set or Unset Local or Session-wide Environment Variables in Linux

Local Environment Variables can be created using following commands:

$ var=value 
OR
$ export var=value

These variables are session wide and are valid only for current terminal session. To Clear these session-wide environment variables following commands can be used:

1. Using env

By default, "env" command lists all the current environment variables. But, if used with '-i' switch, it temporarily clears out all the environment variables and lets user execute a command in current session in absence of all the environment variables.

$ env –i [Var=Value]… command args…

Here, var=value corresponds to any local environment variable that you want to use with this command only.

$ env –i bash

Will give bash shell which temporarily would not have any of the environment variable. But, as you exit from the shell, all the variables would be restored.

2. Using unset

Another way to clear local environment variable is by using unset command. To unset any local environment variable temporarily,

$ unset <var-name>

Where, var-name is the name of local variable which you want to un-set or clear.

3. Set the variable name to ”

Another less common way would be to set the name of the variable which you want to clear, to '' (Empty). This would clear the value of the local variable for current session for which it is active.

NOTE – YOU CAN EVEN PLAY WITH AND CHANGE THE VALUES OF SYSTEM OR USER ENVIRONMENT VARIABLES, BUT CHANGES WOULD REFLECT IN CURRENT TERMINAL SESSION ONLY AND WOULD NOT BE PERMANENT.

Learn How to Create, User-Wide and System-Wide Environment Variables in Linux

In section, we will going to learn how to set or unset local, user and system wide environment variables in Linux with below examples:

1. Set and Unset Local Variables in Linux

a.) Here, we create a local variable VAR1 and set it to any value. Then, we use unset to remove that local variable, and at the end that variable is removed.

$ VAR1='TecMint is best Site for Linux Articles'
$ echo $VAR1
$ unset VAR1
$ echo $VAR1

Set Unset Local Environment Variables

Set Unset Local Environment Variables

b.) Another way of creating a local variable is by using export command. The local variable created will be available for current session. To unset the variable simply set the value of variable to ''.

$ export VAR='TecMint is best Site for Linux Articles'
$ echo $VAR
$ VAR=
$ echo $VAR

Export Local Environment Variables

Export Local Environment Variables in Linux

c.) Here, we created a local variable VAR2 and set it to a value. Then in-order to run a command temporarily clearing out all local and other environment variables, we executed 'env –i' command. This command here executed bash shell by clearing out all other environment variables. After entering 'exit' on the invoked bash shell, all variables would be restored.

$ VAR2='TecMint is best Site for Linux Articles'
$ echo $VAR2
$ env -i bash
$ echo $VAR2   

Use Env Command to Unset Variables

Use Env Command to Unset Variables

2. Set and Unset User-Wide Environment Variables in Linux

a.) Modify .bashrc file in your home directory to export or set the environment variable you need to add. After that source the file, to make the changes take effect. Then you would see the variable ('CD' in my case), taking effect. This variable will be available every time you open a new terminal for this user, but not for remote login sessions.

$ vi .bashrc

Add the following line to .bashrc file at the bottom.

export CD='This is TecMint Home'

Now run the following command to take new changes and test it.

$ source .bashrc 
$ echo $CD

Set User-Wide Environment Variables in Linux

Set User-Wide Environment Variables in Linux

To remove this variable, just remove the following line in .bashrc file and re-source it:

b.) To add a variable which will be available for remote login sessions (i.e. when you ssh to the user from remote system), modify .bash_profile file.

$ vi .bash_profile

Add the following line to .bash_profile file at the bottom.

export VAR2='This is TecMint Home'

When on sourcing this file, the variable will be available when you ssh to this user, but not on opening any new local terminal.

$ source .bash_profile 
$ echo $VAR2

Here, VAR2 is not initially available but, on doing ssh to user on localhost, the variable becomes available.

$ ssh tecmint@localhost
$ echo $VAR2

Export User Wide Variables in Bash Profile

Export User Wide Variables in Bash Profile

To remove this variable, just remove the line in .bash_profile file which you added, and re-source the file.

NOTE: These variables will be available every time you are logged in to current user but not for other users.

3. Set and Unset System-Wide Environment Variables in Linux

a.) To add system wide no-login variable (i.e. one which is available for all users when any of them opens new terminal but not when any user of machine is remotely accessed) add the variable to /etc/bash.bashrc file.

export VAR='This is system-wide variable'

Add System Wide Environment Variables

Add System Wide Environment Variables

After that, source the file.

$ source /etc/bash.bashrc 

Now this variable will be available for every user when he opens any new terminal.

$ echo $VAR
$ sudo su
$ echo $VAR
$ su -
$ echo $VAR

Check System Wide Variables

Check System Wide Variables

Here, same variable is available for root user as well as normal user. You can verify this by logging in to other user.

b.) If you want any environment variable to be available when any of the user on your machine is remotely logged in, but not on opening any new terminal on local machine, then you need to edit the file – '/etc/profile'.

export VAR1='This is system-wide variable for only remote sessions'

Add System Wide Variables in Profile

Add System Wide Variables in Profile

After adding the variable, just re-source the file. Then the variable would be available.

$ source /etc/profile
$ echo $VAR1

To remove this variable, remove the line from /etc/profile file and re-source it.

c.) However, if you want to add any environment which you want to be available all throughout the system, on both remote login sessions as well as local sessions( i.e. opening a new terminal window) for all users, just export the variable in /etc/environment file.

export VAR12='I am available everywhere'

Add System Variable in Environment File

Add System Variable in Environment File

After that just source the file and the changes would take effect.

$ source /etc/environment
$ echo $VAR12
$ sudo su
$ echo $VAR12
$ exit
$ ssh localhost
$ echo $VAR12

Check Environment Variable for All Users

Check Environment Variable for All Users

Here, as we see the environment variable is available for normal user, root user, as well as on remote login session (here, to localhost).

To clear out this variable, just remove the entry in the /etc/environment file and re-source it or login again.

NOTE: Changes take effect when you source the file. But, if not then you might need to log out and log in again.

Conclusion

Thus, these are few ways we can modify the environment variables. If you find any new and interesting tricks for the same do mention in your comments.

Source

4 Ways to Find Out What Ports Are Listening in Linux

The state of a port is either openfilteredclosed, or unfiltered. A port is said to be open if an application on the target machine is listening for connections/packets on that port.

In this article, we will explain four ways to check open ports and also will show you how to find which application is listening on what port in Linux.

1. Using Netstat Command

Netstat is a widely used tool for querying information about the Linux networking subsystem. You can use it to print all open ports like this:

$ sudo netstat -ltup 

The flag -l tells netstat to print all listening sockets, -t shows all TCP connections, -u displays all UDP connections and -p enables printing of application/program name listening on the port.

Check Open Ports Using Netstat Command

Check Open Ports Using Netstat Command

 

To print numeric values rather than service names, add the -n flag.

$ sudo netstat -lntup

Show Numeric Values

Show Numeric Values

You can also use grep command to find out which application is listening on a particular port, for example.

$ sudo netstat -lntup | grep "nginx"

Find Port of Running Application

Find Port of Running Application

Alternatively, you can specify the port and find the application bound to, as shown.

$ sudo netstat -lntup | grep ":80"

Find Application Using a Port Number

Find Application Using a Port Number

2. Using ss Command

ss is another useful tool for displaying information about sockets. It’s output looks similar to that of netstat. The following command will show all listening ports for TCP and UDP connections in numeric value.

$ sudo ss -lntu

Find Open Ports Using ss Command

Find Open Ports Using ss Command

3. Using Nmap Command

Nmap is a powerful and popular network exploration tool and port scanner. To install nmap on your system, use your default package manager as shown.

$ sudo apt install nmap  [On Debian/Ubuntu]
$ sudo yum install nmap  [On CentOS/RHEL]
$ sudo dnf install nmap  [On Fedora 22+]

To scan all open/listening ports in your Linux system, run the following command (which should take a long time to complete).

$ sudo nmap -n -PN -sT -sU -p- localhost

4. Using lsof Command

The final tool we will cover for querying open ports is lsof command, which is used to list open files in Linux. Since everything is a file in Unix/Linux, an open file may be a stream or a network file.

To list all Internet and network files, use the -i option. Note that this command shows a mix of service names and numeric ports.

$ sudo lsof -i

List Open Network Files Using lsof Command

List Open Network Files Using lsof Command

To find which application is listening on a particular port, run lsof in this form.

$ sudo lsof -i :80

Find Application Using Port

Find Application Using Port

That’s all! In this article, we have explained four ways to check open ports in Linux. We also showed how to check which processes are bound upon particular ports. You can share your thoughts or ask any questions via the feedback form below.

Source

How to Find a Process Name Using PID Number in Linux

In this article, we will look at how to find a process name by its process identification number (PID). Before we dive into the actual solution, let us briefly talk about how processes are created and identified by Linux.

Every time a user or the system (Linux) launches a program, the kernel will create a process. A process holds execution details of the program in memory such as its input and output data, variables and so on.

Importantly, since Linux is a multitasking operating system, it executes several programs simultaneously, and this means each process process must be identified specifically.

The kernel identifies each process using a process ID (PID), a every instance of process must have a unique PIDfrom other processes which is assigned when the process is invoked, to avoid any execution errors.

The /proc file system stores information about currently running processes on your system, it contains directories for each process.
Use the ls command to list its contents, however, the list may be long, so employ a pipeline and the less utility to view the /proc contents in a more convenient way as below:

$ ls /proc 
OR
$ ls /proc | less 
List /proc File System
1     168   2230  25    329   584   7386  83         driver        schedstat
10    169   2234  2503  33    603   74    830        execdomains   scsi
1070  17    2247  2507  34    610   7411  833        fb            self
1081  1702  2256  2523  349   611   7423  836        filesystems   slabinfo
109   1714  2258  253   35    612   745   839        fs            softirqs
11    173   2266  2551  36    613   746   84         interrupts    stat
110   1760  2273  26    362   62    75    844        iomem         swaps
1188  1763  2278  2688  3642  63    7533  85         ioports       sys
12    1769  2282  2694  3643  64    7589  86         irq           sysrq-trigger
1204  177   2283  2695  37    6436  76    860        kallsyms      sysvipc
1209  1773  2285  2698  38    65    7619  87         kcore         thread-self
1254  18    2287  2699  39    66    7689  9          keys          timer_list
13    1847  2295  27    3974  67    7690  94         key-users     timer_stats
15    1914  23    2702  3976  68    77    977        kmsg          tty
152   1917  2308  28    4273  6897  7725  981        kpagecgroup   uptime
153   1918  2309  280   4374  69    7729  987        kpagecount    version
154   1938  2310  2815  4392  6969  7733  997        kpageflags    version_signature
155   1956  2311  2817  44    6980  78    acpi       loadavg       vmallocinfo
156   1981  2315  282   45    7     79    asound     locks         vmstat
1565  1986  2316  283   4543  70    790   buddyinfo  mdstat        zoneinfo
1567  1988  2317  29    46    71    8     bus        meminfo
157   2     2324  2935  461   7102  80    cgroups    misc
1579  20    2347  2944  4686  72    808   cmdline    modules
158   2010  2354  3     47    73    81    consoles   mounts
1584  2043  2436  30    4700  7304  810   cpuinfo    mtrr
159   2044  2437  3016  5     7311  815   crypto     net
1590  21    2442  31    515   7322  82    devices    pagetypeinfo
16    2167  2443  318   5273  7347  820   diskstats  partitions
160   22    2492  32    5274  7367  823   dma        sched_debug

From the screenshot above, the numbered directories store information files about the processes in execution, where each number corresponds to a PID.

Below is the list of files for systemd process with PID 1:

$ ls /proc/1
Show SystemD Process PID
ls: cannot read symbolic link '/proc/1/cwd': Permission denied
ls: cannot read symbolic link '/proc/1/root': Permission denied
ls: cannot read symbolic link '/proc/1/exe': Permission denied
attr        coredump_filter  gid_map    mountinfo   oom_score      schedstat  status
autogroup   cpuset           io         mounts      oom_score_adj  sessionid  syscall
auxv        cwd              limits     mountstats  pagemap        setgroups  task
cgroup      environ          loginuid   net         personality    smaps      timers
clear_refs  exe              map_files  ns          projid_map     stack      uid_map
cmdline     fd               maps       numa_maps   root           stat       wchan
comm        fdinfo           mem        oom_adj     sched          statm

You can monitor processes and their PIDs using traditional Linux commands such as pstop and relatively new glances command plus many more as in the examples below:

$ ps aux
Show Running Processes with PID
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0 185728  6268 ?        Ss   10:15   0:01 /sbin/init splash
root         2  0.0  0.0      0     0 ?        S    10:15   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    10:15   0:00 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   10:15   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    10:15   0:09 [rcu_sched]
root         8  0.0  0.0      0     0 ?        S    10:15   0:00 [rcu_bh]
root         9  0.0  0.0      0     0 ?        S    10:15   0:00 [migration/0]
root        10  0.0  0.0      0     0 ?        S    10:15   0:00 [watchdog/0]
root        11  0.0  0.0      0     0 ?        S    10:15   0:00 [watchdog/1]
root        12  0.0  0.0      0     0 ?        S    10:15   0:00 [migration/1]
root        13  0.0  0.0      0     0 ?        S    10:15   0:00 [ksoftirqd/1]
root        15  0.0  0.0      0     0 ?        S<   10:15   0:00 [kworker/1:0H]
root        16  0.0  0.0      0     0 ?        S    10:15   0:00 [watchdog/2]
root        17  0.0  0.0      0     0 ?        S    10:15   0:00 [migration/2]
root        18  0.0  0.0      0     0 ?        S    10:15   0:00 [ksoftirqd/2]
root        20  0.0  0.0      0     0 ?        S<   10:15   0:00 [kworker/2:0H]
root        21  0.0  0.0      0     0 ?        S    10:15   0:00 [watchdog/3]
root        22  0.0  0.0      0     0 ?        S    10:15   0:00 [migration/3]
root        23  0.0  0.0      0     0 ?        S    10:15   0:00 [ksoftirqd/3]
root        25  0.0  0.0      0     0 ?        S<   10:15   0:00 [kworker/3:0H]
root        26  0.0  0.0      0     0 ?        S    10:15   0:00 [kdevtmpfs]
root        27  0.0  0.0      0     0 ?        S<   10:15   0:00 [netns]
root        28  0.0  0.0      0     0 ?        S<   10:15   0:00 [perf]
....

Monitor Linux processes using traditional top command.

$ top

Monitor Linux Processes with top Command

Monitor Linux Processes with top Command

Monitor Linux processes using glances, a new real-time process monitoring tool for Linux.

$ glances

Glances - Real Time Linux Processes Monitoring

Glances – Real Time Linux Processes Monitoring

Learn more about how to install Glances in Linux systems.

Find Out Process PID Number

To find out the PID of a process, you can use pidof, a simple command to print out the PID of a process:

$ pidof firefox
$ pidof python
$ pidof cinnamon

Find Linux Process PID

Find Linux Process PID

Coming back to our point of focus, assuming you already know the PID of a process, you can print its name using the command form below:

$ ps -p PID -o format 

Where:

  1. -p specifies the PID
  2. -o format enables a user-defined format

Find Out Process Name Using PID Number

In this section, we will see how to find out a process name using its PID number with the help of user defined format i.e comm= which means command name, same as the process name.

$ ps -p 2523 -o comm=
$ ps -p 2295 -o comm=

Find Linux Process Name

Find Linux Process Name

For additional usage information and options, look through the ps man page.

$ man ps

If you want to kill a process using its PID number, I suggest you to read Find and Kill Linux Processes Using its PID.

Thats it for the moment, if you know any other better way to find out a process name using PID, do share with us via our comment section below.

Source

2 Useful Tools to Find and Delete Duplicate Files in Linux

Organizing your home directory or even system can be particularly hard if you have the habit of downloading all kinds of stuff from the internet.

Often you may find you have downloaded the same mp3, pdf, epub (and all kind of other file extensions) and copied it to different directories. This may cause your directories to become cluttered with all kinds of useless duplicated stuff.

In this tutorial you are going to learn how to find and delete duplicate files in Linux using rdfind and fdupescommand-line tools.

A note of caution – always be careful what you delete on your system as this may lead to unwanted data loss. If you are using a new tool, first try it in a test directory where deleting files will not be a problem.

Rdfind – Finds Duplicate Files in Linux

Rdfind comes from redundant data find. It is a free tool used to find duplicate files across or within multiple directories. It uses checksum and find duplicates based on file contains not only names.

Rdfind uses algorithm to classify the files and detects which of the duplicates is the original file and considers the rest as duplicates. The rules of ranking are:

  • If A was found while scanning an input argument earlier than BA is higher ranked.
  • If A was found at a depth lower than BA is higher ranked.
  • If A was found earlier than BA is higher ranked.

The last rule is used particularly when two files are found in the same directory.

To install rdfind in Linux, use the following command as per your Linux distribution.

$ sudo apt-get install rdfind     [On Debian/Ubuntu]
$ sudo yum install epel-release && $ sudo yum install rdfind    [On CentOS/RHEL]
$ sudo dnf install rdfind         [On Fedora 22+]

To run rdfind on a directory simply type rdfind and the target directory. Here is an example:

$ rdfind /home/user

Find Duplicate Files in Linux

Find Duplicate Files in Linux

As you can see rdfind will save the results in file called results.txt located in the same directory from where you ran the program. The file contains all the duplicate files that rdfind has found. You can review the file and remove the duplicate files manually if you want to.

Another thing you can do is to use the -dryrun option that will provide a list of duplicates without taking any actions:

$ rdfind -dryrun true /home/user

When you find the duplicates, you can choose to replace them with hardlinks.

$ rdfind -makehardlinks true /home/user

And if you wish to delete the duplicates you can run.

$ rdfind -deleteduplicates true /home/user

To check other useful options of rdfind you can use the rdfind manual with.

$ man rdfind 

Fdupes – Scan for Duplicate Files in Linux

Fdupes is another program that allows you to identify duplicate files on your system. It is free and open source and written in C. It uses following methods to determine duplicate files:

  • Comparing partial md5sum signatures
  • Comparing full md5sum signatures
  • Byte-by-byte comparison verification

Just like rdfind it has similar options:

  • Search recursively
  • Exclude empty files
  • Shows size of duplicate files
  • Delete duplicates immediately
  • Exclude files with different owner

Fdupes syntax is similar to rdfind. Simply type the command followed by the directory you wish to scan.

$ fdupes <dir>

To search files recursively, you will have to specify the -r option like this.

$ fdupes -r <dir>

You can also specify multiple directories and specify a dir to be searched recursively.

$ fdupes <dir1> -r <dir2>

To have fdupes calculate the size of the duplicate files use the -S option.

$ fdupes -S <dir>

To gather summarized information about the found files use the -m option.

$ fdupes -m <dir>

Scan Duplicate Files in Linux

Scan Duplicate Files in Linux

Finally if you want to delete all duplicates use the -d option like this.

$ fdupes -d <dir>

Fdupes will ask which of the found files to delete. You will need to enter the file number:

Delete Duplicate Files in Linux

Delete Duplicate Files in Linux

A solution that is definitely not recommended is to use the -N option which will result in preserving the first file only.

$ fdupes -dN <dir>

To get a list of available options to use with fdupes review the help page by running.

$ fdupes -help
Conclusion

Rdfind and fdupes are both very useful tools to find duplicated files on your Linux system, but you should be very careful when deleting such files.

If you are unsure if you need a file or not, it would be better to create a backup of that file and remember its directory prior deleting it. If you have any questions or comments, please submit them in the comment section below.

Source

WP2Social Auto Publish Powered By : XYZScripts.com