Linux

Useful Linux commands and their usage

Linux has some handy and useful commands that we often need to use. Here I am listing some useful linux commands and operations that I used during my installation and use of Amazon EC2 servers.

1. Create FTP users

FTP is used for accessing the server files remotely. To use FTP make sure vsftpd service is running on the server already. If it’s not yet installed on server, you can do this using one of the below commands:-

Command to install FTP on CentOS Server:

Command to install FTP on Ubuntu Server:

Following are list of commands to create a FTP user:

Now you will be able to connect to server using the above created FTP user. You can use FileZilla or some other FTP client to connect. If you are able to access all directories other than the one mentioned in the command then you need to restrict the user to that specific directory. Open the file vsftpd.conf (it should be inside /etc/vsftpd directory) and set the value of chroot_local_user to Yes.

 

2. Finding some particular text in all files in a directory

Syntax of command to list all files containing a particular text:

{directory} is a path of directory in which we want to look. If we want to search in current direct then we use dot (.)

{exclude dir} is name of directory that we want to exclude from the operation.

{text} is word or phrase we want to search.

Example #1: Search all files in current directory that contains text “hello world” but do not search this text in log directory

 

3. Finding list of all files edited since a particular date.

We can find the list of all files which were edited since a particular day’s very easily using the  find  command. Here is the syntax of command for listing files:

n is the days since we want to see the edited files where its values can be less than and equal to zero i.e. n<=0

Example #1:  Search all files edited since yesterday.

 

4. Finding the difference between two file/directory

We often came across a situation where we need to find the difference between two files or directory to check what new is added or updated since last backup of the code was taken. Though we can do it easily GIT DIFF command if the code is managed through a GIT repo. But what if the is not managed on GIT? Linux has another utility using which we can accomplish the task, the DIFF command. DIFF works similar to GIT DIFF command. Here are few examples to demonstrate the usage:

Example #1: Find difference between two files

Example #2: Find the differences between two directories recursively

You can more detailed usages of DIFF  using “diff –help” command.

 

5. Find number of files in all directories of current directory

The following command will list the count of all files inside all directories in the current directory

 

6. Find number of directories in a directory

The following command will list the count of all directories in the current directory

 

7. Find number of files in current directory with a specific extension

The following command will list the count of all files in images directory with extension .jpg

 

8. Correcting files and directory permission

In the Linux, the default directory permission is 755 and default file permission is 644. The permission of all directories and the files can be corrected in bulk using these commands.

 

9. View last few lines of a huge file.

In the Linux, the size of log files increases very hugely over the period of time. It’s become very difficult situation we need to view log file to check some issue due to the huge size of that file. A very nice Linux command saves our time that case. TAIL and WATCH  are those commands here are the demonstration of these commands.

Example #1: View last 10 lines of the access log file.

Example #2: Watch last 10 lines of the access_log file. The watch command displays the updated last 10 lines.

 

10. Find the top 10 IP hits on the server

 

11. Command to check server: port connectivity in case telnet is not installed or disabled on the server

 

12. Download file from WeTranfser using wget

Open the wetransfer download page and click on download and stop immediately. It will log the download URL in the download section of the browser. Copy the download URL and use the following in the Linux terminal.

 

13. Download data from FTP using wget

About the author

Sujeet Kr Singh