10 SCP Commands to Transfer Files/Folders in Linux

Linux administrator should be familiar with CLI environment. Since GUI mode in Linux servers is not a common to be installed. SSH may the most popular protocol to enable Linux administrator to manage the servers via remote in secure way. Built-in with SSH command there is SCP command. SCP is used to copy file(s) between servers in secure way.

Linux SCP Commands

10 Linux SCP Commands

The below command will read as “copy source_file_name” into “destination_folder” at “destination_host” using “username account”.

Basic syntax of SCP
scp source_file_name username@destination_host:destination_folder

There are much parameters in SCP command that you can use. Here are the parameters that may useful on daily basis usage.

Provide the detail information of SCP process using -v parameter

Basic SCP command without parameter will copy the files in background. User will see nothing unless the process is done or some error appears. You can use “-v” parameter to print debug information into the screen. It can help you debugging connection, authentication and configuration problems.

pungki@mint ~/Documents $ scp -v Label.pdf mrarianto@202.x.x.x:.
Sample Output
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: Host '202.x.x.x' is known and matches the RSA host key.
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Next authentication method: password
mrarianto@202.x.x.x's password:
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
Sending file modes: C0770 3760348 Label.pdf
Sink: C0770 3760348 Label.pdf
Label.pdf 100% 3672KB 136.0KB/s 00:27
Transferred: sent 3766304, received 3000 bytes, in 65.2 seconds
Bytes per second: sent 57766.4, received 46.0
debug1: Exit status 0

Provide modification times, access times, and modes from original files

The “-p” parameter will help you on this. An estimated time and the connection speed will appear on the screen.

pungki@mint ~/Documents $ scp -p Label.pdf mrarianto@202.x.x.x:.
Sample Output
mrarianto@202.x.x.x's password:
Label.pdf 100% 3672KB 126.6KB/s 00:29

Make file transfer faster using -C parameter

One of parameter that can faster your file transfer is “-C” parameter. The “-C” parameter will compress your files on the go. The unique thing is the compression is only happen in the network. When the file is arrived to the destination server, it will returning into the original size as before the compression happen.

Take a look of these commands. It is using a single file of 93 Mb.

pungki@mint ~/Documents $ scp -pv messages.log mrarianto@202.x.x.x:.
Sample Output
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
mrarianto@202.x.x.x's password:
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: Sending command: scp -v -p -t .
File mtime 1323853868 atime 1380425711
Sending file timestamps: T1323853868 0 1380425711 0
messages.log 100% 93MB 58.6KB/s 27:05
Transferred: sent 97614832, received 25976 bytes, in 1661.3 seconds
Bytes per second: sent 58758.4, received 15.6
debug1: Exit status 0

Copying file without “-C” parameter will result 1661.3 second. Yo may compare the result to the command below which using “-C” parameter.

pungki@mint ~/Documents $ scp -Cpv messages.log mrarianto@202.x.x.x:.
Sample Output
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Host '202.x.x.x' is known and matches the RSA host key.
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Next authentication method: publickey
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
mrarianto@202.x.x.x's password:
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: channel 0: new [client-session]
debug1: Sending command: scp -v -p -t .
File mtime 1323853868 atime 1380428748
Sending file timestamps: T1323853868 0 1380428748 0
Sink: T1323853868 0 1380428748 0
Sending file modes: C0600 97517300 messages.log
messages.log 100% 93MB 602.7KB/s 02:38
Transferred: sent 8905840, received 15768 bytes, in 162.5 seconds
Bytes per second: sent 54813.9, received 97.0
debug1: Exit status 0
debug1: compress outgoing: raw data 97571111, compressed 8806191, factor 0.09
debug1: compress incoming: raw data 7885, compressed 3821, factor 0.48

As you can see, when you are using compression, transfer process is done in 162.5 second. It is 10 times faster than not using “-C” parameter. If you are copying a lot files across the network, “-C” parameter would help you to decrease the total time you need.

The thing that we should notice that compression method will not work on any files. When the source file is already compressed, you will not find any improvement there. Files such as .zip.rarpictures, and .iso files will not affected by “-C” parameter.

Select another cipher to encrypt files

By default SCP using “AES-128” to encrypt files. If you want to change to another cipher to encrypt it, you can use “-c” parameter. Take a look of this command.

pungki@mint ~/Documents $ scp -c 3des Label.pdf mrarianto@202.x.x.x:.

mrarianto@202.x.x.x's password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

Above command tell SCP to use 3des algorithm to encrypt file. Please be careful that this parameter using “-c” not “-C“.

Limiting bandwidth usage

Another parameter that may useful is “-l” parameter. The “-l” parameter will limit the bandwidth to use. It will be useful if you do an automation script to copy a lot of file, but you don’t want the bandwidth is drained by the SCPprocess.

pungki@mint ~/Documents $ scp -l 400 Label.pdf mrarianto@202.x.x.x:.

mrarianto@202.x.x.x's password:
Label.pdf 100% 3672KB 50.3KB/s 01:13

The 400 value behind “-l” parameter is mean that we limit the bandwidth for SCP process only 50 KB/sec. One thing to remember that bandwidth is specified in Kilobits/sec (kbps). It is mean that 8 bits equal with 1 byte.

While SCP counts in Kilobyte/sec (KB/s). So if you want to limit your bandwidth for SCP maximum only 50 KB/s, you need to set it into 50 x 8 = 400.

Specify specific port to use with SCP

Usually SCP is using port 22 as a default port. But for security reason, you may change the port into another port. For example, we are using port 2249. Then the command should be like this.

pungki@mint ~/Documents $ scp -P 2249 Label.pdf mrarianto@202.x.x.x:.

mrarianto@202.x.x.x's password:
Label.pdf 100% 3672KB 262.3KB/s 00:14

Make sure that it use capital “P” not “p“, since “p” is already used for preserved times and modes.

Copy files inside directory recursively

Sometimes we need to copy directory and all files / directories inside it. It will be better if we can do it in 1command. SCP support that scenario using “-r” parameter.

pungki@mint ~/Documents $ scp -r documents mrarianto@202.x.x.x:.

mrarianto@202.x.x.x's password:
Label.pdf 100% 3672KB 282.5KB/s 00:13
scp.txt 100% 10KB 9.8KB/s 00:00

When the copy process is done, at the destination server you will found a directory named “documents” with all it’s files. The folder “documents” is automatically created.

Disable progress meter and warning / diagnostic message

If you choose not to see progress meter and warning / diagnostic messages from SCP, you may disable it using “-q” parameter. Here’s the example.

pungki@mint ~/Documents $ scp -q Label.pdf mrarianto@202.x.x.x:.

mrarianto@202.x.x.x's password:
pungki@mint ~/Documents $

As you can see, after the you enter the password, there is no any information about SCP process. After the process is complete, you will be see a prompt again.

Copy files using SCP through Proxy

Proxy server is usually used in office environment. Natively, SCP is not proxy configured. When your environment using proxy, you have to “tell” SCP to communicate with the proxy.

Here’s the scenario. The proxy address is 10.0.96.6 and the proxy port is 8080. The proxy also implemented user authentication. First, you need to create “~/.ssh/config” file. Second you put this command inside it.

ProxyCommand /usr/bin/corkscrew 10.0.96.6 8080 %h %p ~/.ssh/proxyauth

Then you need to create file “~/.ssh/proxyauth” which contain.

myusername:mypassword

After that you can do SCP transparently as usual.

Please notice that corkscrew is might not installed yet on your system. On my Linux Mint, I need to install it first, using standard Linux Mint installation procedure.

$ apt-get install corkscrew

For other yum based systems, users can install corkscrew using the following yum command.

# yum install corkscrew

Another thing that since “~/.ssh/proxyauth” file contain your “username” and “password” in clear-text format, please make sure that the file can be accessed by you only.

Select different ssh_config file

For mobile user who often switch between company network and public network, it will be suffer to always change settings in SCP. It is better if we can put a different ssh_config file to match our needs.

Here’s a sample scenario

Proxy is used in company network but not in public network and you are regularly switch network.

pungki@mint ~/Documents $ scp -F /home/pungki/proxy_ssh_config Label.pdf

mrarianto@202.x.x.x:.
mrarianto@202.x.x.x's password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

By default “ssh_config” file per user will be placed in “~/.ssh/config“. Creating a specific “ssh_config” file with proxy compatible, will make you easier to switch between networks.

When you are on company network, you can use “-F” parameter. When you are on public network, you can skip “-F” parameter.

That’s all about SCP. You can see man pages of SCP for more detail. Please feel free to leave comments and suggestions.

Source

10 sFTP Command Examples to Transfer Files on Remote Servers in Linux

File Transfer Protocol (FTP) was widely used protocol to transfer files or data remotely in unencrypted format which is not secure way to communicate. As we all know that File Transfer Protocol is not at all secure because all transmissions happens in clear text and the data can be readable by anyone during sniffing the packets on the network.

sftp commands

10 sftp command examples

So, basically FTP can be used in limited cases or on the networks that you trust. Over the period of time SCPand SSH addresses this security ambiguity and added an encrypted secure layer while transferring data between remote computers.

SFTP (Secure File Transfer Protocol) runs over SSH protocol on standard port 22 by default to establish a secure connection. SFTP has been integrated into many GUI tools (FileZillaWinSCPFireFTP etc.).

Security Warnings: Please don’t open SSH port (Secure SHell) globally as this would be a security breaches. You can only open for specific IP from where you are going to transfer or manage files on remote system or vice versa.

  1. 5 Best Practices to Secure and Protect SSH Server
  2. 10 Wget Command Examples in Linux

This article will guide you 10 sftp command examples to use it through interactive command-line interface.

1. How to Connect to SFTP

By default, same SSH protocol is used to authenticate and establish a SFTP connection. To start an SFTP session, enter the username and remote hostname or IP address at the command prompt. Once authentication successful, you will see a shell with an sftp> prompt.

[root@tecmint ~]# sftp tecmint@27.48.137.6

Connecting to 27.48.137.6...
tecmint@27.48.137.6's password:
sftp>

2. Getting Help

Once, you in the sftp prompt, check the available commands by typing ‘?‘ or ‘help‘ at command prompt.

sftp> ?
Available commands:
cd path                       Change remote directory to 'path'
lcd path                      Change local directory to 'path'
chgrp grp path                Change group of file 'path' to 'grp'
chmod mode path               Change permissions of file 'path' to 'mode'
chown own path                Change owner of file 'path' to 'own'
help                          Display this help text
get remote-path [local-path]  Download file
lls [ls-options [path]]       Display local directory listing
ln oldpath newpath            Symlink remote file
lmkdir path                   Create local directory
lpwd                          Print local working directory
ls [path]                     Display remote directory listing
lumask umask                  Set local umask to 'umask'
mkdir path                    Create remote directory
put local-path [remote-path]  Upload file
pwd                           Display remote working directory
exit                          Quit sftp
quit                          Quit sftp
rename oldpath newpath        Rename remote file
rmdir path                    Remove remote directory
rm path                       Delete remote file
symlink oldpath newpath       Symlink remote file
version                       Show SFTP version
!command                      Execute 'command' in local shell
!                             Escape to local shell
?                             Synonym for help

3. Check Present Working Directory

The command ‘lpwd‘ is used to check the Local present working directory, whereas ‘pwd‘ command is used to check Remote working directory.

sftp> lpwd
Local working directory: /
sftp> pwd
Remote working directory: /tecmint/
  1. lpwd – print the current directory on your system
  2. pwd – print the current directory on the ftp server

4. Listing Files

Listing files and directories in local as well as remote system.

On Remote
sftp> ls
On Local
sftp> lls

5. Upload File

Put single or multiple files in remote system.

sftp> put local.profile
Uploading local.profile to /tecmint/local.profile

6. Upload Mutiple Files

Putting multiple files on in remote system.

sftp> mput *.xls

6. Download Files

Getting single or multiple files in local system.

sftp> get SettlementReport_1-10th.xls
Fetching /tecmint/SettlementReport_1-10th.xls to SettlementReport_1-10th.xls

Get multiple files on a local system.

sftp> mget *.xls

Note: As we can see by default with get command download file in local system with same name. We can download remote file with different name specifying the name at the end. (This applies only while downloading single file).

7. Switching Directories

Switching from one directory to another directory in local and remote locations.

On Remote
sftp> cd test
sftp>
On Local
sftp> lcd Documents

8. Create Directories

Creating new directories on local and remote locations.

sftp> mkdir test
sftp> lmkdir Documents

9. Remove Directories

Remove directory or file in remote system.

sftp> rm Report.xls
sftp> rmdir sub1

Note: To remove/delete any directory from remote location, the directory must be empty.

10. Exit sFTP Shell

The ‘!‘ command drop us in local shell from where we can execute Linux commands. Type ‘exit‘ command where we can see sftp> prompt return.

sftp> !

[root@sftp ~]# exit
Shell exited with status 1
sftp>

Conclusion

The SFTP is a very useful tool for administrating servers and transferring files to and from (Local and Remote). We hope this tuts will help you to understand the usage of SFTP in some extent.

Source

How to Add a New Disk Larger Than 2TB to An Existing Linux

Have you ever tried to do the partitioning of hard disk larger than 2TB using fdisk utility and wondered why you end up getting a warning to use GPT? Yes, you got that right. We cannot partition a hard disk larger than 2TB using fdisk tool.

In such cases, we can use parted command. The major difference lies in the partitioning formats that fdiskuses DOS partitioning table format and parted uses GPT format.

TIP: You can use gdisk as well instead of parted tool.

In this article, we will show you to add a new disk larger than 2TB to an existing Linux server such as RHEL/CentOS or Debian/Ubuntu.

I am using fdisk and parted utilities to do this configuration.

First list the current partition details using fdisk command as shown.

# fdisk -l

List Linux Partition Table

List Linux Partition Table

For the purpose of this article, I am attaching a hard disk of 20GB capacity, which can be followed for disk larger than 2TB as well. Once you added a disk, verify the partition table using same fdisk command as shown.

# fdisk -l

List New Partition Table

List New Partition Table

Tip: If you are adding a physical hard disk, you may find that partitions already created. In such cases, you can use fdsik to delete the same before using parted.

# fdisk /dev/xvdd

Use d switch for the command to delete the partition and w to write the changes and quit.

Delete Linux Partition

Delete Linux Partition

Important: You need to be careful while deleting the partition. This will erase the data on the disk.

Now its time to partition a new hard disk using parted command.

# parted /dev/xvdd

Set the partition table format to GPT

(parted) mklabel gpt

Create the Primary partition and assign the disk capacity, here I am using 20GB (in your case it would be 2TB).

(parted) mkpart primary 0GB 20GB

Create Partition using Parted

Create Partition using Parted

Just for curiosity, let’s see how this new partition is listed in fdisk.

# fdisk /dev/xvdd

Verify Partition Details

Verify Partition Details

Now format and then mount the partition and add the same in /etc/fstab which controls the file systems to be mounted when the system boots.

# mkfs.ext4 /dev/xvdd1

Format Linux Partition

Format Linux Partition

Once partition has been formatted, now it’s time mount the partition under /data1.

# mount /dev/xvdd1 /data1

For permanent mounting add the entry in /etc/fstab file.

/dev/xvdd1     /data1      ext4      defaults  0   0

Important: Kernel should support GPT in order to partition using GPT format. By default RHEL/CentOS have Kernel with GPT support, but for Debian/Ubuntu you need to recompile the kernel after changing the config.

That’s it! In this article, we have shown you how to use the parted command. Share your comments and feedback with us.

Source

Dtrx – An Intelligent Archive Extraction (tar, zip, cpio, rpm, deb, rar) Tool for Linux

All of us might have faced a situation at some point of time or the other while using Linux tar.gztar.bz2tbzcommands. So many archive types, so many commands to remember… Well, not any-more, thanks to dtrx tool.

Install Dtrx in Linux

Dtrx Tool for Extract Tar files

  1. 18 Tar Commands to Create and Extract Archives in Linux
  2. How to Open, Extract and Create RAR Files in Linux

What is Dtrx?

Dtrx stands for “Do The Right Extraction“, it’s an open source and very effective command-line application for *nix systems that simplify your job of archive extraction easier.

The dtrx command is an replacement of “tar -zxvf” or “tar -xjf” commands and it provides a one single command to extract archives in a number of different formats including tarziprpmdebgem7zcpiorar and many more. It can also used to decompress files compressed with bzip2gzip etc.

By default, dtrx extract contents to a dedicated directory and also fixes permission issues (like permission denied) faced by user while extracting content to ensure that the owner can read and write all those files.

Dtrx Features

  1. Handles many archive types: It provides only one simple command to extract tar, zip, rar, gz, bz2, xz, rpm, deb, gem, self-extracting zip files and many other formats of exe files.
  2. Keeps everything organized: It will extract archives into their own dedicated directories.
  3. Sane permissions: It also make sure, user can read and write all those files after extraction, keeping permission intact.
  4. Recursive extraction: It can find archives inside the archive and extract those too.

How to Install Dtrx in Linux

The dtrx tool is by default included in Ubuntu repositories, all you’ve to do is simple do a apt-get to install in on your system.

On Debian/Ubuntu/Linux Mint

$ sudo apt-get install dtrx

On RHEL/CentOS/Fedora

On Red Hat based systems, dtrx is not available via default repositories, you need to download a dtrx script and install the program system-wide using below commands as root user.

# wget http://brettcsmith.org/2007/dtrx/dtrx-7.1.tar.gz
# tar -xvf dtrx-7.1.tar.gz 
# cd dtrx-7.1
# python setup.py install --prefix=/usr/local
Sample Output
running install
running build
running build_scripts
creating build
creating build/scripts-2.6
copying and adjusting scripts/dtrx -> build/scripts-2.6
changing mode of build/scripts-2.6/dtrx from 644 to 755
running install_scripts
copying build/scripts-2.6/dtrx -> /usr/local/bin
changing mode of /usr/local/bin/dtrx to 755
running install_egg_info
Creating /usr/local/lib/python2.6/site-packages/
Writing /usr/local/lib/python2.6/site-packages/dtrx-7.1-py2.6.egg-info

How to Use dtrx Command

The dtrx command is sort of like the one ring to rule them all in the Lord of The Rings. Instead of having to remember syntax for each archive, all you have to remember is dtrx command.

1. Extracting Single Archive

For example, I want to extract a archive file called “tecmint27-12-2013.gz“, I only execute dtrx command without using any flags.

[root@tecmint]# dtrx tecmint27-12-2013.gz

Other than simplifying the extraction, it has a bunch of other options like extracting the file to a folder and recursively extracting all other archives inside a given archive.

2. Extracting Multiple Archives

Consider you’ve a file “dtrAll.zip“, consisting of dtr1.zipdtr2.zip and dtr3.zip each consisting of dtr1,dtr2 and dtr3 respectively. Instead of having to manually first extract the dtrAll zip and then extracting each one of thedtr1dtr2 and dtr3 you can directly extract it in respective folders by using dtrx and by selecting option “a“, it extracts all the zip files recursively.

[root@tecmint]# dtrx dtrAll.zip
Sample Output
dtrx: WARNING: extracting /root/dtrAll.zip to dtrAll.1
dtrAll.zip contains 3 other archive file(s), out of 3 file(s) total.
You can:
 * _A_lways extract included archives during this session
 * extract included archives this _O_nce
 * choose _N_ot to extract included archives this once
 * ne_V_er extract included archives during this session
 * _L_ist included archives
What do you want to do?  (a/o/N/v/l) a

After, extraction, the contents of the extracted directory can be verified using ls command.

[root@tecmint]# cd dtrAll
[root@tecmint]# ls 

dtr1  dtr1.zip  dtr2  dtr2.zip  dtr3  dtr3.zip

3. Extracting Specific Archive

Let’s say you want to extract the first archive and not archives inside it. By selecting N, it only extracts the given archive and not other archives inside it.

[root@tecmint]# dtrx dtrAll.zip
Sample Output
dtrx: WARNING: extracting /root/dtrAll.zip to dtrAll.1
dtrAll.zip contains 3 other archive file(s), out of 3 file(s) total.
You can:
 * _A_lways extract included archives during this session
 * extract included archives this _O_nce
 * choose _N_ot to extract included archives this once
 * ne_V_er extract included archives during this session
 * _L_ist included archives
What do you want to do?  (a/o/N/v/l) N

The contents of extracted directory can be verified using ls command as shown.

[root@tecmint]# cd dtrAll
[root@tecmint]# ls

dtr1.zip dtr2.zip dtr3.zip

4. Extract Each Layer of Archive

To extract each layer of archive inside archive on a case by case basis i.e., if you want to extract 2nd layer of archives but not the 3rd layer, you can use the “o” option.

Consider you’ve a zip file “dtrNewAll.zip“, which has “dtrAll.zip” and “dtrNew” as it’s contents. Now if you want to extract the contents of “dtrNewAll” and “dtrAll” as well but not of dtr1.zipdtr2.zip and dtr3.zip, you can use “o” and “n” options as shown below.

# dtrx dtrNewAll.zip
Sample Output
dtrNewAll.zip contains 1 other archive file(s), out of 2 file(s) total.
You can:
 * _A_lways extract included archives during this session
 * extract included archives this _O_nce
 * choose _N_ot to extract included archives this once
 * ne_V_er extract included archives during this session
 * _L_ist included archives
What do you want to do?  (a/o/N/v/l) o
dtrAll.zip contains 3 other archive file(s), out of 3 file(s) total.
You can:
 * _A_lways extract included archives during this session
 * extract included archives this _O_nce
 * choose _N_ot to extract included archives this once
 * ne_V_er extract included archives during this session
 * _L_ist included archives
What do you want to do?  (a/o/N/v/l) n

The contents of extracted directory can be verified using ls command as shown.

[root@tecmint]# cd dtrNewAll
[root@tecmint]# ls

dtrAll  dtrAll.zip  dtrNew
[root@tecmint]# cd dtrAll
[root@tecmint]# ls

dtr1.zip dtr2.zip dtr3.zip

We first select the “o” option which means that all archives inside dtrNewAll will be extracted. Later we select the “n” option for dtrAll.zip which means that the archives inside it dtr1.zip , dtr2.zip and dtr3.zip will not be extracted.

5. Extract meta-data from .deb, .rpm and .gem files

The “-m” option extract the meta-data from .deb.rpm and .gem archives, instead of their normal contents. Here is an example of the command.

[root@tecmint]# dtrx -m openfire_3.8.2_all.deb 
[root@tecmint]# dtrx -m openfire-3.8.2-1.i386.rpm
[root@tecmint]# ls

conffiles  control  md5sums  postinst  postrm  prerm

There are lot more dtrx options to explore, just run the “dtrx –help” to list the available options.

[root@tecmint]# dtrx  --help

Usage: dtrx [options] archive [archive2 ...]

Intelligent archive extractor

Options:
  --version             	show program's version number and exit
  -h, --help            	show this help message and exit
  -l, -t, --list, --table      	list contents of archives on standard output
  -m, --metadata        	extract metadata from a .deb/.gem
  -r, --recursive       	extract archives contained in the ones listed
  -n, --noninteractive  	don't ask how to handle special cases
  -o, --overwrite       	overwrite any existing target output
  -f, --flat, --no-directory    extract everything to the current directory
  -v, --verbose         	be verbose/print debugging information
  -q, --quiet           	suppress warning/error messages

Reference Links

dtrx Homepage

I think you must give a try to dtrx, because it’s the only powerful command line tool that gives a single command to decompress any format of archive files.

Source

How to Create Multiple User Accounts in Linux

The two utilities for adding or creating user accounts in Unix/Linux systems are adduser and useradd. These commands are designed to add a single user account in the system at a time. What if you have multiple users accounts to be created? That’s when you need a program such as newusers.

Newusers is a useful command line utility used to update and create new user accounts at a single time. It is intended to be used in IT environments with large systems where a system administrator needed to update or create multiple user accounts in batch. It reads information from stdin (by default) or a file to update a set of existing user accounts or to create new users.

In this article, we will explain how to create multiple user accounts in batch mode using Newusers utility in Linux systems.

To create users in a batch, you can provide their information in a file in the following format, same as the standard password file:

pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell
/bash

Create Multiple User Accounts in Linux

Create Multiple User Accounts in Linux

Save the file and set the required permissions on it.

$ sudo chmod 0600 users.txt 

Now run the newusers command with the input file to add the above user accounts at once.

$ sudo newusers users.txt

First, newusers program tries to create or update the specified accounts, and then write these changes to the user or group databases. In case of any errors except in the final writes to the databases, no changes are committed to the databases. This is simply how the newusers command works.

If the previous command is successful, check the /etc/passwd and /etc/groups files to confirm that the user accounts have been added as shown.

$ cat /etc/passwd | grep -E "ravi|tecmint"

Verify User Accounts in Linux

Verify User Accounts in Linux

For more information, see the newuser man page.

$ man newuser 

You might also like to check these following related articles.

  1. 3 Ways to Change a Users Default Shell in Linux
  2. How to Create a Shared Directory for All Users in Linux
  3. Whowatch – Monitor Linux Users and Processes in Real Time
  4. How to Send a Message to Logged Users in Linux

In this article, we’ve explained how to create multiple users in Linux using newusers program. Use the feedback form below to ask any questions or share your comments with us. If you know of any similar utilities out there, let us know as well.

Source

How to Run Shell Scripts with Sudo Command in Linux

sudo is a powerful command line tool that enables a “permitted user” to run a command as another user (the superuser by default), as defined by a security policy. On most if not all Linux systems, the security policy is driven by the /etc/sudoers file.

Read Also10 Useful Sudoers Configurations for Setting ‘sudo’ in Linux

Therefore, to run a shell script or program as root, you need to use sudo command. However, sudo only recognizes and runs commands that exist in directories specified in the secure_path in the /etc/sudoers, unless a command is present in the secure_path, you’ll counter an error such as the one below.

This will happen even if the script exists in a directory in the PATH environmental variable, because when a user invokes sudoPATH is replaced with secure_path.

$ echo  $PATH
$ ls  -l
$ sudo proconport.sh 80

Sudo Error While Running Script

Sudo Error While Running Script

In the above scenario, the directory /home/aaronkilik/bin is in the PATH environment variable and we are trying to run the script /home/aaronkilik/bin/proconport.sh (finds process listening on a port) with root privileges.

Then we encountered the error “sudo: proconport.sh: command not found”, since /home/aaronkilik/bin is not in the sudo secure_path as shown in next screenshot.

Check Sudo Secure Path

Check Sudo Secure Path

To fix this, we need to add the directory containing our scripts in the sudo secure_path by using the visudo command by editing /etc/sudoers file as follows.

$ sudo visudo

Attention: This method has serious security implications especially on servers running on the Internet. This way, we risk exposing our systems to various attacks, because an attacker who manages to gain access to an unsecure (without superuser privileges) directory that has been added to secure_path, can run a malicious script/program with sudo command.

For security reason, do check out the following article from the sudo website explains a vulnerability relating to secure_path: https://www.sudo.ws/sudo/alerts/secure_path.html

Preferably, we can provide the absolute path to a script while running it with sudo:

$ sudo ./proconport.sh 80

Run Script Using Sudo Command

Run Script Using Sudo Command

That’s It! You may follow list of articles about sudo command:

  1. How to Run ‘sudo’ Command Without Entering a Password in Linux
  2. How to Keep ‘sudo’ Password Timeout Session Longer in Linux
  3. How to Fix “Username is not in the sudoers file. This incident will be reported” in Ubuntu
  4. Let Sudo Insult You When You Enter Incorrect Password

Source

How to Check Remote Ports are Reachable Using ‘nc’ Command

port is a logical entity which acts as a endpoint of communication associated with an application or process on an Linux operating system. It is useful to know which ports are open and running services on a target machine before using them.

We can easily list open ports in Linux on a local machine using the netstat or several other Linux commands such NMAP.

In this guide, we will show you how to determine if ports on a remote host are reachable/open using simple netcat (in short nc) command.

netcat (or nc in short) is a powerful and easy-to-use utility that can be employed for just about anything in Linux in relation to TCP, UDP, or UNIX-domain sockets.

# yum install nc                  [On CentOS/RHEL]
# dnf install nc                  [On Fedora 22+]
$ sudo apt-get install netcat     [On Debian/Ubuntu]

We can use it to: open TCP connections, listen on arbitrary TCP and UDP ports, send UDP packets, do port scanning under both IPv4 and IPv6 and beyond.

Using netcat, you can check if a single or multiple or a range of open ports as follows. The command below will help us see if the port 22 is open on the host 192.168.56.10:

$ nc -zv 192.168.1.15 22

In the command above, the flag:

  1. -z – sets nc to simply scan for listening daemons, without actually sending any data to them.
  2. -v – enables verbose mode.

The next command will check if ports 8022 and 21 are open on the remote host 192.168.5.10 (we can use the hostname as well):
nc -zv 192.168.56.10 80 22 21

It is also possible to specify a range of ports to be scanned:’

$ nc -zv 192.168.56.10 20-80

For more examples and usage of netcat command, read through our articles as follows.

  1. Transfer Files Between Linux Servers Using netcat Command
  2. Linux Network Configuration and Troubleshooting Commands

That’s all. In this article, we explained how to check if ports on a remote host are reachable/open using simple netcat commands.

Source

4 Ways to Find Server Public IP Address in Linux Terminal

In computer networking, an IP (Internet Protocol) address is a numerical identifier assigned permanently or temporarily to every device connected to a network that uses the Internet Protocol for communication. Its two major functions are to identify a network or host on a network and also serve for location addressing.

Read AlsoHow to Set Static IP Address and Configure Network in Linux

There are currently two versions of IP addresses: IPv4 and IPv6, which can either be private (viewable within an internal network) or public (can be seen by other machines on the Internet).

Additionally, a host can be assigned a static or dynamic IP address depending on the network configurations. In this article, we will show you 4 ways to find your Linux machine or server public IP address from the terminal in Linux.

1. Using dig Utility

dig (domain information groper) is a simple command line utility for probing DNS name servers. To find your public IP addresses, use the opendns.com resolver as in the command below:

$ dig +short myip.opendns.com @resolver1.opendns.com

120.88.41.175

2. Using host Utility

host command is an easy-to-use command line utility for carrying out DNS lookups. The command below will help to display your systems public IP address.

$ host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'

120.88.41.175

Important: The next two methods employ third party websites to display your IP address on the command line as described below.

3. Using wget Command Line Downloader

wget is a powerful command line downloader that supports various protocols like HTTP, HTTPS, FTP and many more. You can use it with third party websites to view your public IP address as follows:

$ wget -qO- http://ipecho.net/plain | xargs echo
$ wget -qO - icanhazip.com

120.88.41.175

4. Using cURL Command Line Downloader

curl is a popular command line tool for uploading or downloading files from a server using any of the supported protocols (HTTP, HTTPS, FILE, FTP, FTPS and others). The following commands displays your public IP address.

$ curl ifconfig.co
$ curl ifconfig.me
$ curl icanhazip.com

120.88.41.175

That’s It! You may find these following articles useful to read.

  1. 5 Linux Command Line Based Tools for Downloading Files and Browsing Websites
  2. 11 Ways to Find User Account Info and Login Details in Linux
  3. 7 Ways to Determine the File System Type in Linux (Ext2, Ext3 or Ext4)

That’s all! If you have any questions or other methods to share in relation to this topic, use the feedback form below to write back to us.

Source

A – Z LINUX COMMANDS (136) – OVERVIEW WITH EXAMPLES

Linux Commands Cheat Sheet

adduser/addgroup agetty alias anacron
apropos apt apt-get aptitude
arch arp at atq
atrm awk batch basename
bc bg bzip cal
cat chgrp chmod chown
cksum clear cmp comm
cp date dd df
diff dir dmidecode du
echo eject env exit
expr factor find free
grep groups gzip gunzip
head history hostname hostnamectl
hwclock hwinfo id ifconfig
ionice iostat ip iptables
iw iwlist kill killall
kmod last ln locate
login ls lshw lscpu
lsof lsusb man mdsum
mkdir more mv nano
nc/netcat netstat nice nmap
nproc openssl passwd pidof
ping ps pstree pwd
rdiff-backup reboot rename rm
rmdir scp shutdown sleep
sort split ssh stat
su sudo sum tac
tail talk tar tee
time top touch tr
uname uniq uptime users
vim/vi w wall watch
wc wget whatis which
who whereis xargs yes
youtube-dl zcmp/zdiff zip zz

There are a countless number of commands in Linux. We are bound to use a number of them on a daily routine or numerous times to perform common tasks than others. It is important to note that certain commands are “distro-based” – they can only be found in specific distros. While others are generic Unix/Linux commands that you’ll find in all if not most mainstream distros.

In this article, we will introduce you a list of most frequently used Linux commands with their examples for easy learning. You can find the actual description of each Linux command in their manual page which you can access like this:

$ man command-name

adduser/addgroup Command

The adduser and addgroup commands are used to add a user and group to the system respectively according to the default configuration specified in /etc/adduser.conf file.

$ sudo adduser tecmint

For more adduser and addgroup commands: 15 Practical Examples on adduser Command in Linux

agetty Command

agetty is a program which manages physical or virtual terminals and is invoked by init. Once it detects a connection, it opens a tty port, asks for a user’s login name and calls up the /bin/login command. Agetty is a substitute of Linux getty:

$ agetty -L 9600 ttyS1 vt100

alias Command

alias is a useful shell built-in command for creating aliases (shortcut) to a Linux command on a system. It is helpful for creating new/custom commands from existing Shell/Linux commands (including options):

$ alias home='cd /home/tecmint/public_html'

The above command will create an alias called home for /home/tecmint/public_html directory, so whenever you type home in the terminal prompt, it will put you in the /home/tecmint/public_html directory.

anacron Command

anacron is a Linux facility used to run commands periodically with a frequency defined in days, weeks and months.

Unlike its sister cron; it assumes that a system will not run continuously, therefore if a scheduled job is due when the system is off, it’s run once the machine is powered on.

For more information about anacron and cron read: Cron Vs Anacron: How to Schedule Jobs Using Anacron on Linux

apropos Command

apropos command is used to search and display a short man page description of a command/program as follows.

$ apropos adduser

apt Command

apt tool is a relatively new higher-level package manager for Debian/Ubuntu systems:

$ sudo apt update

For more apt usage read: 15 Useful Examples on APT Command

apt-get Command

apt-get is a powerful and free front-end package manager for Debian/Ubuntu systems. It is used to install new software packages, remove available software packages, upgrade existing software packages as well as upgrade entire operating system.

$ sudo apt-get update

For more apt-get usage, read: 25 Useful Commands of APT-GET for Package Management

aptitude Command

aptitude is a powerful text-based interface to the Debian GNU/Linux package management system. Like apt-get and apt; it can be used to install, remove or upgrade software packages on a system.

$ sudo aptitude update

For more usage on aptitude, read: Learn Debian Package Management with Aptitude Command

arch Command

arch is a simple command for displaying machine architecture or hardware name (similar to uname -m):

$ arch 

arp Command

ARP (Address Resolution Protocol) is a protocol that maps IP network addresses of a network neighbor with the hardware (MAC) addresses in an IPv4 network.

You can use it as below to find all alive hosts on a network:

$ sudo arp-scan --interface=enp2s0 --localnet  

at Command

at command is used to schedule tasks to run in a future time. It’s an alternative to cron and anacron, however, it runs a task once at a given future time without editing any config files:

For example, to shutdown the system at 23:55 today, run:

$ sudo echo "shutdown -h now" | at -m 23:55

atq Command

atq command is used to view jobs in at command queue:

$ atq

atrm Command

atrm command is used to remove/deletes jobs (identified by their job number) from at command queue:

$ atrm 2

For more usage about at command, read: How to Use ‘at’ Command to Schedule a Task in Linux

awk Command

Awk is a powerful programming language created for text processing and generally used as a data extraction and reporting tool.

$ awk '//{print}'/etc/hosts

For more Awk concepts (basic and advance) with simple and easy-to-understand, we’ve created a book that contains 13 chapters with a total of 41 pages, which covers all Awk basic and advance usage with practical examples: Awk Getting Started Guide for Beginners

batch Command

batch is also used to schedule tasks to run a future time, similar to the at command.

basename Command

basename command helps to print the name of a file stripping of directories in the absolute path:

$ basename bin/findhosts.sh

bc Command

bc is a simple yet powerful and arbitrary precision CLI calculator language which can be used like this:

$ echo 20.05 + 15.00 | bc

bg Command

bg is a command used to send a process to the background.

$ tar -czf home.tar.gz .
$ bg 
$ jobs

To learn more about bg command, read: Start Linux Command in Background and Detach Process in Terminal

bzip2 Command

bzip2 command is used to compress or decompress file(s).

$ bzip2 -z filename      #Compress
$ bzip2 -d filename.bz2  #Decompress

To learn more examples on bzip2, read: How to Compress and Decompress a .bz2 File in Linux

cal Command

The cal command print a calendar on the standard output.

$ cal

cat Command

cat command is used to view contents of a file or concatenate files, or data provided on standard input, and display it on the standard output.

$ cat file.txt

To learn more about cat command, read: 13 Useful Cat Command Examples on Linux

chgrp Command

chgrp command is used to change the group ownership of a file. Provide the new group name as its first argument and the name of file as the second argument like this:

$ chgrp tecmint users.txt

chmod Command

chmod command is used to change/update file access permissions like this.

$ chmod +x sysinfo.sh

chown Command

chown command changes/updates the user and group ownership of a file/directory like this.

$ chmod -R www-data:www-data /var/www/html

To learn more about chgrp, chmod and chwon commands, read: Managing Users & Groups, File Permissions & Attributes in Linux

cksum Command

cksum command is used to display the CRC checksum and byte count of an input file.

$ cksum README.txt

clear Command

clear command lets you clear the terminal screen, simply type.

$ clear

cmp Command

cmp performs a byte-by-byte comparison of two files like this.

$ cmp file1 file2

comm Command

comm command is used to compare two sorted files line-by-line as shown below.

$ comm file1 file2

cp Command

cp command is used for copying files and directories from one location to another.

$ cp /home/tecmint/file1 /home/tecmint/Personal/

For more information on how to copy files in Linux, read:

  1. How to Copy a File to Multiple Directories in Linux
  2. Advanced Copy Command – Shows Progress Bar While Copying Large Files/Folders in Linux
  3. Progress – A Tiny Tool to Monitor Progress for (cp, mv, dd, tar, etc.) Commands in Linux

date Command

date command displays/sets the system date and time like this.

$ date
$ date --set="8 JUN 2017 13:00:00"

To learn more about how to set date in Linux, read: How to Set System Date in Linux

dd Command

dd command is used for copying files, converting and formatting according to flags provided on the command line. It can strip headers, extracting parts of binary files and so on.

The example below shows creating a boot-able USB device:

$ dd if=/home/tecmint/kali-linux-1.0.4-i386.iso of=/dev/sdc1 bs=512M; sync

df Command

df command is used to show file system disk space usage as follows.

$ df -h

For more usage on df command, read: 12 Useful ‘df’ Command Examples to Check Disk Space

diff Command

diff command is used to compare two files line by line. It can also be used to find the difference between two directories in Linux like this:

$ diff file1 file2

Some useful diff tools for Linux: 8 Best File Comparison and Difference (Diff) Tools for Linux

dir Command

dir command works like Linux ls command, it lists the contents of a directory.

$ dir

dmidecode Command

dmidecode command is a tool for retrieving hardware information of any Linux system. It dumps a computer’s DMI (a.k.a SMBIOS) table contents in a human-readable format for easy retrieval.

To view your system hardware info, you can type:

$ sudo dmidecode --type system

Some useful tools to find out Linux system hardware info: 10 Useful Commands to Get Linux Hardware Information

du Command

du command is used to show disk space usage of files present in a directory as well as its sub-directories as follows.

$ du /home/aaronkilik

echo Command

echo command prints a text of line provided to it.

$ echo “This is TecMint - Linux How Tos”

eject Command

eject command is used to eject removable media such as DVD/CD ROM or floppy disk from the system.

$ eject /dev/cdrom
$ eject /mnt/cdrom/
$ eject /dev/sda

env Command

env command lists all the current environment variables and used to set them as well.

$ env

Learn more on How to Set and Unset Environment Variables in Linux

exit Command

exit command is used to exit a shell like so.

$ exit

expr Command

expr command is used to calculate an expression as shown below.

$ expr 20 + 30

factor Command

factor command is used to show the prime factors of a number.

$ factor 10

find Command

find command lets you search for files in a directory as well as its sub-directories. It searches for files by attributes such as permissions, users, groups, file type, date, size and other possible criteria.

$ find /home/tecmint/ -name tecmint.txt

Learn more on how to find files in Linux:

  1. How to Use ‘find’ Command to Search for Multiple Filenames (Extensions)
  2. How to Find Number of Files in a Directory and Subdirectories
  3. How to Find Today’s Modified Files in Linux
  4. How to Find and Sort Files Based on Modified Date and Time
  5. How to Find Top Directories and Files (Disk Space) in Linux

free Command

free command shows the system memory usage (free, used, swapped, cached, etc.) in the system including swap space. Use the -h option to display output in human friendly format.

$ free -h 

Learn more on how to find memory usage in Linux.

  1. Find Top Running Processes by Highest Memory and CPU Usage in Linux
  2. Smem – Reports Memory Consumption Per-Process and Per-User Basis in Linux
  3. How to Clear RAM Memory Cache, Buffer and Swap Space on Linux

grep Command

grep command searches for a specified pattern in a file (or files) and displays in output lines containing that pattern as follows.

$ grep ‘tecmint’ domain-list.txt

Learn more about grep command usage in Linux.

  1. What’s Difference Between Grep, Egrep and Fgrep in Linux?
  2. 12 Basic Linux ‘Grep’ Command Examples in Linux
  3. 11 Advanced Linux ‘Grep’ Commands in Linux

groups Command

groups command displays all the names of groups a user is a part of like this.

$ groups
$ groups tecmint

gzip Command

Gzip helps to compress a file, replaces it with one having a .gz extension as shown below:

$ gzip passwds.txt
$ cat file1 file2 | gzip > foo.gz

gunzip Command

gunzip expands or restores files compressed with gzip command like this.

$ gunzip foo.gz

head Command

head command is used to show first lines (10 lines by default) of the specified file or stdin to the screen:

# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head 

history Command

history command is used to show previously used commands or to get info about command executed by a user.

$ history

Learn more about Linux history command.

  1. The Power of Linux “History Command” in Bash Shell
  2. Set Date and Time for Each Command You Execute in Bash History
  3. How to Use ‘Yum History’ to Find Out Installed/Removed Packages Info

hostname Command

hostname command is used to print or set system hostname in Linux.

$ hostname
$ hostname NEW_HOSTNAME

hostnamectl Command

hostnamectl command controls the system hostname under systemd. It is used to print or modify the system hostname and any related settings:

$ hostnamectl
$ sudo hostnamectl set-hostname NEW_HOSTNAME

hwclock

hwclock is a tool for managing the system hardware clock; read or set the hardware clock (RTC).

$ sudo hwclock
$ sudo hwclock --set --date 8/06/2017

hwinfo Command

hwinfo is used to probe for the hardware present in a Linux system like this.

$ hwinfo

Learn more about how to get Linux hardware info.

  1. I-Nex – An Advanced Tool to Collect System/Hardware Information in Linux
  2. 9 Useful Tools to Get System Information in Linux

id Command

id command shows user and group information for the current user or specified username as shown below.

$ id tecmint

ifconfig Command

ifconfig command is used to configure a Linux systems network interfaces. It is used to configure, view and control network interfaces.

$ ifconfig
$ sudo ifconfig eth0 up
$ sudo ifconfig eth0 down
$ sudo ifconfig eth0 172.16.25.125

ionice Command

ionice command is used to set or view process I/O scheduling class and priority of the specified process.

If invoked without any options, it will query the current I/O scheduling class and priority for that process:

$ ionice -c 3 rm /var/logs/syslog

To understand how it works, read this article: How to Delete HUGE (100-200GB) Files in Linux

iostat Command

iostat is used to show CPU and input/output statistics for devices and partitions. It produces useful reports for updating system configurations to help balance the input/output load between physical disks.

$ iostat 

ip Command

ip command is used to display or manage routing, devices, policy routing and tunnels. It also works as a replacement for well known ifconfig command.

This command will assign an IP address to a specific interface (eth1 in this case).

$ sudo ip addr add 192.168.56.10 dev eth1

iptables Command

iptables is a terminal based firewall for managing incoming and outgoing traffic via a set of configurable table rules.

The command below is used to check existing rules on a system (using it may require root privileges).

$ sudo iptables -L -n -v

Learn more about iptables firewall in Linux.

  1. How to Start/Stop and Enable/Disable FirewallD and Iptables in Linux
  2. Basic Guide on IPTables (Linux Firewall) Tips / Commands
  3. Nishita Agarwal Shares Her Interview Experience on Linux ‘iptables’ Firewall

iw Command

iw command is used to manage wireless devices and their configuration.

$ iw list 

iwlist Command

iwlist command displays detailed wireless information from a wireless interface. The command below enables you to get detailed information about the wlp1s0interface.

$ iwlist wlp1s0 scanning

kill Command

kill command is used to kill a process using its PID by sending a signal to it (default signal for kill is TERM).

$ kill -p 2300
$ kill -SIGTERM -p 2300

killall Command

killall command is used to kill a process by its name.

$ killall firefox

Learn more about kill and killall command in Linux.

  1. How to Find and Kill Running Processes in Linux
  2. A Guide to Kill, Pkill and Killall Commands to Terminate a Process
  3. How to Kill Processes/Unresponsive Applications Using ‘xkill’ Command

kmod Command

kmod command is used to manage Linux kernel modules. To list all currently loaded modules, type.

$ kmod list

last Command

last command display a listing of last logged in users.

$ last 

ln Command

ln command is used to create a soft link between files using the -s flag like this.

$ ln -s /usr/bin/lscpu cpuinfo

locate Command

locate command is used to find a file by name. The locate utility works better and faster than it’s find counterpart.

The command below will search for a file by its exact name (not *name*):

$ locate -b '\domain-list.txt'

login Command

login command is used to create a new session with the system. You’ll be asked to provide a username and a password to login as below.

$ sudo login

ls Command

ls command is used to list contents of a directory. It works more or less like dir command.

The -l option enables long listing format like this.

$ ls -l file1

To know more about ls command, read our guides.

  1. 15 Basic ‘ls’ Command Examples in Linux
  2. 7 Quirky ‘ls’ Command Tricks Every Linux User Should Know
  3. How to Sort Output of ‘ls’ Command By Last Modified Date and Time
  4. 15 Interview Questions on Linux “ls” Command – Part 1
  5. 10 Useful ‘ls’ Command Interview Questions – Part 2

lshw Command

lshw command is a minimal tool to get detailed information on the hardware configuration of the machine, invoke it with superuser privileges to get a comprehensive information.

$ sudo lshw  

lscpu Command

lscpu command displays system’s CPU architecture information (such as number of CPUs, threads, cores, sockets, and more).

$ lscpu

lsof Command

lsof command displays information related to files opened by processes. Files can be of any type, including regular files, directories, block special files, character special files, executing text reference, libraries, and stream/network files.

To view files opened by a specific user’s processes, type the command below.

$ lsof -u tecmint

lsusb Command

lsusb command shows information about USB buses in the system and the devices connected to them like this.

$ lsusb 

man Command

man command is used to view the on-line reference manual pages for commands/programs like so.

$ man du
$ man df

md5sum Command

md5sum command is used to compute and print the MD5 message digest of a file. If run without arguments, debsums checks every file on your system against the stock md5sum files:

$ sudo debsums

mkdir Command

mkdir command is used to create single or more directories, if they do not already exist (this can be overridden with the -p option).

$ mkdir tecmint-files
OR 
$ mkdir -p tecmint-files

more Command

more command enables you to view through relatively lengthy text files one screenful at a time.

$ more file.txt

Check difference between more and less command and Learn Why ‘less’ is Faster Than ‘more’ Command

mv Command

mv command is used to rename files or directories. It also moves a file or directory to another location in the directory structure.

$ mv test.sh sysinfo.sh 

nano Command

nano is a popular small, free and friendly text editor for Linux; a clone of Pico, the default editor included in the non-free Pine package.

To open a file using nano, type:

$ nano file.txt

nc/netcat Command

nc (or netcat) is used for performing any operation relating to TCP, UDP, or UNIX-domain sockets. It can handle both IPv4 and IPv6 for opening TCP connections, sending UDP packets, listening on arbitrary TCP and UDP ports, performing port scanning.

The command below will help us see if the port 22 is open on the host 192.168.56.5.

$ nc -zv 192.168.1.5 22

Learn more examples and usage on nc command.

  1. How to Check Remote Ports are Reachable Using ‘nc’ Command
  2. How to Transfer Files Between Computers Using ‘nc’ Command

netstat Command

netstat command displays useful information concerning the Linux networking subsystem (network connections, routing tables, interface statistics, masquerade connections, and multicast memberships).

This command will display all open ports on the local system:

$ netstat -a | more

nice Command

nice command is used to show or change the nice value of a running program. It runs specified command with an adjusted niceness. When run without any command specified, it prints the current niceness.

The following command starts the process “tar command” setting the “nice” value to 12.

$ nice -12 tar -czf backup.tar.bz2 /home/*

nmap Command

nmap is a popular and powerful open source tool for network scanning and security auditing. It was intended to quickly scan large networks, but it also works fine against single hosts.

The command below will probe open ports on all live hosts on the specified network.

$ nmap -sV 192.168.56.0/24

nproc Command

nproc command shows the number of processing units present to the current process. It’s output may be less than the number of online processors on a system.

$ nproc  

openssl Command

The openssl is a command line tool for using the different cryptography operations of OpenSSL’s crypto library from the shell. The command below will create an archive of all files in the current directory and encrypt the contents of the archive file:

$ tar -czf - * | openssl enc -e -aes256 -out backup.tar.gz

passwd Command

passwd command is used to create/update passwords for user accounts, it can also change the account or associated password validity period. Note that normal system users may only change the password of their own account, while root may modify the password for any account.

$ passwd tecmint

pidof Command

pidof displays the process ID of a running program/command.

$ pidof init
$ pidof cinnamon

ping Command

ping command is used to determine connectivity between hosts on a network (or the Internet):

$ ping google.com

ps Command

ps shows useful information about active processes running on a system. The example below shows the top running processes by highest memory and CPU usage.

# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

pstree Command

pstree displays running processes as a tree which is rooted at either PID or init if PID is omitted.

$ pstree

pwd Command

pwd command displays the name of current/working directory as below.

$ pwd

rdiff-backup Command

rdiff-backup is a powerful local/remote incremental backup script written in Python. It works on any POSIX operating system such as Linux, Mac OS X.

Note that for remote backups, you must install the same version of rdiff-backup on both the local and remote machines. Below is an example of a local backup command:

$ sudo rdiff-backup /etc /media/tecmint/Backup/server_etc.backup

reboot Command

reboot command may be used to halt, power-off or reboot a system as follows.

$ reboot

rename Command

rename command is used to rename many files at once. If you’ve a collection of files with “.html” extension and you want to rename all of them with “.php” extension, you can type the command below.

$ rename 's/\.html$/\.php/' *.html

rm command

rm command is used to remove files or directories as shown below.

$ rm file1
$ rm -rf my-files

rmdir Command

rmdir command helps to delete/remove empty directories as follows.

$ rmdir /backup/all

scp Command

scp command enables you to securely copy files between hosts on a network, for example.

$ scp ~/names.txt root@192.168.56.10:/root/names.txt

shutdown Command

shutdown command schedules a time for the system to be powered down. It may be used to halt, power-off or reboot the machine like this.

$ shutdown --poweroff

Learn how to show a Custom Message to Users Before Linux Server Shutdown.

sleep Command

sleep command is used to delay or pause (specifically execution of a command) for a specified amount of time.

$ check.sh; sleep 5; sudo apt update

sort Command

sort command is used to sort lines of text in the specified file(s) or from stdin as shown below

$ cat words.txt

Learn more examples of sort command in Linux.

  1. 7 Interesting Linux ‘sort’ Command Examples
  2. How to Sort Output of ‘ls’ Command By Last Modified Date and Time
  3. How to Find and Sort Files Based on Modification Date and Time

split Command

split as the name suggests, is used to split a large file into small parts.

$ tar -cvjf backup.tar.bz2 /home/tecmint/Documents/* 

ssh Command

ssh (SSH client) is an application for remotely accessing and running commands on a remote machine. It is designed to offer a secure encrypted communications between two untrusted hosts over an insecure network such as the Internet.

$ ssh tecmint@192.168.56.10

Learn more about ssh command and how to use it on Linux.

  1. 5 Best Practices to Secure and Protect SSH Server
  2. Configure “No Password SSH Keys Authentication” with PuTTY on Linux
  3. SSH Passwordless Login Using SSH Keygen in 5 Easy Steps
  4. Restrict SSH User Access to Certain Directory Using Chrooted Jail

stat Command

stat is used to show a file or file system status like this (-f is used to specify a filesystem).

$ stat file1

su Command

su command is used to switch to another user ID or become root during a login session. Note that when su is invoked without a username, it defaults to becoming root.

$ su 
$ su tecmint

sudo Command

sudo command allows a permitted system user to run a command as root or another user, as defined by the security policy such as sudoers.

In this case, the real (not effective) user ID of the user running sudo is used to determine the user name with which to query the security policy.

$ sudo apt update
$ sudo useradd tecmint
$ sudo passwd tecmint

Learn more about sudo command and how to use it on Linux.

  1. 10 Useful Sudoers Configurations for Setting ‘sudo’ in Linux
  2. How to Run ‘sudo’ Command Without Entering a Password in Linux
  3. How to Keep ‘sudo’ Password Timeout Session Longer in Linux

sum Command

sum command is used to show the checksum and block counts for each each specified file on the command line.

$ sum output file.txt 

tac Command

tac command concatenates and displays files in reverse. It simply prints each file to standard output, showing last line first.

$tac file.txt

tail Command

tail command is used to display the last lines (10 lines by default) of each file to standard output.

If there more than one file, precede each with a header giving the file name. Use it as follow (specify more lines to display using -n option).

$ tail long-file
OR
$ tail -n 15 long-file

talk Command

talk command is used to talk to another system/network user. To talk to a user on the same machine, use their login name, however, to talk to a user on another machine use ‘user@host’.

$ talk person [ttyname]
OR
$ talk‘user@host’ [ttyname]

tar Command

tar command is a most powerful utility for archiving files in Linux.

$ tar -czf home.tar.gz .

Learn more about tar command and its usage on Linux.

  1. 18 Tar Command Examples in Linux
  2. How to Split Large ‘tar’ Archive into Multiple Files of Certain Size
  3. How to Extract Tar Files to Specific or Different Directory in Linux

tee Command

tee command is used to read from standard input and prints to standard output and files as shown below.

$ echo "Testing how tee command works" | tee file1 

time Command

time command runs programs and summarizes system resource usage.

$ time wc /etc/hosts

top Command

top program displays all processes on a Linux system in regards to memory and CPU usage and provides a dynamic real-time view of a running system.

$ top

touch Command

touch command changes file timestamps, it can also be used to create a file as follows.

$ touch file.txt

tr Command

tr command is a useful utility used to translate (change) or delete characters from stdin, and write the result to stdout or send to a file as follows.

$ cat domain-list.txt | tr [:lower:] [:upper:]

uname Command

uname command displays system information such as operating system, network node hostname kernel name, version and release etc.

Use the -a option to show all the system information:

$ uname 

uniq Command

uniq command displays or omits repeated lines from input (or standard input). To indicate the number of occurrences of a line, use the -c option.

$ cat domain-list.txt

uptime Command

uptime command shows how long the system has been running, number of logged on users and the system load averages as follows.

$ uptime

users Command

users command shows the user names of users currently logged in to the current host like this.

$ users

vim/vi Command

vim (Vi Improved) popular text editor on Unix-like operating systems. It can be used to edit all kinds of plain text and program files.

$ vim file

Learn how to use vi/vim editor in Linux along with some tips and tricks.

  1. 10 Reasons Why You Should Use Vi/Vim Editor in Linux
  2. How to Install and Use Vi/Vim Editor in Linux
  3. How to Save a File in Vim Editor in Linux
  4. How to Exit a File in Vim Editor in Linux
  5. Learn Useful ‘Vi/Vim’ Editor Tips and Tricks to Enhance Your Skills
  6. 8 Interesting ‘Vi/Vim’ Editor Tips and Tricks for Every Linux Administrator

w Command

w command displays system uptime, load averages and information about the users currently on the machine, and what they are doing (their processes) like this.

$ w

wall Command

wall command is used to send/display a message to all users on the system as follows.

$ wall “This is TecMint – Linux How Tos”

watch Command

watch command runs a program repeatedly while displaying its output on fullscreen. It can also be used to watch changes to a file/directory. The example below shows how to watch the contents of a directory change.

$ watch -d ls -l

wc Command

wc command is used to display newline, word, and byte counts for each file specified, and a total for many files.

$ wc filename

wget Command

wget command is a simple utility used to download files from the Web in a non-interactive (can work in the background) way.

$ wget -c http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz

whatis Command

whatis command searches and shows a short or one-line manual page descriptions of the provided command name(s) as follows.

$ whatis wget

which Command

which command displays the absolute path (pathnames) of the files (or possibly links) which would be executed in the current environment.

$ which who

who Command

who command shows information about users who are currently logged in like this.

$ who

whereis Command

whereis command helps us locate the binary, source and manual files for commands.

$ whereis cat

xargs Command

xargs command is a useful utility for reading items from the standard input, delimited by blanks (protected with double or single quotes or a backslash) or newlines, and executes the entered command.

The example below show xargs being used to copy a file to multiple directories in Linux.

$ echo /home/aaronkilik/test/ /home/aaronkilik/tmp | xargs -n 1 cp -v /home/aaronkilik/bin/sys_info.sh

yes Command

yes command is used to display a string repeatedly until when terminated or killed using [Ctrl + C] as follows.

$ yes "This is TecMint - Linux HowTos"

youtube-dl Command

youtube-dl is a lightweight command-line program to download videos and also extract MP3 tracks from YouTube.com and a few more sites.

The command below will list available formats for the video in the provided link.

$ youtube-dl --list-formats https://www.youtube.com/watch?v=iR

zcmp/zdiff Command

zcmp and zdiff minimal utilities used to compare compressed files as shown in the examples below.

$ zcmp domain-list.txt.zip basic_passwords.txt.zip
$ zdiff domain-list.txt.zip basic_passwords.txt.zip 

zip Command

zip is a simple and easy-to-use utility used to package and compress (archive) files.

$ tar cf - . | zip | dd of=/dev/nrst0 obs=16k
$ zip inarchive.zip foo.c bar.c --out outarchive.zip
$ tar cf - .| zip backup -

zz Command

zz command is an alias of the fasd commandline tool that offers quick access to files and directories in Linux. It is used to quickly and interactively cd into a previously accessed directory by selecting the directory number from the first field as follows.

$ zz

That’s it for now! As we mentioned before, there are a countless number of commands in Linux. The list is possibly longer than we can offer.

Source

14 Useful Examples of Linux ‘sort’ Command

Sort is a Linux program used for printing lines of input text files and concatenation of all files in sorted order. Sort command takes blank space as field separator and entire Input file as sort key. It is important to notice that sort command don’t actually sort the files but only print the sorted output, until your redirect the output.

This article aims at deep insight of Linux ‘sort‘ command with 14 useful practical examples that will show you how to use sort command in Linux.

1. First we will be creating a text file (tecmint.txt) to execute ‘sort‘ command examples. Our working directory is ‘/home/$USER/Desktop/tecmint.

The option ‘-e‘ in the below command enables interpretion of backslash and /n tells echo to write each string to a new line.

$ echo -e "computer\nmouse\nLAPTOP\ndata\nRedHat\nlaptop\ndebian\nlaptop" > tecmint.txt

Split String by Lines in Linux

2. Before we start with ‘sort‘ lets have a look at the contents of the file and the way it look.

$ cat tecmint.txt

Check Content of File

3. Now sort the content of the file using following command.

$ sort tecmint.txt

Sort Content of File linux

Note: The above command don’t actually sort the contents of text file but only show the sorted output on terminal.

4. Sort the contents of the file ‘tecmint.txt‘ and write it to a file called (sorted.txt) and verify the content by using cat command.

$ sort tecmint.txt > sorted.txt
$ cat sorted.txt

Sort File Content in Linux

5. Now sort the contents of text file ‘tecmint.txt‘ in reverse order by using ‘-r‘ switch and redirect output to a file ‘reversesorted.txt‘. Also check the content listing of the newly created file.

$ sort -r tecmint.txt > reversesorted.txt
$ cat reversesorted.txt

Sort Content By Reverse

6. We are going a create a new file (lsl.txt) at the same location for detailed examples and populate it using the output of ‘ls -l‘ for your home directory.

$ ls -l /home/$USER > /home/$USER/Desktop/tecmint/lsl.txt
$ cat lsl.txt

Populate Output of Home Directory

Now will see examples to sort the contents on the basis of other field and not the default initial characters.

7. Sort the contents of file ‘lsl.txt‘ on the basis of 2nd column (which represents number of symbolic links).

$ sort -nk2 lsl.txt

Note: The ‘-n‘ option in the above example sort the contents numerically. Option ‘-n‘ must be used when we wanted to sort a file on the basis of a column which contains numerical values.

Sort Content by Column

8. Sort the contents of file ‘lsl.txt‘ on the basis of 9th column (which is the name of the files and folders and is non-numeric).

$ sort -k9 lsl.txt

Sort Content Based on Column

9. It is not always essential to run sort command on a file. We can pipeline it directly on the terminal with actual command.

$ ls -l /home/$USER | sort -nk5

Sort Content Using Pipe Option

10. Sort and remove duplicates from the text file tecmint.txt. Check if the duplicate has been removed or not.

$ cat tecmint.txt
$ sort -u tecmint.txt

Sort and Remove Duplicates

Rules so far (what we have observed):

  1. Lines starting with numbers are preferred in the list and lies at the top until otherwise specified (-r).
  2. Lines starting with lowercase letters are preferred in the list and lies at the top until otherwise specified (-r).
  3. Contents are listed on the basis of occurrence of alphabets in dictionary until otherwise specified (-r).
  4. Sort command by default treat each line as string and then sort it depending upon dictionary occurrence of alphabets (Numeric preferred; see rule – 1) until otherwise specified.

11. Create a third file ‘lsla.txt‘ at the current location and populate it with the output of ‘ls -lA‘ command.

$ ls -lA /home/$USER > /home/$USER/Desktop/tecmint/lsla.txt
$ cat lsla.txt

Populate Output With Hidden Files

Those having understanding of ‘ls‘ command knows that ‘ls -lA’=’ls -l‘ + Hidden files. So most of the contents on these two files would be same.

12. Sort the contents of two files on standard output in one go.

$ sort lsl.txt lsla.txt

Sort Contents of Two Files

Notice the repetition of files and folders.

13. Now we can see how to sort, merge and remove duplicates from these two files.

$ sort -u lsl.txt lsla.txt

 Sort, Merge and Remove Duplicates from File

Notice that duplicates has been omitted from the output. Also, you can write the output to a new file by redirecting the output to a file.

14.
 We may also sort the contents of a file or the output based upon more than one column. Sort the output of ‘ls -l‘ command on the basis of field 2,5 (Numeric) and 9 (Non-Numeric).

$ ls -l /home/$USER | sort -t "," -nk2,5 -k9

Sort Content By Field Column

That’s all for now. In the next article we will cover a few more examples of ‘sort‘ command in detail for you. Till then stay tuned and connected to Tecmint. Keep sharing. Keep commenting. Like and share us and help us get spread.

Source

WP2Social Auto Publish Powered By : XYZScripts.com