Difference between revisions of "Miscellaneous Linux Commands"

From TheBestLinux.com
Jump to navigation Jump to search
 
(14 intermediate revisions by the same user not shown)
Line 38: Line 38:
 
<br />
 
<br />
 
<br />
 
<br />
== <h3>How to grep(find text string) from files that have been compressed with Gzip</h3> ==
+
OR
Unix and Linux systems come with a modified version of grep named zgrep. The Linux zgrep command works just like the grep command, except it works on text files that have been compressed with the gzip command.
+
<br /><br />
 +
<pre style="color:blue">
 +
du -a /directory_name | sort -n -r | head -n 10
 +
</pre>
 +
<br /><br />
 +
Many other examples with more advanced options can be found at this URL:<br />
 +
https://www.tutorialspoint.com/find-the-largest-top-10-files-and-directories-on-a-linux
 +
<br /><br />
 +
 
 +
== <h3>How to grep from files that have been compressed with Gzip using zgrep</h3> ==
 +
Unix and Linux systems come with a modified version of grep named zgrep. The Linux zgrep command works just like the grep command, except it works on text files that have been compressed with gzip.
 +
<br /><br />
 +
The zgrep command does not support recursive usage, however, the following command line string does allow you to zgrep recursively through multiple directory levels:
 +
<pre style="color:blue">
 +
find . -name *.gz -exec zgrep -- string_searching_for {} +
 +
</pre>
 
<br />
 
<br />
 
<br />
 
<br />
 +
 
== <h3>Change Time Zone of Linux Server from Command Line</h3> ==
 
== <h3>Change Time Zone of Linux Server from Command Line</h3> ==
 
To manually and permanently change the time zone of a Linux system, it's a very simple matter of copying the time zone file you want over the localtime file of the system, as follows(Using U.S. Pacific Standard Time):
 
To manually and permanently change the time zone of a Linux system, it's a very simple matter of copying the time zone file you want over the localtime file of the system, as follows(Using U.S. Pacific Standard Time):
Line 57: Line 73:
 
The .bashrc file contains aliases, which are custom commands used often.  For instance, I like having an alias called "cls" to run the shell "clear" command to clear the contents of the currently visible screen.
 
The .bashrc file contains aliases, which are custom commands used often.  For instance, I like having an alias called "cls" to run the shell "clear" command to clear the contents of the currently visible screen.
 
More bash shell configuration info to come when I have the time.
 
More bash shell configuration info to come when I have the time.
 +
<br /><br />
 +
Here's my standard .bashrc add-ons I typically add to all new systems.  The first, "export PS1", heavily customizes the command prompt to display the entire current path in nice colors along with the FQDN - the Fully Qualified Domain Name, such as "TheBestLinux.com".
 +
<br />
 +
The second customization I make is adding an alias to the "nslookup" command, so that it is always run with the "norecur" switch, forcing nslookup to always run in noresursive mode.
 +
<br />
 +
The last extra customization is setting the "EDITOR" variable to always use "VI" instead of Nano or Pico or some other user friendly, but not as powerfull command line editor.
 +
<br />
 +
Here's what the end of my customized .bashrc file looks like:
 +
<pre style="color:blue">
 +
# Added by Jamie:
 +
export PS1='[\[\e[0;36m\]\u\[\e[0m\]@\[\e[0;33m\]\H\[\e[0m\]:\[\e[0;35m\]\w\[\e[0m\]]$ '
 +
alias nslookup="nslookup -norecur"
 +
EDITOR=/usr/bin/vi
 +
</pre>
 +
<br /><br />
  
 
== <h3>Convert Computer Epoch Date & Time Codes to Human Readable Dates & Times</h3> ==
 
== <h3>Convert Computer Epoch Date & Time Codes to Human Readable Dates & Times</h3> ==
Line 88: Line 119:
 
<br />
 
<br />
 
This simple example can be applied to thousands of scenarios where you want to iterate through some data a specific number of times, or until a specific target is reached.  Use your imagination to apply this to any repetitive command line tools you use!
 
This simple example can be applied to thousands of scenarios where you want to iterate through some data a specific number of times, or until a specific target is reached.  Use your imagination to apply this to any repetitive command line tools you use!
 +
<br /><br />
 +
 +
== <h3>E-Mail Info</h3> ==
 +
To redirect email sent to an alias address to another email address on the Linux email server, edit the /etc/aliases file, and then be sure to '''IMMEDIATELY''' run the <b>newaliases</b> command to rebuild the actual database the MTA uses!
 
<br /><br />
 
<br /><br />
  
Line 192: Line 227:
 
== <h4>USB Devices</h4> ==
 
== <h4>USB Devices</h4> ==
 
<br />
 
<br />
To determine which device your USB thumb drive is connect to, use the "dmesg" command again, as follows:
+
To determine which device your USB thumb drive is connected to, use the "dmesg" command again, as follows:
 
<pre style="color:blue">
 
<pre style="color:blue">
 
# dmesg | grep -i 'Attached SCSI'
 
# dmesg | grep -i 'Attached SCSI'

Latest revision as of 01:43, 20 November 2024

Shell Commands


Display all network interface names on any Linux system from the shell:

/sbin/ifconfig | grep BROADCAST | cut -d " " -f1 | sed 's/.$//' 


Most Linux machines will only display one network interface, such as shown on my Slackware 14.1 Virtual Machine Server:

eth0 


Other systems may have more than one, such as those with virtual machine software or with more than one physical NIC.
Also, some systems use non-standared network interface names, other than the usual eth0.

Here are some samples of those types of systems. In these examples, the first network interface name is the main one used, and the following ones are used for the virtual machine software.

Sample output from my Fedora 20 (Heisenbug) VirtualBox server:

em1
virbr0


And here is a sample output from my Fedora 17 (Beefy Miracle) laptop running VMWare:

p5p1                                                                           
vmnet1                                                                         
vmnet8



The "ps" Process Command Syntax

To show all executing processes:

$ ps ax


To show all processed running by a specific user(This example shows all processes running by the Apache WebServer):

$ ps -U apache



How to find the 10 largest files in the current directory

ls -XS | head -n 10



OR

du -a /directory_name | sort -n -r | head -n 10



Many other examples with more advanced options can be found at this URL:
https://www.tutorialspoint.com/find-the-largest-top-10-files-and-directories-on-a-linux

How to grep from files that have been compressed with Gzip using zgrep

Unix and Linux systems come with a modified version of grep named zgrep. The Linux zgrep command works just like the grep command, except it works on text files that have been compressed with gzip.

The zgrep command does not support recursive usage, however, the following command line string does allow you to zgrep recursively through multiple directory levels:

find . -name *.gz -exec zgrep -- string_searching_for {} +



Change Time Zone of Linux Server from Command Line

To manually and permanently change the time zone of a Linux system, it's a very simple matter of copying the time zone file you want over the localtime file of the system, as follows(Using U.S. Pacific Standard Time):
MUST be done as root user! System must be rebooted for changes to take effect!

[root@server ~]# cp /usr/share/zoneinfo/America/Los_Angeles /etc/localtime  



Customizing Your Shell & Environment

Most people use the default Linux shell Bash, the Bourne Again SHell, which is basically the Bourne shell with lots of cool add-ons and extra features, such as command completion. There are two main configuration files for your shell, and they are "hidden" files, starting with a dot, so they aren't shown during a normal directory listing unless specified. The configuration files are .bashrc and .bash_profile The .bashrc file contains aliases, which are custom commands used often. For instance, I like having an alias called "cls" to run the shell "clear" command to clear the contents of the currently visible screen. More bash shell configuration info to come when I have the time.

Here's my standard .bashrc add-ons I typically add to all new systems. The first, "export PS1", heavily customizes the command prompt to display the entire current path in nice colors along with the FQDN - the Fully Qualified Domain Name, such as "TheBestLinux.com".
The second customization I make is adding an alias to the "nslookup" command, so that it is always run with the "norecur" switch, forcing nslookup to always run in noresursive mode.
The last extra customization is setting the "EDITOR" variable to always use "VI" instead of Nano or Pico or some other user friendly, but not as powerfull command line editor.
Here's what the end of my customized .bashrc file looks like:

# Added by Jamie:
export PS1='[\[\e[0;36m\]\u\[\e[0m\]@\[\e[0;33m\]\H\[\e[0m\]:\[\e[0;35m\]\w\[\e[0m\]]$ '
alias nslookup="nslookup -norecur"
EDITOR=/usr/bin/vi



Convert Computer Epoch Date & Time Codes to Human Readable Dates & Times

From the Linux command line shell:

date -d @1449129600


Gives the output of:

Thu Dec  3 00:00:00 PST 2015



Using perl:

perl -le 'print scalar localtime $ARGV[0]' 1449129600


Gives the output of:

Thu Dec  3 00:00:00 2015



Display the Full Calendars of the Last 20 Years with a One-Liner Command!

For example, say it is the year 2015, and you want to display all of the full calendars for the last 20 years, you can you this one line command to spit it out to the terminal console, or redirect it into a text file for later use.

This is how to do it with what is known as a "One-Liner" command, which is actually a set of commands strung together on one line:

$ for ((year=1996;year<=2015;year++)); do cal $year; done


This simple example can be applied to thousands of scenarios where you want to iterate through some data a specific number of times, or until a specific target is reached. Use your imagination to apply this to any repetitive command line tools you use!

E-Mail Info

To redirect email sent to an alias address to another email address on the Linux email server, edit the /etc/aliases file, and then be sure to IMMEDIATELY run the newaliases command to rebuild the actual database the MTA uses!

Miscellaneous CD/DVD & USB Command Line Commands

To show all mounted drives, both the "mount" and "df" commands are useful and helful, as seen here(Example using one of my servers):

$ mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=8104892k,nr_inodes=2026223,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
/dev/mapper/fedora-root on / type ext4 (rw,relatime,seclabel,data=ordered)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=41,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
tmpfs on /tmp type tmpfs (rw,seclabel)
configfs on /sys/kernel/config type configfs (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
sunrpc on /proc/fs/nfsd type nfsd (rw,relatime)
/dev/md126p1 on /boot type ext4 (rw,relatime,seclabel,data=ordered)
/dev/mapper/data-home on /home type ext4 (rw,relatime,seclabel,data=ordered)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
gvfsd-fuse on /run/user/1000/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)


Output of the "df" command on one of my servers: (I added the -h switch to show sizes in "Human Readable" format, meaning it shows an M or G for Megabytes & Gigabytes)

$ df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/fedora-root        58G   47G  8.0G  86% /
devtmpfs                      7.8G     0  7.8G   0% /dev
tmpfs                         7.8G   88M  7.7G   2% /dev/shm
tmpfs                         7.8G  1.2M  7.8G   1% /run
tmpfs                         7.8G     0  7.8G   0% /sys/fs/cgroup
tmpfs                         7.8G   92M  7.7G   2% /tmp
/dev/mapper/extra-extra1      197G   93G   95G  50% /mnt/extra1
/dev/mapper/more-morespace1   586G  371G  186G  67% /mnt/more1
/dev/md126p1                  969M  116M  787M  13% /boot
/dev/mapper/fedora-save       197G   60M  187G   1% /mnt/save
/dev/mapper/data-vms2         296G  180G  102G  64% /mnt/vms2
/dev/mapper/backup-vms        197G  184G  3.7G  99% /mnt/vms
/dev/mapper/archive-archive1  501G  399G   77G  84% /mnt/archive
/dev/mapper/data-data1         99G   79G   15G  84% /mnt/data1
/dev/mapper/data-home         197G  172G   16G  92% /home



***** VERY IMPORTANT INFO!!! *****

In order to "SAFELY" remove a CD, DVD, USB, Removable Hard Drive, or any type of removable media or drive, YOU MUST FIRST UNMOUNT the removable media or drive, and also possibly detatch and/or eject the media or drive. Here are the command line commands to perform these functions:
The first step applies to all situations and types of drives or medium, which is to "UNMOUNT" the drive, using the "umount" command. NOTICE the "N" is dropped in the actual unmount command, so "umount" is NOT a typo!

For example, here is how to unount a mounted CD ROM disk inserted into the drive. Also, instead of pushing the little push button on the front of CD & DVD drives, I prefer to use command line commands to both open and close the drive tray, if it is the type that has a tray that opens/ejects to insert a disk.

To open and close the CD/DVD drive tray, use the eject command as follows:

$ eject

This should open the disk tray. If you have more than one CD/DVD ROM drive, you need to specify the device name of the unit you want to use, as shown here:

$ eject /dev/sdb 

or

$ eject /dev/sdc 


The actual device name depends on how it is installed in your computer. To get an idea of the actual device name your computer uses for it's CD/DVD ROM drives, I like to use the "dmesg" command, as show here:

$ dmesg | grep -i cd-rom
[    0.666858] scsi 4:0:0:0: CD-ROM            ASUS     DRW-24F1ST   a   1.00 PQ: 0 ANSI: 5
[    0.670145] cdrom: Uniform CD-ROM driver Revision: 3.20
[    0.670287] sr 4:0:0:0: Attached scsi CD-ROM sr0


As you can see from above, the CD/DVD ROM burner I have is an Asus DRW-24F1ST, attached to scsi CD-ROM device "sr0".
This is the important device name: sr0 which you can then use to eject/open and close the CD-ROM drive tray.
FYI, "sr0" stands for "SCSI Recorder 0", in case you were wondering...

So, the actual working example on my server to open/eject the tray is:

$ eject /dev/sr0 


The actual working command to close the CD-ROM tray after I've inserted a disk into the tray on my server is:

$ eject -t /dev/sr0 



USB Devices


To determine which device your USB thumb drive is connected to, use the "dmesg" command again, as follows:

# dmesg | grep -i 'Attached SCSI'



To manually check & fix a USB file system, use this example(As the root super user):

# fsck.fat -atvV /dev/sde1
fsck.fat 3.0.25 (2014-01-17)
fsck.fat 3.0.25 (2014-01-17)
Checking we can access the last sector of the filesystem
0x41: Dirty bit is set. Fs was not properly unmounted and some data may be corrupt.
 Automatically removing dirty bit.
Boot sector contents:
System ID "MSDOS5.0"
Media byte 0xf8 (hard disk)
       512 bytes per logical sector
     16384 bytes per cluster
     10144 reserved sectors
First FAT starts at byte 5193728 (sector 10144)
         2 FATs, 32 bit entries
   3784704 bytes per FAT (= 7392 sectors)
Root directory start at cluster 2 (arbitrary size)
Data area starts at byte 12763136 (sector 24928)
    945313 data clusters (15488008192 bytes)
63 sectors/track, 128 heads
      8064 hidden sectors
  30274944 sectors total
Starting check/repair pass.
Checking for bad clusters.
Reclaiming unconnected clusters.
Checking free cluster summary.
Starting verification pass.
Checking for unused clusters.
Performing changes.
/dev/sde1: 30111 files, 611080/945313 clusters



Safest way to totally and completely disconnect a USB device from a Linux machine:
(Assuming your USB device is detected by the OS as /dev/sde - adjust command for your device name!)

udisks --unmount /dev/sde && udisks --detach /dev/sde
OR if already unmounted:
udisks --detach /dev/sde 


This safely powers down the USB device, causing any LED light on the USB device to turn off, indicating it is truly safe to unplug the USB thumb drive!


To format USB thumb type drive with original FAT file system, so it can be read by all computers,

****************************************************************************************************************************
***** FIRST:  SAVE ALL DATA you do not want to lose!!!  Otherwise, you WILL lose it after you follow this procedure!!! *****
****************************************************************************************************************************



Delete all existing partitions using fdisk(substituting x for the actual drive letter your USB drive is recognized as by the "dmesg" command:

fdisk /dev/sdx


Then, within fdisk, use the "D" key, lower case, not shifted upper case as I have typed here, to delete each partition that may exist.
Once all partitions have been deleted, create ONE(1) new partition by tapping the "N" key, again lower case, not shifted as in my example.
Then, hit the enter key to complete creation of the new partition, and then change the partition type by tapping the "T" key and then tapping the "B" key.
This should change the partition type from Linux type 82 to FAT 32 type b. Confirm this by tapping the "P" key and hitting "ENTER".
Create ONE(1) new partition of b type(FAT 32, NOT LBA!).


Format new partition on USB drive using this command:
(replacing x with your USB key drive letter)

mkfs.vfat -F 32 /dev/sdx1