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:

yum install vsftpd

Command to install FTP on Ubuntu Server:

apt-get install vsftpd

Following are list of commands to create a FTP user:

mkdir /var/www/domain.com
useradd <username>
passwd <username>
chown -R <username> /var/www/domain.com
usermod -d /var/www/domain.com

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:

find {directory} -type f ! -path {exclude dir} -exec grep -l {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

find . -type f ! -path "log" -exec grep -l "hello world" {} \;

 

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:

find . -type f -mtime n

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.

find . -type f -mtime -1

 

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

diff file1.php file2.php

Example #2: Find the differences between two directories recursively

diff -r dir1 dir2

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

find * -maxdepth 0 -type d -exec sh -c "echo -n {} ' ' ; ls -lR {} | wc -l" \;

 

6. Find number of directories in a directory

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

find . -mindepth 1 -maxdepth 1 -type d | wc -l

 

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

find images -type f -name "*.jpg" -exec rm {} +

 

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.

find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

 

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.

tail -n 10 access_log

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

watch tail -n 10 access_log

 

10. Find the top 10 IP hits on the server

cat access_log| cut -f 1 -d ' '|sort|uniq -c|sort -nr|head

 

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

(echo > /dev/tcp/10.0.0.1/443) >/dev/null 2>&1 \
    && echo "It's up" || echo "It's down"

 

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.

wget --user-agent Mozilla/4.0 'wetransfer-down-file-url' -O output-filename

 

13. Download data from FTP using wget

wget ftp://user:pwd@host/filename
#OR
wget --user=NAME --password='PASSWORD' ftp://url/path/file.name

About the author

Sujeet Kr Singh