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

A Guide to Kill, Pkill and Killall Commands to Terminate a Process in Linux

Linux Operating System comes with Kill command to terminate a process. The command makes it possible to continue running the server without the need of reboot after a major change/update. Here comes the great power of Linux and this is one of the reasons, why Linux is running on 90% of servers, on the planet.

Linux Kill Command

Kill, Pkill and Killall Commands Examples

Kill command send a signal, a specified signal to be more perfect to a process. The kill command can be executed in a number of ways, directly or from a shell script.

Using kill command from /usr/bin provide you some extra feature to kill a process by process name using pkill. The common syntax for kill command is:

# kill [signal or option] PID(s)

For a kill command a Signal Name could be:

Signal Name		Signal Value			Behaviour

SIGHUP			      1				Hangup
SIGKILL			      9				Kill Signal
SIGTERM			      15			Terminate

Clearly from the behaviour above SIGTERM is the default and safest way to kill a process. SIGHUP is less secure way of killing a process as SIGTERMSIGKILL is the most unsafe way among the above three, to kill a process which terminates a process without saving.

In order to kill a process, we need to know the Process ID of a process. A Process is an instance of a program. Every-time a program starts, automatically an unique PID is generated for that process. Every Process in Linux, have a pid. The first process that starts when Linux System is booted is – init process, hence it is assigned a value of ‘1‘ in most of the cases.

Init is the master process and can not be killed this way, which insures that the master process don’t gets killed accidentally. Init decides and allows itself to be killed, where kill is merely a request for a shutdown.

To know all the processes and correspondingly their assigned pid, run.

# ps -A
Sample Output
PID TTY          TIME CMD
    1 ?        00:00:01 init
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 migration/0
    4 ?        00:00:00 ksoftirqd/0
    5 ?        00:00:00 migration/0
    6 ?        00:00:00 watchdog/0
    7 ?        00:00:01 events/0
    8 ?        00:00:00 cgroup
    9 ?        00:00:00 khelper
   10 ?        00:00:00 netns
   11 ?        00:00:00 async/mgr
   12 ?        00:00:00 pm
   13 ?        00:00:00 sync_supers
   14 ?        00:00:00 bdi-default
   15 ?        00:00:00 kintegrityd/0
   16 ?        00:00:00 kblockd/0
   17 ?        00:00:00 kacpid
   18 ?        00:00:00 kacpi_notify
   19 ?        00:00:00 kacpi_hotplug
   20 ?        00:00:00 ata/0
   21 ?        00:00:00 ata_aux
   22 ?        00:00:00 ksuspend_usbd

How about Customising the above output using syntax as ‘pidof process‘.

# pidof mysqld
Sample Output
1684

Another way to achieve the above goal is to follow the below syntax.

# ps aux | grep mysqld
Sample Output
root      1582  0.0  0.0   5116  1408 ?        S    09:49   0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql     1684  0.1  0.5 136884 21844 ?        Sl   09:49   1:09 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root     20844  0.0  0.0   4356   740 pts/0    S+   21:39   0:00 grep mysqld

Before we step ahead and execute a kill command, some important points to be noted:

  1. A user can kill all his process.
  2. A user can not kill another user’s process.
  3. A user can not kill processes System is using.
  4. A root user can kill System-level-process and the process of any user.

Another way to perform the same function is to execute ‘pgrep‘ command.

# pgrep mysq
Sample Output
3139

To kill the above process PID, use the kill command as shown.

kill -9 3139

The above command will kill the process having pid=3139, where PID is a Numerical Value of process.

Another way to perform the same function, can be rewritten as.

# kill -SIGTERM 3139

Similarly ‘kill -9 PID‘ is similar to ‘kill -SIGKILL PID‘ and vice-versa.

How about killing a process using process name

You must be aware of process name, before killing and entering a wrong process name may screw you.

# pkill mysqld

Kill more than one process at a time.

# kill PID1 PID2 PID3

or

# kill -9 PID1 PID2 PID3

or

# kill -SIGKILL PID1 PID2 PID3

What if a process have too many instances and a number of child processes, we have a command ‘killall‘. This is the only command of this family, which takes process name as argument in-place of process number.

Syntax:
# killall [signal or option] Process Name

To kill all mysql instances along with child processes, use the command as follow.

# killall mysqld

You can always verify the status of the process if it is running or not, using any of the below command.

# service mysql status
# pgrep mysql
# ps -aux | grep mysql

That’s all for now, from my side.

Source

TLDR – Easy to Understand Man Pages for Every Linux User

One of the most commonly used and reliable ways of getting help under Unix-like systems is via man pages. Man pages are the standard documentation for every Unix-like system and they correspond to online manuals for programs, functions, libraries, system calls, formal standards and conventions, file formats and so on. However, man pages suffer from many failings one of which is they are too long and some people just don’t like to read too much text on the screen.

The TLDR (stands for “Too Long; Didn’t Read“. ) pages are summarized practical usage examples of commands on different operating systems including Linux. They simplify man pages by offering practical examples.

Read Also5 Useful Tools to Remember Linux Commands Forever

TLDR is an Internet slang, meaning a post, article, comment or anything such as a manual page was too long, and whoever used the phrase didn’t read it for that reason. The content of TLDR pages is openly available under the permissive MIT License.

In this short article, we will show how to install and use TLDR pages in Linux.

Requirements

  1. Install Latest Nodejs and NPM Version in Linux Systems

Before installing, you can try the live demo of TLDR.

How to Install TLDR Pages in Linux Systems

To conveniently access TLDR pages, you need to install one of the supported clients called Node.js, which is the original client for the tldr-pages project. We can install it from NPM by running.

$ sudo npm install -g tldr

TLDR also available as a Snap package, to install it, run.

$ sudo snap install tldr

After installing the TLDR client, you can view man pages of any command, for example tar command here (you can use any other command here):

$ tldr tar

View Tar Command Man Page

View Tar Command Man Page

Here is another example of accessing the summarized man page for ls command.

$ tldr ls

View ls Command Man Page

View ls Command Man Page

To list all commands for the chosen platform in the cache, use the -l flag.

$ tldr -l 

List All Linux Commands

List All Linux Commands

To list all supported commands in the cache, use the -a flag.

$ tldr -a

You can update or clear the local cache by running.

$ tldr -u	#update local cache 
OR
$ tldr -c 	#clear local cache

To search pages using keywords, use the -s options, for example.

$ tldr -s  "list of all files, sorted by modification date"

Search Linux Commands Using Keyword

Search Linux Commands Using Keyword

To change the color theme (simple, base16, ocean), use the -t flag.

$ tldr -t ocean

You can also show a random command, with the -r flag.

$ tldr -r   

View Man Page for Random Linux Command

View Man Page for Random Linux Command

You can see a complete list of supported options by running.

$ tldr -h

Note: You can find a list of all supported and dedicated client applications for different platforms, in the TLDR clients wiki page.

TLDR Project Homepagehttps://tldr.sh/

That’s all for now! The TLDR pages are summarized practical examples of commands provided by the community. In this short article, we’ve showed how to install and use TLDR pages in Linux. Use the feedback form to share your thoughts about TLDR or share with us any similar programs out there.

Source

Zenity – Creates Graphical (GTK+) Dialog Boxes in Command-line and Shell Scripts

GNU Linux, the operating system built on very powerful Kernel called Linux. Linux is famous for its command Line operations. With the invent of Linux in day-to-day and Desktop computing, nix remains no more biased towards command-Line, it is equally Graphical and developing Graphical application remains no more a difficult task.

Install Zenity in Linux

Zenity Display Graphical Boxes

Here in this article we will be discussing creation and execution of simple Graphical Dialog box using GTK+application called “Zenity“.

What is Zenity?

Zenity is an open source and a cross-platform application which displays GTK+ Dialog Boxes in command-line and using shell scripts. It allows to ask and present information to/from shell in Graphical Boxes. The application lets you create Graphical dialog boxes in command-line and makes the interaction between user and shell very easy.

There are other alternatives, but nothing compares to the simplicity of Zenity, specially when you don’t need complex programming. Zenity, a tool you must have your hands on.

Zenity Features

  1. FOSS Software
  2. Cross Platform Application
  3. Allow GTK+ Dialog Box Execution
  4. Command Line Tool
  5. Support in Shell Scripting

Usefulness

  1. Easy GUI Creation
  2. Less features than other complex Tools
  3. Enables shell scripts to interact with a GUI users
  4. Simple dialog creation is possible for graphical user interaction

Since Zenity is available for all known major platforms, and based on GTK+ library, Zenity program can be ported to/from another platform.

Installation of Zenity in Linux

Zentity is by default installed or available in repository of most of the Standard Linux distribution of today. You can check if is installed onto your machine or not by executing following commands.

ravisaive@tecmint:~$ zenity --version 

3.8.0
ravisaive@tecmint:~$ whereis zenity 

zenity: /usr/bin/zenity /usr/bin/X11/zenity /usr/share/zenity /usr/share/man/man1/zenity.1.gz

If it’s not installed, you can install it using Apt or Yum command as shown below.

ravisaive@tecmint:~$ sudo apt-get install zenity		[on Debian based systems]

root@tecmint:~# yum install zenity				[on RedHat based systems]

Moreover you can also build it from the source files, download the latest Zenity source package (i.e. current version 3.8) using a following link.

  1. http://ftp.gnome.org/pub/gnome/sources/zenity/

Zenity Basic Dialog Boxes

Some of the basic Dialogs of Zenity, which can be invoked directly from the command-line.

1. How about a quick calendar dialog?
root@tecmint:~# zenity --calendar

Calendar Dialogue

Calendar Dialog

2. An error Dialog Box
root@tecmint:~# zenity --error

Error Dialogue

Error Dialog

3. A General text Entry Dialog Box
root@tecmint:~# zenity --entry

Entry Dialogue

Entry Dialog

4. An Information Dialog
root@tecmint:~# zenity --info

Info Dialogue

Info Dialog

5. A question Dialog box
root@tecmint:~# zenity --question

Question Box

Question Box

6. A progress Bar
root@tecmint:~# zenity --progress

Progress Bar

Progress Bar

7. Scale Dialog
root@tecmint:~# zenity --scale

Scale Box

Scale Box

8. A Password Dialog
root@tecmint:~# zenity --password

Password Box

Password Box

9. A Form Dialog box
root@tecmint:~# zenity --forms

Forms

Forms

10. An about Dialog
root@tecmint:~# zenity --about

About Zenity

About Zenity

Create Shell Script Dialog

Now we would be discussing Zenity Dialog creation using simple shell scripts here. Although we can create single Dialog by executing Zenity commands directly from the shell (as we did above) but then we can’t link two Dialog boxes in order to obtain some meaningful result.

How about an interactive dialog box which takes input from you, and shows the result.

#!/bin/bash 
first=$(zenity --title="Your's First Name" --text "What is your first name?" --entry) 
zenity --info --title="Welcome" --text="Mr./Ms. $first" 
last=$(zenity --title="Your's Last Name" --text "$first what is your last name?" --entry) 
zenity --info --title="Nice Meeting You" --text="Mr./Ms. $first $last"

Save it to ‘anything.sh‘ (conventionally) and do not forget to make it executable. Set 755 permission on anything.sh file and run the script.

root@tecmint:~# chmod 755 anything.sh 
root@tecmint:~# sh anything.sh

Enter Your First Name

Enter Your First Name

Welcome Dialogue

Welcome Dialog

Enter Your Last Name

Enter Your Last Name

Welcome Message

Welcome Message

About Script Description

The conventional shebang aka hashbang

#!/bin/bash

In the below line ‘first’ is a variable and the value of variable is Generated at run time.

    1. –entry‘ means zenity is asked to generate an text Entry box.
    2. – title=‘ defines the title of generated text box.
    3. —text=‘ defines the text that is available on text Entry box.
first=$(zenity --title="Your's First Name" --text "What is your first name?" --entry)

This line of the below script file is for generation of Information (–info) Dialog box, with title “Welcome” and Text “Mr./Ms.first”

zenity --info --title="Welcome" --text="Mr./Ms. $first"

This Line of the script is Similar to line number two of the script except here a new variable ‘last’ is defined.

last=$(zenity --title="Your's Last Name" --text "$first what is your last name?" --entry)

This last line of the script is again similar to the third line of the script and it generates information Dialog box which contains both the variables ‘$first’ and ‘$last’.

zenity --info --title="Nice Meeting You" --text="Mr./Ms. $first $last"

For more information on how to create custom dialog boxes using shell script, visit at following reference page Zenity.

  1. https://help.gnome.org/users/zenity/stable/

In the next article we would be integrating Zenity with more shell script for GUI user interaction. Till then stay tuned and connected to Tecmint. Don’t forget to give your valuable feedback in comment section.

Source

Gtkdialog – Create Graphical (GTK+) Interfaces and Dialog Boxes Using Shell Scripts in Linux

Gtkdialog (or gtkdialog) is an open source nifty utility for creating and building GTK+ Interfaces and Dialog Boxes with the help of Linux shell scripts and using GTK library, as well as using an xml-like syntax, which makes easy to create interfaces using gtkdialog. It is much similar to most famous tool called Zenity, but it comes with some useful customizable features that enables you to easily create many widgets like vbox, hbox, button, frame, text, menu, and a lot more.

Install Gtkdialog in Linux

Create Gtkdialog Boxes in Linux

Read Also : Create GTK+ Graphical Dialog Boxes using Zenity

Installation of Gtkdialog in Linux

You can download gtkdialog-0.8.3 (which is is the latest version) or you may also use wget command, unpack the downloaded file and run these following commands to compile from source.

$ sudo apt-get install build-essential		[on Debian based systems]
# yum install gcc make gcc-c++			[on RedHat based systems]
$ wget https://gtkdialog.googlecode.com/files/gtkdialog-0.8.3.tar.gz
$ tar -xvf gtkdialog-0.8.3.tar.gz
$ cd gtkdialog-0.8.3/
$ ./configure
$ make
$ sudo make install

Now let’s start creating some boxes, create a new “myprogram” script in your home folder.

My First Program
$ cd
$ touch myprogram

Now open the “myprogram” file using any text editor you want, and add the following code to it.

#!/bin/bash 

GTKDIALOG=gtkdialog 
export MAIN_DIALOG=' 

<window title="My First Program" icon-name="gtk-about" resizable="true" width-request="300" height-request="310"> 

<vbox> 
	<hbox space-fill="true" space-expand="true"> 
		<button>	 
			<label>Welcome to TecMint.com Home!</label> 
			<action>echo "Welcome to TecMint.com Home!"</action> 
		</button> 
	</hbox> 
</vbox> 
</window> 
' 

case $1 in 
	-d | --dump) echo "$MAIN_DIALOG" ;; 
	*) $GTKDIALOG --program=MAIN_DIALOG --center ;; 

esac 
------------

Save the file, and set execute permission and run it as shown.

$ chmod 755 myprogram
$ ./myprogram

This is how your first program created and executed using gtkdialog.

Gtkdialog Program

Gtkdialog Program

Now, we will explain the code in short.

  1. #!/bin/bash: The first line of any shell script, it is used to specify the bash shell path.
  2. GTKDIALOG = gtkdialog: Here we defined a variable to use it later when executing the shell script with gtkdialog, this line must be in all scripts that you create using gtkdialog.
  3. export MAIN_DIALOG=: Another variable we defined which will contain all syntax for our interface, you can replace MAIN_DIALOG with any name you want, but you have to replace it also in last 4 lines of the script.
  4. Window Title: I don’t think that this code need to be explained, we created a title, a default icon for the window, we choose if it was resizable or not, and we defined the width and height we want, of course all of those options are secondary, you can just use the <window> tag if you want.
  5. <vbox> : We use the vbox tag to create a vertical box, it is important to create a vbox tag in order to contain other tags such as hbox and button, etc.
  6. <hbox>: Here we created a horizontal box using the <hbox> tag, “space-fill” and “space-expand” are options to expand the hbox through the window.
  7. <button>: Create a new button.
  8. <label>: This is the default text for the button, we closed the label tag using </label>, of course it is very important to close all the tags that we use.
  9. <action>: This what happens when the button is clicked, you can run a shell command if you want or execute any other file if you want, there are many other actions and signals as well, don’t forget to close it using </action>.
  10. </button>: To close the button tag.
  11. </hbox>: To close the hbox tag.
  12. </window>: To close the window tag.

The last 4 lines must also be in all shell scripts that you create using gtkdialog, they execute the MAIN_DIALOG variable using gtkdialog command with the –center option to center the window, very useful in fact.

My Second Program

Similarly, create a another file and call it as ‘secondprogram‘ and add the following whole content to it.

#!/bin/bash 

GTKDIALOG=gtkdialog 
export MAIN_DIALOG=' 

<window title="My Second Program" icon-name="gtk-about" resizable="true" width-request="250" height-request="150"> 

<vbox> 
	<hbox space-fill="true"> 
		<combobox>	 
			<variable>myitem</variable> 
			<item>First One</item> 
			<item>Second One</item> 
			<item>Third One</item> 
		</combobox> 
	</hbox> 
	<hbox> 
		<button> 
			<label>Click Me</label> 
			<action>echo "You choosed $myitem"</action> 
		</button> 
	</hbox> 
<hseparator width-request="240"></hseparator> 

	<hbox> 
		<button ok></button> 
	</hbox> 
</vbox> 
</window> 
' 

case $1 in 
	-d | --dump) echo "$MAIN_DIALOG" ;; 
	*) $GTKDIALOG --program=MAIN_DIALOG --center ;; 

esac

Save the file, set execute permission on it and run it as shown.

$ chmod 755 secondprogram
$ ./secondprogram

Gtkdialog Select Box

Gtkdialog Select Box

Now, we will explain the code in short.

  1. We create a combobox widget using <combobox>, the <variable> tag is the default name of the variable which the chosen item will be stored in, we used this variable to print the selected item later using echo.
  2. <hseparator> is a horizontal separator, you can set the default width for it using width-request option.
  3. <button ok></button> is an OK button that will close the window just when you click it, it is very useful so we don’t need to create a custom button to do that.
My Third Prgoram

Create another file called ‘thirdprogram‘ and add the whole bunch of code to it.

#!/bin/bash 

GTKDIALOG=gtkdialog 
export MAIN_DIALOG=' 

<window title="My Second Program" icon-name="gtk-about" resizable="true" width-request="250" height-request="150"> 

<notebook tab-label="First | Second|"> 
<vbox> 
	<hbox space-fill="true"> 
		<combobox>	 
			<variable>myitem</variable> 
			<item>First One</item> 
			<item>Second One</item> 
			<item>Third One</item> 
		</combobox> 
	</hbox> 
	<hbox> 
		<button> 
			<label>Click Me</label> 
			<action>echo "You choosed $myitem"</action> 
		</button> 
	</hbox> 
<hseparator width-request="240"></hseparator> 

	<hbox> 
		<button ok></button> 
	</hbox> 
</vbox> 

<vbox> 

	<hbox space-fill="true"> 
		<text> 
		<label>Spinbutton </label> 
		</text> 
	</hbox> 

	<hbox space-fill="true" space-expand="true"> 
		<spinbutton range-min="0" range-max="100" range-value="4"> 
			<variable>myscale</variable> 
			<action>echo $myscale</action> 
		</spinbutton> 
	</hbox> 

	<hbox> 
		<button ok></button> 
	</hbox> 

</vbox> 
</notebook> 
</window> 
' 

case $1 in 
	-d | --dump) echo "$MAIN_DIALOG" ;; 
	*) $GTKDIALOG --program=MAIN_DIALOG --center ;; 

esac

Save the file, grant execute permission and fire it as shown.

$ chmod 755 thirdprogram
$ ./thirdprogram

Select Name from Dropdown

Select Name from Dropdown

Select Number from Dropdown

Select Number from Dropdown

Here, the explanation of code in more detailed fashion.

  1. We created two notebook tabs using <notebook>, the tab-label option is where you can create tabs, gtkdialog will create tabs depending on the labels you enter, every <vbox> is defined as a tab, so the first tab starts with the first <vbox>, the second tab starts with the second <vbox>.
  2. <text> is a text widget, we used the <label> tag to set the default text for it.
  3. <spinbutton> tag will create a new spin button, range-min option is the minimum value, and range-max is the maximum value for the spin button, range-value is the default value for the spin button.
  4. We gave a variable “myscale” to the <spinbutton>.
  5. We printed the selected value using echo and $myscale variable, the default signal for the action here is “value-changed” which helped us doing that.

This was just an example window, you can create more complicated interfaces using gtkdialog if you want, you can browse the official documentation in gtkdialog website to view all gtkdialog tags from the link below.

Gtkdialog Documentation

Have you used gtkdialog to create GUIs for your shell scripts before? Or have you used any such utility to create interfaces? What do you think about it?

Source

7 ‘dmesg’ Commands for Troubleshooting and Collecting Information of Linux Systems

The ‘dmesg‘ command displays the messages from the kernel ring buffer. A system passes multiple runlevel from where we can get lot of information like system architecture, cpu, attached device, RAM etc. When computer boots up, a kernel (core of an operating system) is loaded into memory. During that period number of messages are being displayed where we can see hardware devices detected by kernel.

Read Also10 Linux Commands to Collect System and Hardware Information

dmesg Command Examples

dmesg Command Examples

The messages are very important in terms of diagnosing purpose in case of device failure. When we connect or disconnect hardware device on the system, with the help of dmesg command we come to know detected or disconnected information on the fly. The dmesg command is available on most Linux and Unix based Operating System.

Let’s throw some light on most famous tool called ‘dmesg’ command with their practical examples as discussed below. The exact syntax of dmesg as follows.

# dmseg [options...]

1. List all loaded Drivers in Kernel

We can use text-manipulation tools i.e. ‘more‘, ‘tail‘, ‘less‘ or ‘grep‘ with dmesg command. As output of dmesg log won’t fit on a single page, using dmesg with pipe more or less command will display logs in a single page.

[root@tecmint.com ~]# dmesg | more
[root@tecmint.com ~]# dmesg | less
Sample Output
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.11.0-13-generic (buildd@aatxe) (gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu8) ) #20-Ubuntu SMP Wed Oct 23 17:26:33 UTC 2013 
(Ubuntu 3.11.0-13.20-generic 3.11.6)
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   NSC Geode by NSC
[    0.000000]   Cyrix CyrixInstead
[    0.000000]   Centaur CentaurHauls
[    0.000000]   Transmeta GenuineTMx86
[    0.000000]   Transmeta TransmetaCPU
[    0.000000]   UMC UMC UMC UMC
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[    0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000007dc08bff] usable
[    0.000000] BIOS-e820: [mem 0x000000007dc08c00-0x000000007dc5cbff] ACPI NVS
[    0.000000] BIOS-e820: [mem 0x000000007dc5cc00-0x000000007dc5ebff] ACPI data
[    0.000000] BIOS-e820: [mem 0x000000007dc5ec00-0x000000007fffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000e0000000-0x00000000efffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fec00000-0x00000000fed003ff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fed20000-0x00000000fed9ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fee00000-0x00000000feefffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000ffb00000-0x00000000ffffffff] reserved
[    0.000000] NX (Execute Disable) protection: active
.....

Read AlsoManage Linux Files Effectively using commands head, tail and cat

2. List all Detected Devices

To discover which hard disks has been detected by kernel, you can search for the keyword “sda” along with “grep” like shown below.

[root@tecmint.com ~]# dmesg | grep sda

[    1.280971] sd 2:0:0:0: [sda] 488281250 512-byte logical blocks: (250 GB/232 GiB)
[    1.281014] sd 2:0:0:0: [sda] Write Protect is off
[    1.281016] sd 2:0:0:0: [sda] Mode Sense: 00 3a 00 00
[    1.281039] sd 2:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[    1.359585]  sda: sda1 sda2 < sda5 sda6 sda7 sda8 >
[    1.360052] sd 2:0:0:0: [sda] Attached SCSI disk
[    2.347887] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null)
[   22.928440] Adding 3905532k swap on /dev/sda6.  Priority:-1 extents:1 across:3905532k FS
[   23.950543] EXT4-fs (sda1): re-mounted. Opts: errors=remount-ro
[   24.134016] EXT4-fs (sda5): mounted filesystem with ordered data mode. Opts: (null)
[   24.330762] EXT4-fs (sda7): mounted filesystem with ordered data mode. Opts: (null)
[   24.561015] EXT4-fs (sda8): mounted filesystem with ordered data mode. Opts: (null)

NOTE: The ‘sda’ first SATA hard drive, ‘sdb’ is the second SATA hard drive and so on. Search with ‘hda’ or ‘hdb’ in the case of IDE hard drive.

3. Print Only First 20 Lines of Output

The ‘head’ along with dmesg will show starting lines i.e. ‘dmesg | head -20’ will print only 20 lines from the starting point.

[root@tecmint.com ~]# dmesg | head  -20

[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.11.0-13-generic (buildd@aatxe) (gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu8) ) #20-Ubuntu SMP Wed Oct 23 17:26:33 UTC 2013 (Ubuntu 3.11.0-13.20-generic 3.11.6)
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   NSC Geode by NSC
[    0.000000]   Cyrix CyrixInstead
[    0.000000]   Centaur CentaurHauls
[    0.000000]   Transmeta GenuineTMx86
[    0.000000]   Transmeta TransmetaCPU
[    0.000000]   UMC UMC UMC UMC
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[    0.000000] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000007dc08bff] usable
[    0.000000] BIOS-e820: [mem 0x000000007dc08c00-0x000000007dc5cbff] ACPI NVS
[    0.000000] BIOS-e820: [mem 0x000000007dc5cc00-0x000000007dc5ebff] ACPI data
[    0.000000] BIOS-e820: [mem 0x000000007dc5ec00-0x000000007fffffff] reserved

4. Print Only Last 20 Lines of Output

The ‘tail’ along with dmesg command will print only 20 last lines, this is useful in case we insert removable device.

[root@tecmint.com ~]# dmesg | tail -20

parport0: PC-style at 0x378, irq 7 [PCSPP,TRISTATE]
ppdev: user-space parallel port driver
EXT4-fs (sda1): mounted filesystem with ordered data mode
Adding 2097144k swap on /dev/sda2.  Priority:-1 extents:1 across:2097144k
readahead-disable-service: delaying service auditd
ip_tables: (C) 2000-2006 Netfilter Core Team
nf_conntrack version 0.5.0 (16384 buckets, 65536 max)
NET: Registered protocol family 10
lo: Disabled Privacy Extensions
e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
Slow work thread pool: Starting up
Slow work thread pool: Ready
FS-Cache: Loaded
CacheFiles: Loaded
CacheFiles: Security denies permission to nominate security context: error -95
eth0: no IPv6 routers present
type=1305 audit(1398268784.593:18630): audit_enabled=0 old=1 auid=4294967295 ses=4294967295 res=1
readahead-collector: starting delayed service auditd
readahead-collector: sorting
readahead-collector: finished

5. Search Detected Device or Particular String

It’s difficult to search particular string due to length of dmesg output. So, filter the lines with are having string like ‘usb‘ ‘dma‘ ‘tty‘ and ‘memory‘ etc. The ‘-i’ option instruct to grep command to ignore the case (upper or lower case letters).

[root@tecmint.com log]# dmesg | grep -i usb
[root@tecmint.com log]# dmesg | grep -i dma
[root@tecmint.com log]# dmesg | grep -i tty
[root@tecmint.com log]# dmesg | grep -i memory
Sample Output
[    0.000000] Scanning 1 areas for low memory corruption
[    0.000000] initial memory mapped: [mem 0x00000000-0x01ffffff]
[    0.000000] Base memory trampoline at [c009b000] 9b000 size 16384
[    0.000000] init_memory_mapping: [mem 0x00000000-0x000fffff]
[    0.000000] init_memory_mapping: [mem 0x37800000-0x379fffff]
[    0.000000] init_memory_mapping: [mem 0x34000000-0x377fffff]
[    0.000000] init_memory_mapping: [mem 0x00100000-0x33ffffff]
[    0.000000] init_memory_mapping: [mem 0x37a00000-0x37bfdfff]
[    0.000000] Early memory node ranges
[    0.000000] PM: Registered nosave memory: [mem 0x0009f000-0x000effff]
[    0.000000] PM: Registered nosave memory: [mem 0x000f0000-0x000fffff]
[    0.000000] please try 'cgroup_disable=memory' option if you don't want memory cgroups
[    0.000000] Memory: 2003288K/2059928K available (6352K kernel code, 607K rwdata, 2640K rodata, 880K init, 908K bss, 56640K reserved, 1146920K highmem)
[    0.000000] virtual kernel memory layout:
[    0.004291] Initializing cgroup subsys memory
[    0.004609] Freeing SMP alternatives memory: 28K (c1a3e000 - c1a45000)
[    0.899622] Freeing initrd memory: 23616K (f51d0000 - f68e0000)
[    0.899813] Scanning for low memory corruption every 60 seconds
[    0.946323] agpgart-intel 0000:00:00.0: detected 32768K stolen memory
[    1.360318] Freeing unused kernel memory: 880K (c1962000 - c1a3e000)
[    1.429066] [drm] Memory usable by graphics device = 2048M

6. Clear dmesg Buffer Logs

Yes, we can clear dmesg logs if required with below command. It will clear dmesg ring buffer message logs till you executed the command below. Still you can view logs stored in ‘/var/log/dmesg‘ files. If you connect any device will generate dmesg output.

[root@tecmint.com log]# dmesg -c

7. Monitoring dmesg in Real Time

Some distro allows command ‘tail -f /var/log/dmesg’ as well for real time dmesg monitoring.

[root@tecmint.com log]# watch "dmesg | tail -20"

Conclusion: The dmesg command is useful as dmesg records all the system changes done or occur in real time. As always you can man dmesg to get more information.

Source

WP2Social Auto Publish Powered By : XYZScripts.com