Search Results for: cat

mtime – file modification timestamp in Unix

mtime is one of the three timestamps in Unix that are maintained for each file in most of the filesystems.

Purpose of mtime

The real purpose of the mtime timestamp is to track the last time of changing the contents of a file. Various commands will allow you to access this information later. For example, ls command allows showing list of files along with their last modification times (it's also possible to get ls to confirm the last access time (atime timestamp)for any file).

mtime example

Here's how you can see mtime in real life. Let's create a file named example.txt and get a full ls listing on it:

greys@ubuntu:~$ date
Fri Sep 28 10:25:40 IST 2012
greys@ubuntu:~$ > example.txt
greys@ubuntu:~$ ls -l example.txt
-rw-r--r-- 1 greys greys 0 2012-09-28 10:25 example.txt

As you can see, the last modification of the "example.txt" file is 10:25am.

Now let's wait a minute:

greys@ubuntu:~$ sleep 60

…confirm the file's mtime is still the same:

greys@ubuntu:~$ ls -l example.txt
-rw-r--r-- 1 greys greys 0 2012-09-28 10:25 example.txt

… and now make the change by adding a line "change" to our file:

greys@ubuntu:~$ echo "change" >> example.txt

And if we check the file's mtime timestamp, it will be updated – in my case 10:27am:

greys@ubuntu:~$ ls -l example.txt
-rw-r--r-- 1 greys greys 7 2012-09-28 10:27 example.txt

More info on mtime

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

cat – concatenate files and print to the standard output

cat is a simple yet very useful Unix command. It takes a name of one or more text files, and then shows their contents to the standard output as one stream of data.

cat command example

greys@ubuntu:~$ cat /etc/kernel-img.conf
do_symlinks = yes
relative_links = yes
do_bootloader = no
do_bootfloppy = no
do_initrd = yes
link_in_boot = no
postinst_hook = /sbin/update-grub
postrm_hook   = /sbin/update-grub

for two files, it looks like this:

greys@ubuntu:~$ cat /etc/issue
Ubuntu 7.04 \n \l
\
greys@ubuntu:~$ cat /etc/issue /etc/kernel-img.conf
Ubuntu 7.04 \n \l
\
do_symlinks = yes
relative_links = yes
do_bootloader = no
do_bootfloppy = no
do_initrd = yes
link_in_boot = no
postinst_hook = /sbin/update-grub
postrm_hook   = /sbin/update-grub

[Read more...]

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

locate – quickly find files in Linux

Today I'd like to show you one more option you have when searching for files in Linux. If you have a locate tool installed, you'll be able to find any file almost instantly.

How does locate command work?

locate uses a pretty simple principle – instead of going through your filesystem directory tree every time you need a certain file found, it consults a database which stores locations of most files in your system. The locate database (locatedb) is updated nightly with a separate command. The update occurs during night hours when peak usage of your system is very unlikely, but this means that using such a database through the day will provide instant results.

[Read more...]

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

How To Find a Location of a Directory in Unix

Very quick tip for you today, I just see that many of visitors of this block are curious how they can find a directory in Unix – and so here's a command to help you do just that.

Finding directories in Unix

There's nothing better than to employ the find command. As you might remember, among many things, this wonderful tool allows you to search files by their type. Since nearly everything in Unix is a file, this means you can find directories.

[Read more...]

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

tee: Replicate Standard Output

Now and then I come across a situation when I need to run a script or a Unix command and would like to not only see the output of it on the screen, but also save this output to some log file. Redirecting the standard output using standard Unix stream redirection isn't always useful because your output will either be shown to you, or sent to the file – but not both at the same time

tee command

That's where the tee command becomes really useful. You pipe your output to this command, and let it take care of the rest.

[Read more...]

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

GRUB Boot Loader

GRUB is the "GRand Unified Bootloader", a boot loader that supports multiple boot options, such as various Linux boot modes, and other operating systems like Windows, BSD, and so on.

A boot loader simply loads up what's necessary for an operating system to start up, such as where's the kernel and with what options should it be loaded. Once it loads it up it hands off control to the kernel to complete the process.

You can configure GRUB boot options by editing the /boot/grub/grub.cfg or in some systems /etc/grub.conf file or using one of the graphical tools that make doing this easier and safer. For most Linux users the grub configuration will be automatically created, even including other operating systems you might have installed.

Key part of the grub.cfg file is where it lists the operating system options. One such entry may look something like this (from a sample configuration):

menuentry 'Ubuntu, with Linux 2.6.32-24-generic' –class ubuntu –class gnu-linux –class gnu –class os { recordfail insmod ext2 set root='(hd1,6)' search –no-floppy –fs-uuid –set 6655ee5e-45d1-4d1c-9a7d-10f30f16e745 linux /boot/vmlinuz-2.6.32-24-generic root=UUID=6655ee5e-45d1-4d1c-9a7d-10f30f16e745 ro quiet splash initrd /boot/initrd.img-2.6.32-24-generic }

The menuentry line defines the name of the entry and proceeds to specify some options. Key options here are set root, which specifies the partition on which the root directory of the operating system resides, the linux option followed by a path on the specified root directory where the kernel is and with which options to load it, and the initrd line specifying the location of the initialization file.

Besides these menu entries and options you can also set some general options for GRUB. A couple basic ones that may be of interest are GRUB_DEFAULT and GRUB_TIMEOUT.

GRUB_DEFAULT sets which of the menu entries will be selected by default, and booted into automatically after a timeout. Its numeric value corresponds to the order in which a menu entry is listed, keeping in mind that it starts at "0", not "1". So if you want the first menu entry to be the default you set GRUB_DEFAULT=0 or if you want the third one to be the default set GRUB_DEFAULT=2, and so on.

You can also set GRUB_DEFAULT=saved if you want the default to be the last selected entry on the previous boot.

GRUB_TIMEOUT simply sets the number of seconds it will count to before booting the default entry automatically. It's typically set to 10 seconds: GRUB_TIMEOUT=10. You can change it to whatever you want, or put "-1" if you want to disable the timeout and have it boot only when you explicitly tell it to. Of course, "0" will make it boot immediately.

If you've edited the grub.cfg file and saved it you also need to update grub with the new configuration by running this command:

sudo update-grub

Finally, if you need an easy way to modify GRUB bootloader configuration with a graphical user interface you can use grub-customizer or Boot Repair.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Hard Link

Files in a file system are kept track of by so called "index nodes" or "inodes" which store some metadata about each file such as its type, file, access/change/modification/deletion times, owner, group, and so on (not including file names though). Typically, only one file can be associated with a single inode, but this is where hard links come in.

A hard link allows multiple files to be associated with a single inode where one file is a link, and the other is the original file. The key thing is that the link refers to the same inode pointing to the same data. This is different from a soft link, also known as a "symbolic link" or "symlink" which only contains an abstract path to the original file rather than being associated with the original file's inode.

Because of this hard links don't work across multiple file systems or partitions, and they also cannot link to directories. Their biggest advantage, however, is that they stay linked to the original file wherever on the same file system they are, even if the path location of the original file changes (because the link refers to the inode not the path).

In contrast soft links refer to nothing if the original file changes its location, becoming broken links. Their advantage, however, is that they can link across file systems as well as link to directories (because they refer to the path).

To create a hard link to a file use a command like this:

ln /home/user/file.txt /home/user/file-link.txt

The /home/user/file-link will be a hard link to /home/user/file, and will refer to said file even if you move it to /home/user2/file.txt.

To create a soft link or a symlink run the same command, but with an -s option:

ln -s /home/user/file.txt /home/user/file-link.txt  

That will create file-link.txt as a link to file.txt, but if you move file.txt from /home/user to /home/user2 the link, still pointing to the original path, will be broken.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

How to Use visudo

The visudo command is a safe and secure way of editing the /etc/sudoers file on UNIX and Linux systems. Since the sudoers file determines which users can run administrative tasks, those requiring superuser privileges, it is a good idea to take some precautions when editing it, and that's what visudo does.

It locks the sudoers file so it cannot be edited by anyone else simultaneously. It also checks the syntax of your edits and provides basic sanity checks. If someone else is editing the file you'll get a message to try later, and if there are errors in your edits it wont save them.

Preventing simultaneous editing by someone else is helpful to ensure your edits aren't lost, and saving a sudoers file without errors is important because you could otherwise end up locked out of your system. An unreadable sudoers file will prevent you from running administrative tasks by using the sudo command or becoming root, and editing the sudoers file itself requires those privileges. So you really don't want to screw that one up.

Visudo is basically a wrapper for a text editor such as vi or nano. Vi is traditionally the default unless your distribution or OS has something else set up. For basics on how to use vi for editing check out the vi survival guide.

Visudo has a built in list of supported editors that can be used, and you can change which it will use by setting the "EDITOR" environment variable on the command line like this: export EDITOR=nano. This will set nano as the default editor. To save this permanently add the same line to the .bashrc file in your home directory. On Ubuntu, where nano is actually set as the default, you can also change it by running sudo update-alternatives –config editor and then selecting your preference.

Editing Sudoers

To open up the /etc/sudoers file for editing with visudo simply run sudo visudo.

Before making any edits it's a good idea to check the existing configuration, and understand what everything means. One line you'll definitely encounter is this:

root    ALL=(ALL:ALL) ALL

This gives the root user all of the superuser privileges, as can be expected. The format of the rule set such as this is as follows:

user hosts=(users:groups) commands

What you're doing is specifying which commands can a given user run under which circumstances. In case where all of them are set to ALL, like for root, it means that the user can run all commands on all hosts, as all users and groups.

If all you want is enable another user with the same powers as root, obtainable by issuing the sudo command before the desired command, you can just copy the root line and change "root" with your username, in this example "daniel":

daniel    ALL=(ALL:ALL) ALL

But if you don't want to give all of the privileges you can adjust the rules. For example you can allow "daniel" to only run certain commands:

daniel ALL=(ALL:ALL) mytop,cat,tail

Besides users you can also give superuser permissions to groups using a % indicator:

%admin ALL=(ALL) ALL

This would allow all users in the admin group to run all commands as root.

Aliases

Finally, you can set up aliases to group multiple entries into a single one for use in these statements. There are four types of aliases: User_Alias for listing users, Runas_Alias for listing users a given user can run as, Host_Alias for listing hosts, and Cmnd_Alias for listing commands.

Aliases are useful if you have a more complex set up with multitude of users that should have varying degrees of privileges on the system. To set up an alias just state the alias type, its name, and then the list of users, hosts or commands you want to associate it with. For example to set up a User_Alias you can do this:

User_Alias MANAGERS = steve,bill,james

All the other aliases follow the same format only with the different specified type, and listing different types of things, like users, hosts or commands. If we wanted to put the three commands from the above example with the "daniel" user under an alias we could do this:

Cmnd_Alias READ = mytop,cat,tail

And then instead of listing these two commands in our configuration for daniel we can just specify the READ alias:

daniel ALL=(ALL:ALL) READ

It works the same way for other types of aliases. If we want to give the same privileges to users steve, bill, and james we can say:

MANAGERS ALL=(ALL:ALL) READ

You get the idea.

These are the basics of using visudo and editing the sudoers file with it. We recommend you check out the manual pages if you ever need more detailed reference, like man visudo and man sudoers. You can also see a sample sudoers file with many examples at its web site.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Most Important sshd Configuration Options

SSH, or Secure SHell, allows the user of one computer on the network to connect to and use the shell of another over a secure connection. It consists of two basic components, the SSH client used to connect to a remote server, and the SSH server daemon (sshd) running on the server to accept SSH connections from elsewhere.

Configuration for the sshd server is found in the /etc/ssh/sshd_config file. The client configuration is in /etc/ssh/ssh_config.

Here are some of the most important configuration options for an SSH server:

Port

The default port for SSH is 22, which is typically fine, but it could be changed to some other available port if you want to throw an extra obstacle to would be unauthorized attempts to connect.

PermitRootLogin

This option can be set to either yes or no. If it is set to yes then it will allow using SSH to log in directly as root by running something like ssh root@host.net from the client computer. It may be a good idea to set this to "no" in order to close even the remote possibility of someone cracking through the root password and wreaking havoc. Just a decent precaution.

AllowUsers

With this option you can set to allow only some users on the system to connect via SSH. For multiple users separate them by spaces. For example:

AllowUsers james kevin

That will allow only james and kevin users to connect.

LoginGraceTime

This is the amount of time SSH will wait on the user to authenticate before cutting the connection. By default it is set to 120, or 2 minutes, but it can be reduced if you want to diminish chances of someone successfully attempting a brute force attack.

PasswordAuthentication

Set to yes by default this enable password authentication, which definitely should be enabled unless you have public key authentication enabled, because otherwise basically anyone could connect.

PubkeyAuthentication

An alternative or an addition to PasswordAuthentication setting this to yes could significantly increase security. For it to work you also need an option that specifies where the authorized keys are:

AuthorizedKeysFile ~/.ssh/authorized_keys

TCPKeepAlive

Set to yes by default this option checks the status of your connection by sending keepalive messages to the client. If there are network interruptions it will then close the connection rather than continue to use up resources.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

How to Use wget and curl

Both wget and curl are command line tools for transferring files over the network via various network protocols like HTTP or FTP. Wget is a GNU Project by the Free Software Foundation licensed under the GNU GPL whereas Curl is an independent project licensed under a variant of the MIT license.

Curl is also based on a libcurl library, part of the same project, which makes it more suitable for use in programming various applications. It is generally more flexible and featureful whereas wget is simpler.

Simple download

Here's how to initiate a simple download with both wget and curl.

wget ftp://mirrors.kernel.org/gnu/unifont/unifont-7.0.01/unifont_upper-7.0.01.ttf

curl -O ftp://mirrors.kernel.org/gnu/unifont/unifont-7.0.01/unifont_upper-7.0.01.ttf

This will download the file to the current working directory with its original file name (which is what the -O option passed to curl is for). You can run curl without any options as well, but it will dump the file contents on to the screen as it downloads.

Download multiple files at once

You can also download multiple files in one go by specifying multiple URLs:

wget ftp://mirrors.kernel.org/gnu/unifont/unifont-7.0.01/unifont-7.0.01.ttf ftp://mirrors.kernel.org/gnu/unifont/unifont-7.0.01/unifont_upper-7.0.01.ttf

curl -O ftp://mirrors.kernel.org/gnu/unifont/unifont-7.0.01/unifont-7.0.01.ttf -O ftp://mirrors.kernel.org/gnu/unifont/unifont-7.0.01/unifont_upper-7.0.01.ttf

Specifying -O before each URL isn't necessary, but we found that not specifying it results in the second file being downloaded being dumped into the command line.

That said, it is also possible to specify multiple files for download in accordance to a given range of numbers or letters in the file name.

With curl you can also download multiple files with sequential numbers or letters in their names like file1, file2, file3, and so on by specifying the range in the brackets:

curl -O ftp://example.com/file[1-50].txt

Multiple nested sequences are possible too, like this:

curl -O http://example.com/[2002-2014]/file{a-z}.txt

Resume downloads

Both wget and curl allow you to resume partial downloads, which is useful if a network outage in the middle of the download interrupted it, for example, and you want to continue where you left off.

With wget you simply use the -c or –continue option. Some have a habit of always passing this option just in case.

wget -c ftp://mirrors.kernel.org/gnu/unifont/unifont-7.0.01/unifont-7.0.01.ttf

With curl you can use the -C – options:

curl -C - ftp://mirrors.kernel.org/gnu/unifont/unifont-7.0.01/unifont-7.0.01.ttf

That's in a nutshell how to use wget and curl. They're both powerful, curl a bit more powerful than wget, and you can find all about what they can do in their manual pages: man wget, and man curl.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS