Finding Linux files with "locate"

12 Apr, 2010

Kid with magnifying glass
There are a few of ways to find files on a Linux server, but most of them involve actually searching through each file in the filesystem until the correct one is located. This can be time consuming and taxing on the server’s CPU load, especially if you have a lot of files.

Linux has two commands that make searching a little easier: locate and slocate. Unlike other find utilities, locate searches through a database that contains information about the filesystem, bringing up the search results almost instantaneously. The command to update the database is called “updatedb”, and many Linux distributions have the command run via cron every day.

The alternative version of locate, called slocate, is a security-enhanced version that only allows the user to find files he or she has the permission to access. While locate is a great tool for finding things on a server, it does have its issues. For one, you will only find files that were added or changed prior to the last updatedb execution. Furthermore, the very process of updating the database can be taxing on the server, even if it is only once a day. For the right situations, however, locate is a very useful Linux tool.

Photo Source: Flickr

(0) Comment Categories : Software, VPS & Dedicated
Tag: , , , , , , , ,

How to merge two different files in Linux

17 Feb, 2010

sdiff command

In yesterday’s post, we learned how to find the discrepancies between two similar files using a command called diff. With it, we were able to compare them and identify the actual lines containing differences.

Once you have identified the differences between the two, you have a couple of options. If only one file is different, you can delete it, but if both files have slight differences and you need the changes from both, you will need to include the changes from both. That would normally involve creating a third file or revision containing both changes.

With the command sdiff you can display the file differences side-by-side and can also merge the two file changes into a third output file. To run sdiff, use the following command string:

sdiff -o outfile file1.txt file2.txt

It will display the two differences (see the image above). Press Enter, and it will then provide you with options. Choose the one you want and then Quit.

(0) Comment Categories : Software, VPS & Dedicated
Tag: , , , , , ,

How to compare two files in Linux

16 Feb, 2010

diff command
Question: I have two scripts in a directory on my server. One is the right one and one is not, but I do not remember which is which. How can I compare the two files?

Answer: Linux and UNIX-like operating systems usually come with a comparison command called “diff”. This command will display line-by-line differences between two files. It is useful for software developers, but web developers and web application managers will find it useful as well.

To use it, just enter:

diff script1.php script2.php

It will search through both PHP scripts and find any lines that are different. The output will look something like this:

if(!function_exists('add_action')){
header('HTTP/1.0 404 Not Found');
header('Location: ../../');
> exit();
}

In the first file “exit();” is not present, while the second file correctly contains it. Tomorrow we will learn how to merge the two differing files into one correct file.

(0) Comment Categories : Software, VPS & Dedicated
Tag: , , , , ,

How to delete duplicate files in Linux

16 Feb, 2010

terminal icon
Question: Two of my directories on my server have some the same files. How can I easily delete any duplicates while preserving the unique files?

Answer: There exists a Linux tool for just about everything, and this is no exception. You need a tool called fdupes, which searches the path you give it for duplicate files and uses several comparison techniques (sizes, MD5 signatures, byte-by-byte comparison) to find and eliminate duplicates.

First install fdupes. In RHEL/Fedora/CentOS, use the rpmforge repository enter:

# yum install fdupes

To find duplicates in the /etc directory, you would enter:

# fdupes /etc

To force fdupes to prompt you whether to preserve or delete files:

#fdupes -d /etc

As you can see, fdupes might be just what you need to clean up your server and possibly increase that precious disk space.

Source: nixCraft
Photo: Wikimedia Commons

(0) Comment Categories : Software, VPS & Dedicated
Tag: , , , , , , ,

How to list all users in Linux

15 Feb, 2010

User list in Linux
Question: How do I see all of the users that have accounts on my server?

Answer: In a previous post, we covered the “who” command, which will tell an administrator which users are currently logged into the system, but another important thing to know about your server is which users you actually have. You will want to look at a complete list of users to make sure all the existing users are supposed to be there.

To list all users, enter the following command:

cat /etc/passwd | cut -d":" -f1

Also, to find out the total number of accounts, enter:

cat /etc/passwd | wc -l

Please note that this will look for all accounts, including those created by Linux, such as “mail” and “haldaemon”, but it will also include manually created local users.

(0) Comment Categories : Software, VPS & Dedicated, Web servers
Tag: , , , , ,

Whois searching from the Linux command line

1 Feb, 2010

Domain in grasp
There are many web-based Whois search tools and even some desktop ones available for free use, but with the “whois” command on your Linux server, you can use it to perform more complex tasks and even automate the process. For example, you can type:

whois internetblog.org.uk

It will return the registrar, registration status, the date it was registered, renewal date, name servers, address of the registrant, and other important information. Now, if you wanted to automatically send that information to a file, enter:

whois internetblog.org.uk > whois-list

Now, you can continue adding to that list:

whois anydomain.tld >> whois-list

This will append the whois information from the second domain under the information from the first. You can use any Linux/Unix command that can be used to manipulate output to customize the results of your Whois search. For more information about “whois”, type: “man whois” from the command line.

Photo by http://www.anna-OM-line.com

(0) Comment Categories : Software, VPS & Dedicated, Web servers
Tag: , , , , ,

Monitor disk space on a dedicated server

28 Jan, 2010

Bash shell screen
Question: How can I configure a Unix server to tell me when the hard drive is getting full?

Answer: Unix, Linux, and Unix-like systems have a command called “df” that reports disk space information about the server’s file system. Entering the following command:

# df -P /

produces output like the following:

Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/mapper/VolGroup00-LogVol00 6983168 3824556 2798164 58% /

The next step involves configuring a shell script and setting up a cron job to periodically check the disk space and send a warning to notify you when the server’s file system is getting full. For a complete tutorial, see nixCraft.

Photo: Wikimedia Commons

(0) Comment Categories : VPS & Dedicated, Web Hosting
Tag: , , , , , ,

Identifying memory consumption by process in Linux

25 Jan, 2010

pmap command output
Question: How do I find out how memory is used by a particular process in Linux?

Answer: On a Linux server, a nifty little command called “pmap” will do just what you want. The command string that you will need to enter as root is:

# pmap -d PID

But first you will need to know what the PID is. To find the PID for a particular process, run the “ps” command as explained in a previous post. Once you have the process ID, you are now ready to find out more about it. For this example, let’s suppose the PID is 22938. You would enter:

# pmap -d 22938

What you get in a complete memory usage output of every library, command, and file used by that process. For example, 22938 on my computer happened to be Firefox. On a server, it would obviously be something server-related. If Firefox is hogging memory or causing problems, I can pinpoint the exact source of the memory consumption.

Source: nixCraft

(0) Comment Categories : VPS & Dedicated, Web Hosting
Tag: , , , , , ,

How to redirect Linux command output to a file

29 Dec, 2009

Linux kernel booting
All Linux servers have a useful feature that is only one character long. The character is > , and it makes saving command output to files extremely easy. It can make interpreting long command output data much easier and less time consuming.

For example, if you wanted to list the entire contents of /usr/lib, you could always run ls /usr/lib, but what you will get are pages of files and directories, probably more than your terminal window will even buffer. To solve this, all you have to do is add a > to the end of the command, followed by the location and name of a new file that will hold the information.

For example, you could save the contents to a file in /home/user and call the text file “libraries”. This assumes that the file does not already exist. If it does, > will overwrite it. Enter the following:

ls /usr/lib > /home/user/libraries

If you already have a file created with data inside and just want to append more data onto the end, add a second >

ls /usr/lib > > /home/user/libraries

Photo Source: Flickr

(0) Comment Categories : Software, Web Hosting
Tag: , , , , , ,

Find out which users are logged in

27 Nov, 2009

Who command
Question: How do I find out which users are logged in to my Linux server?

Answer: To find out who is logged on to your server, you can run a very simple command:

who

You will need to login to your server via SSH order to run this command.

This will tell you the person’s username as well as the date and time they logged in. Who will also tell you a few other things. “who -b” will tell you the last time the system was booted. Enter “who -q” to find out the names of the logged in users and then a summary count of total users.

If you need to make sure you are logged in as a particular user, you can also type

whoami

This will tell you your own username.

This command is very useful if you suspect there are unauthorized users on your server or if you suspect someone of hacking into an authorized user’s account when you know they are not logged in. Therefore, it can be used for general information and for security purposes. For more information about the “who” command, type “man who” from the command line.

(0) Comment Categories : VPS & Dedicated, Web Hosting
Tag: , , , , , ,